Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to signal an error in AWS Lambda for Java without throwing an exception?

If my Lambda throws an Exception with the message 404 then the response as seen in API Gateway is

{
  "errorMessage":"404",
  "errorType":"java.lang.Exception",
  "stackTrace":[..."]
}

and I can match on the errorMessage to affect the HTTP result.

However if I return effectively the same result, viz:

{
  "errorMessage":"404",
  "errorType":"Error"
}

API Gateway doesn't seem to recognise that there is an error and always returns a 200.

Is there any way for my nice functional code to signal an error without throwing an exception?

like image 333
Duncan McGregor Avatar asked Jul 14 '17 17:07

Duncan McGregor


People also ask

How do you show errors in Lambda?

You can see Lambda function errors from monitoring tab on Lambda default view.

Can an AWS Lambda function throw an exception?

The Lambda runtime environment converts the event document into an object, and passes it to your function handler. If Lambda encounters an error, it returns an exception type, message, and HTTP status code that indicates the cause of the error.

How do you make a Lambda fail?

Lambda functions can fail in three cases: An unhandled exception is raised — whether if we received an invalid input, an external API failed, or just a programming bug occurred. Timeout — Lambda running longer than the configured timeout duration is violently closed with a 'Task timed out after …


2 Answers

The Lambda function must exit with an error in order for the response pattern to be evaluated – it is not possible to “fake” an error response by simply returning an “errorMessage” field in a successful Lambda response.

https://aws.amazon.com/blogs/compute/error-handling-patterns-in-amazon-api-gateway-and-aws-lambda/

like image 92
Duncan McGregor Avatar answered Nov 03 '22 15:11

Duncan McGregor


It is more of a generic question rather than Java-related as AWS API Gateway integration has nothing much to do with the language in which AWS Lambda function is written.

You can achieve returning custom error message by following the guidelines from the AWS documentation here: Handle Custom Lambda Errors in API Gateway

For Lambda custom integrations, you must map errors returned by Lambda in the integration response to standard HTTP error responses for your clients. Otherwise, Lambda errors are returned as 200 OK responses by default and the result is not intuitive for your API users.

There are two types of errors that Lambda can return:

  1. Standard Errors
  2. Custom Errors

In your API, you must handle these differently.

With the Lambda proxy integration, Lambda is required to return an output of the following format:

{
  "isBase64Encoded" : "boolean",
  "statusCode": "number",
  "headers": { ... },
  "body": "JSON string"
}

In this output, statusCode is typically 4XX for a client error and 5XX for a server error. API Gateway handles these errors by mapping the Lambda error to an HTTP error response, according to the specified statusCode. For API Gateway to pass the error type (for example, InvalidParameterException), as part of the response to the client, the Lambda function must include a header (for example, "X-Amzn-ErrorType":"InvalidParameterException") in the headers property.

For more details, check out this official document from AWS here: Handle Lambda Errors in API Gateway

like image 45
Abdullah Khawer Avatar answered Nov 03 '22 15:11

Abdullah Khawer