-
Notifications
You must be signed in to change notification settings - Fork 365
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: Event handler middleware not triggered for not found routes #3916
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hi @tomsimpkins! Thanks for raising this issue and identifying this bug. We should process the global middlewares even when invoking Do you want to submit a PR to fix this? We would love to have your contribution here. Please let us know. Thanks. |
Hi @leandrodamascena thanks for getting back. I can't dive into this immediately but I've done some reading and think I know how this might be solved. Please let me know if you agree/disagree. I'd propose modelling the not found handler itself as middleware. So in the resolver we would have: def _not_found(self, method: str):
def not_found_middleware(app, next_middleware):
# call handler for not found here and return Response
middleware_stack = not_found_middleware
for handler in reversed(self._router_middlewares + [_registered_api_adapter]):
middleware_stack = MiddlewareFrame(current_middleware=handler, next_middleware=middleware_stack)
return ResponseBuilder(middleware_stack(self)) The same issue seems to apply with the exception handlers. I think we could provide the exception handler as a middleware to the Route as the last element of the response=self._to_response(
route(router_middlewares=self._router_middlewares + [exception_handler_as_middleware], app=self, route_arguments=route_arguments),
), There's definitely benefit to reusing the logic from Route, so I prefer the answer for the exception handlers to the not found route because of the reuse. |
resurfacing to @leandrodamascena for later this week. I'm gonna tackle as many other issues as possible today. |
Expected Behaviour
I think I would expect (global) middleware to be triggered when a not-found route is used. I have middleware that implements desirable behaviour even when the route is not found.
Current Behaviour
Currently global middleware seems to be bypassed when a not found route is hit.
Code snippet
Possible Solution
The solution I've used currently is to use catch all route handlers to sweep up any unexpected routes, which looks to app like a "valid" route. This means middleware is triggered.
Another solution would be to trigger global middleware before route resolution.
Steps to Reproduce
I think I've supplied enough context. The above code snippet should run under
pytest -rP
Please let me know if more is needed.Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.11
Packaging format used
PyPi
Debugging logs
No response
The text was updated successfully, but these errors were encountered: