RequestValidationError handler scope conflict when using APIRouter
0 reputation · 28 Dec 2022, 00:31 UTC
Goal: Apply a custom validation error response format only to certain APIRouter instances while keeping the default behavior for other routes.
Constraint/Uncertainty: FastAPI permits registering an exception handler for RequestValidationError on an APIRouter, but the documentation does not clarify whether a router‑level handler replaces, supplements, or is ignored when a global handler is also defined. This leaves the precedence and interaction between global and router‑level handlers unspecified.
Questions: Does a router‑level RequestValidationError handler override the global handler for routes under that router? If both handlers exist, which one takes precedence? Can per‑router validation error formatting be achieved without duplicating the handler code?