Error handling

In an AWS Lambda function written in Java, error handling can be done by using the usual try/catch semantics.

If one requires the exception context to be propogated back to the caller (only in the case of a synchronous execution), the following code shows how Lambda returns the exception context in a JSON format. Notice how the stack trace elements are a part of the JSON array:

{  "errorMessage": "An invalid input was supplied",  "errorType": "java.lang.Exception",  "stackTrace": [    "example.Hello.handler(Hello.java:9)",    "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",    "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)", "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)", ...

Get Hands-On Serverless Applications with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.