Skip to content

Bug: APIGatewayRestResolver exception_handler decorator is not stackable #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sprkem opened this issue Nov 10, 2022 · 6 comments · Fixed by #1707
Closed

Bug: APIGatewayRestResolver exception_handler decorator is not stackable #1706

sprkem opened this issue Nov 10, 2022 · 6 comments · Fixed by #1707
Labels
bug Something isn't working

Comments

@sprkem
Copy link
Contributor

sprkem commented Nov 10, 2022

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

@sprkem sprkem added bug Something isn't working triage Pending triage from maintainers labels Nov 10, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Nov 10, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

@sprkem
Copy link
Contributor Author

sprkem commented Nov 10, 2022

If happy I can raise a PR

@heitorlessa
Copy link
Contributor

Hey @sprkem, let's extend the signature to accept one or more exceptions (Exception | List[Exception]) instead.

Stacking will impact performance with the increase of stack frames. We should support it to prevent errors, but the intention we need to address is to handle distinct yet related exceptions with a single exception handler.

Yes, please for the PR!! Ping if you need any help

Thank you!

@sprkem
Copy link
Contributor Author

sprkem commented Nov 10, 2022

I have raised this now: #1707

@github-actions
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Nov 14, 2022
@github-actions
Copy link
Contributor

This is now released under 2.3.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Nov 17, 2022
@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants