Cannot add a custom handler for HTTPExceptions with status_code=500 #2499
-
I found this through FastAPI but the code causing the problem appears to be in Starlette.
This exception handler never gets called. When I dug into it I found the problem was this:
in this file https://github.com/encode/starlette/blob/master/starlette/applications.py I don't understand why it is doing this:
because if I change it to this:
Everything works as expected and my error handler handles 500 errors the same as any other status code. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Same for me. Looks strange because it prevents middle middlewares (a.k.a user middlewares) to work properly on server errors. CORS middleware for example doesn't send CORS headers |
Beta Was this translation helpful? Give feedback.
-
This is definitely an issue. |
Beta Was this translation helpful? Give feedback.
-
Apparently, this is by design. ServerErrorMiddleware (for 500 errors) and ExceptionMiddleware (for everything else) are separate so that the middleware stack can ben in this order ServerErrorMiddleware I.E. Server errors get handled before any custom middleware runs but other HTTP exceptions get handled after any custom middleware runs, which makes sense. |
Beta Was this translation helpful? Give feedback.
Apparently, this is by design. ServerErrorMiddleware (for 500 errors) and ExceptionMiddleware (for everything else) are separate so that the middleware stack can ben in this order
ServerErrorMiddleware
CustomMiddleware1
CustomMiddleware2
ExceptionMiddleware
I.E. Server errors get handled before any custom middleware runs but other HTTP exceptions get handled after any custom middleware runs, which makes sense.
https://www.starlette.io/middleware/#using-middleware