question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: APIGatewayRestResolver exception_handler decorator is not stackable

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sprkemcommented, Nov 10, 2022
0reactions
github-actions[bot]commented, Nov 17, 2022

This is now released under 2.3.0 version!

Read more comments on GitHub >

github_iconTop Results From Across the Web

app.exception_handler does not work when used in separate ...
When I have APIGatewayRestResolver object and @app.exception_handler decorator in same file then it works but if I create a separate file ...
Read more >
exception handling in decorator function - python
It seems to call my function and return correctly, but the exceptions are never caught within my retry function. So I can't handle...
Read more >
Error Handling in Python using Decorators - GeeksforGeeks
Error handling using decorators. The following example shows how a general error handling code without use of any decorator looks like:.
Read more >
Guide to Spring Boot REST API Error Handling - Toptal
In this article, we cover how to implement proper Spring Boot exception handling when building a REST API . Person confused about a...
Read more >
Handling Application Errors — Flask Documentation (2.2.x)
Error handlers still respect the exception class hierarchy. If you register handlers for both HTTPException and Exception , the Exception handler will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found