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?
You can see Lambda function errors from monitoring tab on Lambda default view.
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.
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 …
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/
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With