问题
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?
回答1:
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/
回答2:
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:
- Standard Errors
- 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
回答3:
I think this is more a design question rather than a Java/AWS question.
The intended design of AWS lambda is ... when your code has internal problems, it will return 500 (the ugly ones) while if the client is trying to reach something that's not there (400 or 300). This is true regardless of framework (same for Java Spring and Python Django etc.)
If your function is nicely detecting an error and returning a perfect object (json), then it is (by definition) not an internal problems of your code. In other words, your code is functioning as intended. As far as lambda is concerned, it is 200 ok.
If you want ugly response (5xx), you need to throw an error/exception. Else, lambda will think your code is fine and whatever error you are throwing inside your json is normal payload. It is not to say you should not encode your error in json response. It is perfectly normal to include application-specific/business-logic driven errors to pass in payload like this.
I hope this answer your question. In short,
there is no other way to have lambda return 5xx than throwing an error and it is intended behavior.
来源:https://stackoverflow.com/questions/45108530/is-there-any-way-to-signal-an-error-in-aws-lambda-for-java-without-throwing-an-e