Middleware error handling inconsistencies #3031
Unanswered
MorganGellert
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. I was writing an ASGI middleware and noticed some odd error handling behavior if an exception is raised in the
receive
callback.With no other middleware lower on the stack (later in the list of middleware), HTTPExceptions are handled as expected and land on exception handlers as expected. (In code below set
trigger
toExceptionTrigger.HTTP_EXCEPTION
orExceptionTrigger.HTTP_EXCEPTION_ALT
.)However if the middleware lower on the stack calls
request.body()
it will raise the HTTPException in the callback and it will not be caught by the exception handler stack resulting in a 500 response from the server. (In code below settrigger
toExceptionTrigger.UNCAUGHT
.)Strangest of all, if there is a
BaseHTTPMiddleware
lower on the stack that does not callrequest.body()
then theExceptionGroup
is not parsed apart properly but anExceptionGroup
can be caught by the exception handler stack. (In code below settrigger
toExceptionTrigger.EXCEPTION_GROUP
.)I tried to do a thorough search of the Discussions for middleware exception issues and didn't find these exact problems. This broke my brain for a while so let me know if I can answer any questions!
(Using python 3.13.0 and starlette 0.48.0.)
Thanks so much!
Beta Was this translation helpful? Give feedback.
All reactions