Search code examples
javaamazon-web-servicesaws-lambdaaws-api-gateway

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?


Solution

  • 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.

    Source: Error Handling Patterns in Amazon API Gateway and AWS Lambda