Skip to content
Discussion options

You must be logged in to vote

From Starlette's docs: https://www.starlette.io/exceptions/#httpexception

You should only raise HTTPException inside routing or endpoints. Middleware classes should instead just return appropriate responses directly.

Also: https://www.starlette.io/exceptions/#errors-and-handled-exceptions

... the middleware stack of a Starlette application is configured like this:

  • ServerErrorMiddleware - Returns 500 responses when server errors occur.
  • Installed middleware
  • ExceptionMiddleware - Deals with handled exceptions, and returns responses.
  • Router
  • Endpoints

So, exception handlers will not work for HTTPExceptions raised by middlewares, because they are raised outside of the ExceptionMiddleware

Replies: 5 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@ArtyomVancyan
Comment options

Comment options

You must be logged in to vote
5 replies
@ArtyomVancyan
Comment options

@thuyng-ing
Comment options

@thuyng-ing
Comment options

@thuyng-ing
Comment options

@thuyng-ing
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@jovana
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
6 participants