DurandA - I believe you are absolutely correct: the simplified Lambda Proxy Integration approach relies on you catching your exceptions and returning the standardized format:
def lambda_handler(event, context):
return {
'statusCode': 400,
'body': json.dumps('This is a bad request!')
}
The simplified Lambda Proxy Integration feature was announced in a September 2016 blog post, but one of the examples you cited was posted earlier, in a June 2016 blog post, back when the more complicated Integration Response method was the only way. Maybe you have stumbled on an out of date example.
You also posted a link to the product documentation for error handling, at the top in the section covering the Lambda proxy integration feature, it says:
With the Lambda proxy integration, Lambda is required to return an
output of the following format:
{
"isBase64Encoded" : "boolean",
"statusCode": "number",
"headers": { ... },
"body": "JSON string"
}
Here is a working example that returns a HTTP 400 with message "This is an exception!" using Lambda Proxy Integration.
import json
def exception_handler(e):
# exception to status code mapping goes here...
status_code = 400
return {
'statusCode': status_code,
'body': json.dumps(str(e))
}
def lambda_handler(event, context):
try:
raise Exception('This is an exception!')
return {
'statusCode': 200,
'body': json.dumps('This is a good request!')
}
except Exception as e:
return exception_handler(e)
Output from the above:
$ http https://**********.execute-api.us-east-2.amazonaws.com/test
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Length: 23
Content-Type: application/json
Date: Sun, 23 Feb 2020 05:06:59 GMT
X-Amzn-Trace-Id: Root=1-********-************************;Sampled=0
x-amz-apigw-id: ****************
x-amzn-RequestId: ********-****-****-****-************
"This is an exception!"
I understand your frustration that you do not want to build a custom exception handler. Fortunately, you only have to build a single handler wrapping your lambda_handler function. Wishing you all the best!