-
Notifications
You must be signed in to change notification settings - Fork 421
Feature request: app.exception_handler does not work when used in separate file #1193
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
Comments
hey @arunbhati thanks for sending over an archive to help reproduce it quickly! The issue is that To confirm it, I've added a dummy constant within lambda_function.py import src.abc.axis_controllers as axis_controllers
from src.abc.exceptions import HELLO # HERE!
def lambda_handler(event, context):
return axis_controllers.main(event, context)
event = {
"resource": "/proxy",
"path": "/proxy",
"httpMethod": "GET",
"headers": ""
}
print(HELLO)
lambda_handler(event, {}) exceptions.py from aws_lambda_powertools.event_handler import content_types
from aws_lambda_powertools.event_handler.api_gateway import Response
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
from src.abc import app
HELLO = "WORLD"
@app.exception_handler(ValueError)
def handle_value_error(ex: ValueError):
metadata = {"path": app.current_event.path}
print(f"Malformed request: {metadata}")
return Response(
status_code=400,
content_type=content_types.TEXT_PLAIN,
body="Invalid request",
)
@app.not_found
def handle_not_found_errors(exc: NotFoundError) -> Response:
# Return 418 upon 404 errors
print(f"Not found route: {app.current_event.path}")
return Response(
status_code=418,
content_type=content_types.TEXT_PLAIN,
body="I'm a teapot!"
) |
Hi @heitorlessa did you actually create this feature request? I'm having trouble finding it. Was it every implemented, if so? |
Hey @jasmarc yeah, sorry I didn't update this back (my bad) - it's already live: https://awslabs.github.io/aws-lambda-powertools-python/2.7.1/core/event_handler/api_gateway/#specialized-router-types |
Hi @heitorlessa, has this issue been solved with the mentioned PR: #1824? With newest version of powertools (2.25.0) I am not able to define exception handlers in a separate file with Router. Is there some other way to achieve it? |
Agree to SzymonSwic. I can confirm separate routes using ( I'm happy to contribute if I am assisted a little bit. Thanks!! |
oh hey, seeing this only now as it was closed -- That's something else, that would be a feature request to add @rajalokan if you've got the bandwidth, it'll need two changes (we need a feature request issue):
|
Any update on this feature @heitorlessa // @rajalokan ? |
Reopening this issue as using Sending a PR in few minutes. |
Thank you for commenting here and bringing this issue to our attention @alecrevangelista! |
|
This is now released under 2.36.0 version! |
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.
I don't have much expertise on how decorator works so any help here would be appreciated.
Thanks.
The text was updated successfully, but these errors were encountered: