About behavior HTTPException of sub-applications
See original GitHub issueFirst 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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Sure, I’m not particularly troubled at this behavior, If that is intended, It’s ok.