Run RequestValidationError before middlewares #9582
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
app = FastAPI()
def middleware_example():
pass
async def request_input_validator(request: Request, exc):
return Exception("Bad Input")
app.add_pre_request_middleware(middleware_example)
app.add_exception_middlewares(RequestValidationError, request_input_validator)Descriptionwhen running this code the "middleware_example" function running before the "request_input_validator" i want to find an option to use RequestValidationError before the middleware The logic behind it : first check if is the body is according to the pydantic schema Operating SystemmacOS Operating System DetailsNo response FastAPI Versionlastest Python Version3.11 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
@edanbenavi , You cannot change the order of execution . Middleware runs first before exception handlers (including RequestValidationError). Even you define a custom router and a middleware, the control goes through the app like this :
Since you haven't mentioned a specific middleware, I don't know your use-case to suggest further. |
Beta Was this translation helpful? Give feedback.
@edanbenavi , You cannot change the order of execution . Middleware runs first before exception handlers (including RequestValidationError). Even you define a custom router and a middleware, the control goes through the app like this :
Since you haven't mentioned a specific middleware, I don't know your use-case to suggest further.