question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

About behavior HTTPException of sub-applications

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI, Depends, HTTPException
from fastapi.responses import JSONResponse
from starlette.middleware.cors import CORSMiddleware


app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
    expose_headers=['*'],
)

sub = FastAPI()


@sub.get('/', status_code=200)
async def get():
    raise HTTPException(400, 'error handling sub app')


async def http_exception_handler(_, exc):
    return JSONResponse(exc.detail, status_code=exc.status_code)

# not working
app.add_exception_handler(HTTPException, http_exception_handler)

# working
# sub.add_exception_handler(HTTPException, http_exception_handler)

app.mount('/sub', sub)


@app.get('/healthcheck', status_code=200)
async def healthcheck():
    return 'health check'

Description

I expect response of GET /sub endpoint is “error handling sub app”.

But I got {detail: “error handling sub app”}.

The cause of this, http exception handler attached to root app will not handling sub-app exceptions.

Actually, http exception handler attached to sub app worked fine.

Is this intended behavior?

Operating System

macOS

Operating System Details

MacOS Monterey v 12.3.1

FastAPI Version

0.80.0

Python Version

Python 3.10.4

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
iudeencommented, Aug 26, 2022

I think error handling behavior should be same range as middleware, what do you think?

I’m not sure if this is the right justification. I think middleware sits between your apps and the server, while your exception handlers are within your app’s scope.

0reactions
yoshiya0503commented, Aug 26, 2022

Sure, I’m not particularly troubled at this behavior, If that is intended, It’s ok.

Read more comments on GitHub >

github_iconTop Results From Across the Web

About behavior HTTPException of sub-applications - PullAnswer
I expect response of GET /sub endpoint is "error handling sub app". But I got {detail: "error handling sub app"}. The cause of...
Read more >
Propagating dependency overrides to mounted sub-applications
What I'm aiming at is not exporting sub-applications from production code ... The only thing I can think of is to override this...
Read more >
Error accessing Session from URL rewritten pages - MSDN - Microsoft
This behavior is really strange and it's hampering my ability to create a flexible URL rewriting scheme. Any ideas on how to overcome...
Read more >
tiangolo/fastapi - Gitter
1: raise HTTPException(status_code=422, detail='Archive must contain only one single JSON file') try: ... Fix typo in docs for sub-applications.
Read more >
Applications and Routes — Clastic 20.0 documentation
routes (list) – A list of Route instances, SubApplications, or tuples. ... debug (bool) – Set to True to enable certain debug behavior...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found