fastapi-versioning icon indicating copy to clipboard operation
fastapi-versioning copied to clipboard

Bug for exception handler

Open andy-yu-y opened this issue 4 years ago • 10 comments

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:

  1. initialize app using VersionedFastAPI
  2. make a request to service that raise user defined exception CustomNotFoundError
  3. returns a Http status 500 with error "Caught handled exception, but response already started."

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, )

andy-yu-y avatar Jan 08 '21 08:01 andy-yu-y

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)

bhandanyan-nomad avatar May 25 '21 15:05 bhandanyan-nomad

I think it will be good to solve it in the coming version. Do you have any plan for it?

andy-yu-y avatar May 26 '21 07:05 andy-yu-y

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...

rmerren avatar Jul 15 '21 17:07 rmerren

+1 we are hitting this also

thrix avatar Sep 10 '21 12:09 thrix

+1 for this issue. Could be related to #40

avinashjoshi avatar Dec 10 '21 02:12 avinashjoshi

+1 I hope it will be good to solve it in the coming version.

ysnghr avatar Feb 26 '22 14:02 ysnghr

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.

cabudies avatar Mar 08 '22 10:03 cabudies

@bhandanyan-nomad thanks so much for your solution, this just cost me 1.5 days of my life :D

Butterweck avatar Sep 28 '22 15:09 Butterweck

+1 I am encountering the same issue.

JenySadadia avatar Feb 03 '23 08:02 JenySadadia

+1 We are encountering the same issue as well.

vdymna avatar Apr 13 '23 15:04 vdymna