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

Duncan McGregor :

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?

Duncan McGregor :

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/

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443847&siteId=1