Skip to content

Feature request: app.exception_handler does not work when used in separate file #1193

Closed
@heitorlessa

Description

@heitorlessa

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.

  1. Create a lambda function in Python with Lambda Layer for AWS Powertools from here.
  2. Attach this Zip file to lambda function - Zip file: Archive.zip
  3. Use following input for Lambda request
{
  "resource": "/proxy",
  "path": "/proxy",
  "httpMethod": "GET",
  "headers": null
}
  1. 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",
  1. Now move ValueError exception method from exceptions.py into init.py file in same folder.
  2. 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.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions