Closed
Description
Discussed in #1191
Originally posted by arunbhati April 29, 2022
Hi,
I am using APIGatewayRestResolver and I'm having trouble working with @app.exception_handler. When I have APIGatewayRestResolver object and @app.exception_handler decorator in same file then it works but if I create a separate file for exceptions(exceptions.py) and do the import of APIGatewayRestResolver then it doesn't work.
Following are steps to reproduce.
- Create a lambda function in Python with Lambda Layer for AWS Powertools from here.
- Attach this Zip file to lambda function - Zip file: Archive.zip
- Use following input for Lambda request
{
"resource": "/proxy",
"path": "/proxy",
"httpMethod": "GET",
"headers": null
}
- Lambda function should fail with following error
{
"errorMessage": "",
"errorType": "ValueError",
"requestId": "24f06853-16fc-4657-9360-40409f0726be",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n return axis_controllers.main(event, context)\n",
" File \"/var/task/src/abc/axis_controllers.py\", line 11, in main\n return app.resolve(event, context)\n",
" File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 498, in resolve\n return self._resolve().build(self.current_event, self._cors)\n",
- Now move ValueError exception method from exceptions.py into init.py file in same folder.
- Rerun test with same input and now lambda output will show expected result(shown below).
{
"statusCode": 400,
"headers": {
"Content-Type": "text/plain"
},
"body": "Invalid request",
"isBase64Encoded": false
}
I don't have much expertise on how decorator works so any help here would be appreciated.
Thanks.