zio / zio-lambda

AWS Lambda Runtime built with ZIO
https://zio.dev/zio-lambda
Apache License 2.0
38 stars 14 forks source link

NullPointerException in JsonEncoder #131

Open jlandahl opened 2 years ago

jlandahl commented 2 years ago

I have a lambda that I've compiled with GraalVM into a native binary. When I try to test it I get the following:

timestamp=2022-09-16T20:32:51.642769Z level=ERROR thread=#zio-fiber-0 message="" cause="Exception in thread "zio-fiber-6" java.lang.NullPointerException: null
at zio.json.JsonEncoder$$anon$2.unsafeEncode(JsonEncoder.scala:124)
at foo.FooLambda.apply(FooLambda.scala:10)
at zio.lambda.internal.LoopProcessor.Live.loop(LoopProcessor.scala:32)
at zio.lambda.ZLambda.run(ZLambda.scala:24)"
END RequestId: 5f9c59f2-6e6d-4418-98bb-05bbd98d466c
REPORT RequestId: 5f9c59f2-6e6d-4418-98bb-05bbd98d466c Duration: 366.76 ms Billed Duration: 722 ms Memory Size: 512 MB Max Memory Used: 95 MB Init Duration: 354.70 ms
RequestId: 5f9c59f2-6e6d-4418-98bb-05bbd98d466c Error: Runtime exited with error: exit status 1
Runtime.ExitError

Since it's happening in JsonEncoder's string encoder, I'm guessing that the program ran, but then blew up when trying to render the return string to JSON. Here's the program in question:

object FooLambda extends ZLambda[LambdaEvent, String] {
  override def apply(event: LambdaEvent, context: Context): Task[String] =
    ZIO.scoped(FooApp.program).map { case (created, deleted) => s"Created: $created, Deleted: $deleted" }
}

Line 3 here is line 10 in the traceback.

Any tips on how to debug this?

dbuschman7 commented 2 years ago
jlandahl commented 2 years ago

How about splitting up the line in many lines to isolate exactly which part is causing the error.

The exception is thrown in the JsonEncoder for strings, which suggests to me that my code has already run and it's trying to turn the returned string into JSON.

Send JSON back already encoded

If you look at how ZLambda is defined:

abstract class ZLambda[E: JsonDecoder, A: JsonEncoder] extends ZIOAppDefault ...

I don't have any control over the encoding process. It's called automatically after my program returns a string.

jrmsamson commented 2 years ago

Hi @jlandahl,

Can you try to wrap the string with a case class and provide an implicit JsonEncoder for it and see what you get?

BTW, what version of zio-lambda are you using? I'll try to take some time to look into this.

jlandahl commented 2 years ago

This is happening with 1.0.0-RC6. I'll try what you suggest with a case class and report back.

jrmsamson commented 1 year ago

@jlandahl still having this issue? If not, I'm gonna close this one.

dbuschman7 commented 1 year ago

@jlandahl This is working for us right now:

object ProfileQueryLambda extends ZLambda[AwsAPIGatewayProxyRequestEvent, LambdaResponse]