Exceptions raised in middleware handled by differently #8028
Replies: 6 comments
-
The reason for this seems to be that the Your issues all seem to have their source in functionality inherited directly from starlette, so this should probably be brought up as an issue in starlette. (I suspect that if you were to bring this up in starlette, the response would be that middlewares should be responsible for handling their own exceptions and returning some sort of Separately, if you don't like this, you could just override the |
Beta Was this translation helpful? Give feedback.
-
That is definitely not the case with their But they certainly do handle their own errors, esp in the middleware mentioned above. We can start with our own base middleware which handles error responses like exception handlers - but it does suck that you've gotta handle it in two different places to get consistent output. Will close this one. The previous issue #457 is even more annoying, because we're just trying to use a standard CORSHandler with standard exception handler - now we've gotta intervene to fix it. And I can't find a good reason why anyone using FastAPI and/or starlette to want that behaviour.. Will leave that one open. |
Beta Was this translation helpful? Give feedback.
-
To clarify, the reason I said middleware errors should be 500s above was that any expected errors should be handled directly in the middleware, and that errors initiating from a given middleware without being gracefully handled inside the middleware would presumably be implementation mistakes (worthy of a 500 response). I think you made some good points here, and, for example, currently I don't think there is an excellent pattern for auth-in-middleware (for the reasons you described). It seems to me like there are (at least) three possible solutions to the auth-in-middleware problem that could result in usability improvements for FastAPI:
It might be worth thinking about which, if any, of the above seems most useful to you, and opening an issue with a feature request in fastapi/starlette as appropriate. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help here as always @dmontagu ! 🍰🚀 @hongymagic let me ask you to be less pejorative against the project and its community. We are putting our free time here to help you. It seems you're expecting some things to behave in a predefined way with a previous and subjective concept of what is a "standard" middleware design and behavior. If you use the middlewares from Starlette for auth, they will behave in the way it's designed in Starlette. If you "turn FastAPI's basic authentication into middleware instead of it being per-route operation" you're not using FastAPI's basic auth ideas, you're creating a new middleware that works the way it does in Starlette. If you want to declare those things in a single place, your might want to use a dependency in a |
Beta Was this translation helpful? Give feedback.
-
Apologies, definitely did not mean to be that way, and I do appreciate the time spend answering questions. You are right my understanding of middleware when I read the docs (and as I've cited before) come from other places and make same assumptions about Starlette/FastAPI - when it shouldn't. Thanks for your time, and appreciate the work you guys are doing on FastAPI! |
Beta Was this translation helpful? Give feedback.
-
Hello, i have similar problem:
as result, i have error 500, instead 400 in global error handler, because, middleware context manager, raise error again |
Beta Was this translation helpful? Give feedback.
-
Description
It seems that various of middlewares behave differently when it comes to exception handling:
So for example, if you were to turn FastAPI's basic authentication into middleware instead of it being per-route operation, none of the errors thrown in
HTTPBasic.__call__
are respected and just return 500 internal server error.In frameworks like Express.js or Koa, you can either:
I guess the first one is the more relevant approach here.
I've not looked too deep in to the code base, but the behaviours seems a bit odd at first.
Beta Was this translation helpful? Give feedback.
All reactions