-
Notifications
You must be signed in to change notification settings - Fork 402
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
Bug: exception_handler decorator won't return Response with botocore ClientError #1153
Comments
Definitely a bug in the order of response, as the exception handler was added later and we seem to not be handling the exception type like we do earlier in the call chain. Thanks for spotting another one @sthuber90! |
Happy to support with a PR unless anybody is already working on it |
Please do @sthuber90 i can review and merge it tomorrow morning. |
Actually it seems that any exception raised within an exception handler route will raise and cause the Lambda function to end with an error. I guess you could say that it's intentional however it doesn't seem intuitive at first. @heitorlessa should As @michaelbrewer aparrently implemented that part, I'm interested in his take on this. Please see the following commit for a proposal from my side: 6aa7c00 I can create a PR later but have to drop off for now |
Ok i will have a look. Sorry if i screwed up here. I will have a deeper look. |
Yes to the first question - we should handle ServiceError exceptions
differently as customers will instinctively and eventually raise these to
signal 4xx
…On Tue, 26 Apr 2022 at 19:11, Stephan ***@***.***> wrote:
Actually it seems that any exception raised within an exception handler
route will raise and cause the Lambda function to end with an error. I
guess you could say that it's intentional however it doesn't seem intuitive
at first.
@heitorlessa <https://github.com/heitorlessa> should ServiceError type
exceptions (BadRequestError, InternalServerError, NotFoundError,
UnauthorizedError) be handled differently when raised in an exception
handler than other exceptions? Alternatively, we leave it as is and extend
the documentation to say that exceptions raised in exception handlers will
cause a Lambda function to error.
As @michaelbrewer <https://github.com/michaelbrewer> aparrently
implemented that part, I'm interested in his take on this.
Please see the following commit for a proposal from my side: 6aa7c00
<6aa7c00>
I can create a PR later but have to drop off for now
—
Reply to this email directly, view it on GitHub
<#1153 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBD5SB6R46VP6RAFADTVHAPUNANCNFSM5UJH6PKQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@heitorlessa @sthuber90 - the intention in my mind for Maybe the docs should indicate this and the typing also. But i can see the case for raising your preferred service error. |
In the diff, you do lose the original Another possible solution diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py
index 5d57c645..f4731dd4 100644
--- a/aws_lambda_powertools/event_handler/api_gateway.py
+++ b/aws_lambda_powertools/event_handler/api_gateway.py
@@ -651,7 +651,10 @@ class ApiGatewayResolver(BaseRouter):
def _call_exception_handler(self, exp: Exception, route: Route) -> Optional[ResponseBuilder]:
handler = self._lookup_exception_handler(type(exp))
if handler:
- return ResponseBuilder(handler(exp), route)
+ try:
+ return ResponseBuilder(handler(exp), route)
+ except ServiceError as service_error:
+ exp = service_error
if isinstance(exp, ServiceError):
return ResponseBuilder(
diff --git a/tests/functional/event_handler/test_api_gateway.py b/tests/functional/event_handler/test_api_gateway.py
index 9b5835c8..c0bce92d 100644
--- a/tests/functional/event_handler/test_api_gateway.py
+++ b/tests/functional/event_handler/test_api_gateway.py
@@ -1210,3 +1210,27 @@ def test_exception_handler_not_found_alt():
# THEN call the @app.not_found() function
assert result["statusCode"] == 404
+
+
+def test_exception_handler_raises_service_error(json_dump):
+ # SCENARIO: Support an exception_handler that raises a ServiceError
+ # GIVEN
+ app = ApiGatewayResolver()
+
+ @app.exception_handler(ValueError)
+ def client_error(ex: ValueError):
+ raise BadRequestError("Bad request")
+
+ @app.get("/my/path")
+ def get_lambda() -> Response:
+ raise ValueError("foo")
+
+ # WHEN calling the event handler
+ # AND a ValueError is raised
+ result = app(LOAD_GW_EVENT, {})
+
+ # THEN call the exception_handler
+ assert result["statusCode"] == 400
+ assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
+ expected = {"statusCode": 400, "message": "Bad request"}
+ assert result["body"] == json_dump(expected) |
My PR is just for comment (and if fine review), docs make this clear that nothing should be raised or we support |
I would disagree that |
Just that your |
Thanks both - made a tiny change and merged. Should be in tomorrow's release |
This is now released under 1.25.10 version! |
Expected Behaviour
On botocore ClientError the Lambda function shall return:
Current Behaviour
Lambda function raises exception and ends with error, instead of returning HTTP response
Code snippet
Possible Solution
No response
Steps to Reproduce
Use the provided code snippet to see that the Lambda will throw an exception instead of returning a 400 "Bad Request" response.
AWS Lambda Powertools for Python version
latest (Layer version 18)
AWS Lambda function runtime
3.9
Packaging format used
Lambda Layers
Debugging logs
The text was updated successfully, but these errors were encountered: