-
First check
Examplefrom fastapi import BackgroundTasks, FastAPI, status
from starlette.requests import Request
from starlette.responses import JSONResponse
class MyException(Exception):
...
app = FastAPI()
@app.exception_handler(MyException)
async def sampler_exception_handler(request: Request, exc: MyException):
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"error": "i hope someone sees this"},
)
def write_notification(email: str, message=""):
raise MyException("this is my exception")
with open("log.txt", mode="w") as email_file:
content = f"notification for {email}: {message}"
email_file.write(content)
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
background_tasks.add_task(write_notification, email, message="some notification")
return {"message": "Notification sent in the background"} DescriptionRaising an exception that an exception_handler is registered for, from a background task - causes an error from starlette, which also hides the original exception. running the code from the example, and triggering the endpoint, i expected the error to be either handled by the exception handler, or maybe just ignoring the exception handler as this exception does not happen as part of a request context. the actual behavior is as follows
I understand this might be connected to a starlette design decision from previous discussions, but haven't actually seen this exact cause for the problem. Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
This seems to be resolved by encode/starlette#1158. Will open a PR to update Starlette. |
Beta Was this translation helpful? Give feedback.
-
resolved by #4016 |
Beta Was this translation helpful? Give feedback.
-
@alonme I have the same problem of HTTP exceptions defined in the background task not been returned even with last version of Starlette. How did this solve the problem? |
Beta Was this translation helpful? Give feedback.
-
The thing is that background tasks are run after the response is sent. So exception handlers that would return a different response can't do anything there. Also, just ignoring an exception because the handled couldn't handle it wouldn't be appropriate, that would be a bug. If the handler can't really handle the exception it should raise another error so that you can at least see in the server error logs that something is wrong in the code. Also, even though the stack trace is not full, I would think it probably also included the original place of the exception, right? That would be the right thing to do, I would think. Otherwise, given that background tasks are run after the response is already sent, what would you expect/want to happen? |
Beta Was this translation helpful? Give feedback.
-
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
-
This is still a problem. Can we re-open the issue please ? |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. Any solutions? |
Beta Was this translation helpful? Give feedback.
-
But otherwise it doesn't interfere with the execution much, just the logs. It'd be nice if I can try/catch the thing to suppress the error stack, or at least make it less verbose. |
Beta Was this translation helpful? Give feedback.
resolved by #4016
can be closed