Open andy-yu-y opened 3 years ago
Experiencing the same. This workaround seems to fix it, though it's clearly not ideal:
app = VersionedFastAPI(app, ...)
for sub_app in app.routes:
if hasattr(sub_app.app, "add_exception_handler"):
sub_app.app.add_exception_handler(CustomNotFoundError, custom_exception_handler)
I think it will be good to solve it in the coming version. Do you have any plan for it?
The solution from bhandanyan-nomad above solved the problem for me, but I adapted it to be a little more automatic. In his version, you need to explicitly list the exception handlers to add. This adaptation gets the list of existing exception handlers from the original and applies them all to the subapps. This way I can be lazy and not have to specifically keep track of my exception handlers.
handlers_to_apply = {}
for ex, func in app.exception_handlers.items():
handlers_to_apply[ex]=func
app = VersionedFastAPI(app, ...)
for sub_app in app.routes:
if hasattr(sub_app.app, "add_exception_handler"):
for ex, func in handlers_to_apply.items():
sub_app.app.add_exception_handler(ex, func)
I would also like to see this resolved in future versions...
+1 we are hitting this also
+1 for this issue. Could be related to #40
+1 I hope it will be good to solve it in the coming version.
add_exception_handler
@tijptjik rmerren
Can you show the preview of the folder structure.. I'm trying to implement the same however it's not allowing me. I'm currently having routes specified in different folders as compared to my main file.
@bhandanyan-nomad thanks so much for your solution, this just cost me 1.5 days of my life :D
+1 I am encountering the same issue.
+1 We are encountering the same issue as well.
Describe the bug When trying to define global exception handler, catching user defined exception fails with error "Caught handled exception, but response already started."
def custom_exception_handler( request: Request, exception: CustomNotFoundError ) -> JSONResponse: return JSONResponse( status_code=status.HTTP_404_NOT_FOUND, content={"message": f"Oops! {exception.name}"} )
exception_handlers = {CustomNotFoundError: custom_exception_handler}
app = FastAPI( title="Root app", version="0.1.0", exception_handlers=exception_handlers, ) app.include_router(api_router) app = VersionedFastAPI( app, version_format=f"{MAJOR_PLACEHOLDER}", prefix_format=f"/v{MAJOR_PLACEHOLDER}", default_version=(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION), exception_handlers=exception_handlers, )
To Reproduce Steps to reproduce the behavior:
Expected behavior Global exception handler should catch CustomNotFoundError exception and return HTTP_404_NOT_FOUND with message
Additional context kwargs, should be passed to Fastapi in the constructor of VersionedFastAPI like below: versioned_app = FastAPI( title=app.title, description=app.description, version=semver, openapi_prefix=prefix, kwargs, )