I have a problem with my Exception handler in my FastAPI app. Here's my current code:
@app.exception_handler(Exception)
async def server_error(request: Request, error: Exception):
logger.error(f"Internal server error: {error} \n{traceback.format_exc()}")
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=jsonable_encoder({
"message": "...",
"exception": [str(error)],
"endpoint": request.url
})
)
I would like to add a logging message that prints the stacktrace of the exception that triggered this function. Currently, the output doesn't point to the stacktrace of the exception.
I've tried many ways to fix this, but the only result I ended up with was creating a pull request into the FastAPI repository.
If I missed something, please let me know. Thank you!