You can send exception feedback from a Lambda to an API Gateway API either by raising a Python exception and handling it via a mapping template, or return a specific set of values from the main handler function.
Returning from the Handler Function#
You can return a dictionary that includes at least:
- the desired status code
- the message body (JSONified)
- and whether or not the body is base64 encoded
return {
"statusCode": 404,
"body": simplejson.dumps(
f"User {username} not found"
),
"isBase64Encoded": False,
}
Raising an Exceptions#
Using a mapping template allows you to simply use regex to associate an error message from a plain Python exception with a certain HTTP code. I don’t have any examples because I haven’t done it, preferring the previous method.