Skip to content

Bug: APIGatewayRestResolver exception_handler decorator is not stackable #1706

Closed

Description

@Ozz-Modding

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

Activity

  1. added
    bug
    triage
    on Nov 10, 2022
  2. boring-cyborg commented on Nov 10, 2022

    @boring-cyborg

    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

  3. Ozz-Modding commented on Nov 10, 2022

    @Ozz-Modding
    ContributorAuthor

    If happy I can raise a PR

  4. heitorlessa commented on Nov 10, 2022

    @heitorlessa
    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!

  5. Ozz-Modding commented on Nov 10, 2022

    @Ozz-Modding
    ContributorAuthor

    I have raised this now: #1707

  6. github-actions commented on Nov 14, 2022

    @github-actions
    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.

  7. added
    pending-release
    on Nov 14, 2022
  8. github-actions commented on Nov 17, 2022

    @github-actions
    Contributor

    This is now released under 2.3.0 version!

  9. removed
    pending-release
    on Nov 17, 2022
  10. removed
    triage
    on Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions