Description
Expected Behaviour
I would expect to be able to decorate an exception handler multiple times to reuse handler code.
@app.exception_handler(SchemaValidationError)
@app.exception_handler(BadRequestError)
def handle_it(ex):
pass
Current Behaviour
The first decorator (immediately above the function header) is handled correctly, however subsequent decorators are assigned None
in the map and are therefore not handled. In the example above, a SchemaValidationError
would result in a 502 Internal Server error response to the user.
Code snippet
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.exceptions import BadRequestError
from aws_lambda_powertools.utilities.validation import SchemaValidationError
app = APIGatewayRestResolver()
@app.exception_handler(SchemaValidationError)
@app.exception_handler(BadRequestError)
def something():
pass
Possible Solution
Return the function in the inner function:
def exception_handler(self, exc_class: Type[Exception]):
def register_exception_handler(func: Callable):
self._exception_handlers[exc_class] = func
return func
return register_exception_handler
Steps to Reproduce
Add some logging to powertools for illustration:
def exception_handler(self, exc_class: Type[Exception]):
def register_exception_handler(func: Callable):
self._exception_handlers[exc_class] = func
print(self._exception_handlers)
Run the code snippet above. Note the output e.g.
{<class 'aws_lambda_powertools.event_handler.exceptions.BadRequestError'>: <function something at 0x108813310>, <class 'aws_lambda_powertools.utilities.validation.exceptions.SchemaValidationError'>: None}
AWS Lambda Powertools for Python version
37
AWS Lambda function runtime
3.9
Packaging format used
Lambda Layers
Debugging logs
No response