Replies: 3 comments 4 replies
-
|
As suggested in #7791, a possible workaround is to add something like: @app.exception_handler(StarletteHTTPException)
async def _(request: Request, exc: StarletteHTTPException):
if exc.headers is None:
exc.headers = dict()
if (
request_correlation_id := request.headers.get("Request-Correlation-Id")
) is not None:
exc.headers["Request-Correlation-Id"] = request_correlation_id
return await http_exception_handler(request, exc)However, this means duplicating the logic for updating the temporal response, which seems awkward and may be challenging where this is done on a per-path-operation basis. |
Beta Was this translation helpful? Give feedback.
-
|
I believe a better workaround would be to register the custom exception handler in from fastapi import HTTPException
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(status_code=exc.status_code, content=jsonable_encoder(exc.detail))
async def internal_exception_handler(request: Request, exc: Exception):
# Some custom error handling
pass
app.add_exception_handler(HTTPException, http_exception_handler)
app.add_exception_handler(500, internal_exception_handler)In which the |
Beta Was this translation helpful? Give feedback.
-
|
You should probably be using a middleware for this, not relying on the response object. The current behavior seems fine to me. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Making a GET request to
/successcorrectly propagates theRequest-Correlation-Idheader to the response, but on/failurethe header is not propagated. This happens consistently in Python 3.8-3.13 as demonstrated in https://github.com/textbook/fastapi-bug.Operating System
macOS
Operating System Details
Using macOS locally, but replicated in Ubuntu in GitHub Actions and originally noticed on a Windows machine.
FastAPI Version
0.115.6
Pydantic Version
2.10.4
Python Version
3.8.20
Additional Context
Although I ticked the box:
I'm probably not going to do that. I'm already active on Stack Overflow, and reporting a bug not asking a question as such.
Beta Was this translation helpful? Give feedback.
All reactions