lambci / docker-lambda

Docker images and test runners that replicate the live AWS Lambda environment
MIT License
5.83k stars 431 forks source link

"cause" is missing in lambci/lambda:java8 #227

Closed andresvia closed 4 years ago

andresvia commented 4 years ago

Coming from twitter: https://twitter.com/hichaelmart/status/1197986958955483136?s=20

Given this code:

package exceptions;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class Exceptions implements RequestHandler {

    @Override
    public Object handleRequest(Object o, Context context) {
       throw new RuntimeException(new Cause());
    }

}
package exceptions;

public class Cause extends Throwable {
}

And these docker images:

lambci/lambda                                                             java8                            sha256:dea85f569727d89c7eb8767658cc0dada73ddb9a90fafd570a2c28def893d16a   cd514cb2cb11        24 hours ago        701MB
lambci/lambda                                                             java8-andres-backup              sha256:9f97c1886601d56fdd0599ecc66910dc6ac88ccfea5386ab70cd931edf5d7b96   87eb326d9890        3 months ago        691MB

The image with hash 9f97c1886601d56fdd0599ecc66910dc6ac88ccfea5386ab70cd931edf5d7b96 includes the "cause" in the error, but the newer dea85f569727d89c7eb8767658cc0dada73ddb9a90fafd570a2c28def893d16a does not.

New image pull and run:

# ok
docker pull lambci/lambda:java8 
java8: Pulling from lambci/lambda
Digest: sha256:dea85f569727d89c7eb8767658cc0dada73ddb9a90fafd570a2c28def893d16a
Status: Image is up to date for lambci/lambda:java8
docker.io/lambci/lambda:java8

# ok
docker run --rm -v "$PWD":/var/task:ro,delegated lambci/lambda:java8 exceptions.Exceptions::handleRequest | jq . 
START RequestId: 4b3f755d-8ddb-1181-7bc8-6696ca07b22b Version: $LATEST
java.lang.RuntimeException: exceptions.Cause
    at exceptions.Exceptions.handleRequest(Exceptions.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: exceptions.Cause
    ... 5 more

END RequestId: 4b3f755d-8ddb-1181-7bc8-6696ca07b22b
REPORT RequestId: 4b3f755d-8ddb-1181-7bc8-6696ca07b22b  Init Duration: 812.55 ms    Duration: 249.64 ms Billed Duration: 300 ms Memory Size: 1536 MB    Max Memory Used: 75 MB  
{
  "errorType": "java.lang.RuntimeException",
  "errorMessage": "exceptions.Cause",
  "stackTrace": [
    "exceptions.Exceptions.handleRequest(Exceptions.java:10)",
    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
    "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
    "java.lang.reflect.Method.invoke(Method.java:498)"
  ]
}

Old "backup" image:

# ok
docker run --rm -v "$PWD":/var/task:ro,delegated lambci/lambda:java8-andres-backup exceptions.Exceptions::handleRequest | jq . 
START RequestId: a9f2dd8e-8231-4949-ba2a-8c8908f32d13 Version: $LATEST
java.lang.RuntimeException: exceptions.Cause
    at exceptions.Exceptions.handleRequest(Exceptions.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: exceptions.Cause
    ... 5 more

END RequestId: a9f2dd8e-8231-4949-ba2a-8c8908f32d13
REPORT RequestId: a9f2dd8e-8231-4949-ba2a-8c8908f32d13  Duration: 248.07 ms Billed Duration: 300 ms Memory Size: 1536 MB    Max Memory Used: 8 MB
{
  "errorMessage": "exceptions.Cause",
  "errorType": "java.lang.RuntimeException",
  "stackTrace": [
    "exceptions.Exceptions.handleRequest(Exceptions.java:10)",
    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
    "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
    "java.lang.reflect.Method.invoke(Method.java:498)"
  ],
  "cause": {
    "errorMessage": "exceptions.Cause",
    "errorType": "exceptions.Cause",
    "stackTrace": [
      "exceptions.Exceptions.handleRequest(Exceptions.java:10)",
      "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
      "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
      "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
      "java.lang.reflect.Method.invoke(Method.java:498)"
    ]
  }
}

Thank you @mhart , you are doing a terrific job!

mhart commented 4 years ago

Hmmm, yep I can see how that happened – I've unified all runtimes to use the same API for error reporting, and I had never noticed that Java had this field before (I don't think any of the other runtimes do).

Should have a chance to figure out the best fix for this in a couple of hours 👍

andresvia commented 4 years ago

For C# is explicit in the docs: https://docs.aws.amazon.com/lambda/latest/dg/dotnet-exceptions.html

Some blog post mention it: https://aws.amazon.com/blogs/compute/redirection-in-a-serverless-api-with-aws-lambda-and-amazon-api-gateway/

I had fun.

mhart commented 4 years ago

Truedat! I'm using the custom runtime API and it doesn't mention it – but I'll add it

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html#runtimes-api-invokeerror

mhart commented 4 years ago

Have pushed the new images with this fix – thanks for reporting!