API Gateway + Lambda failed to handle exception
API Gateway + Lambda failed to handle exception
I have Python based Lambda which is integrated with API Gateway and try to handle lambda exception below.
{
"stackTrace": [
[
"/var/task/index.py",
14,
"handler",
"raise Exception(err)"
]
],
"errorType": "Exception",
"errorMessage": "device name existed"
}
I've defined HTTP status 400 of Method Response for the exception.
I also defined "Lambda Error Regex" and "Body Mapping Templates".
But the API Gateway still responses HTTP status 200 instead of 400 when my lambda raised an exception.
Anyone has idea what's going wrong. Did I miss something?
1 Answer
1
I found the solution by myself.
Here is my Lambda code:
raise Exception({
"errorType" : "Exception",
"httpStatus": 400,
"message": err
})
API Gateway will get the error like
{
"stackTrace": [["/var/task/index.py", 17, "handler", ""message": err"]],
"errorType": "Exception",
"errorMessage": "{'message': 'device not found', 'errorType': 'Exception', 'httpStatus': 400}"
}
The reason why my lambda error regex didn't work is that the error message is within single quotes instead of double quotes.
After I changed the lambda error regex like below, everything works fine
.*'errorType': 'Exception'.*
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment