How to make Fastapi put the stacktrace in the body when handling 500 error #7519
-
First check
DescriptionDuring the development cycle, especially when running on a remote machine (QA environment etc.) it's pretty convenient to have the traceback directly in the body of a 500 response so that one does not have to connect to the machine to get the traceback. Of course it's something that one should opt-in , as you don't want to have that in production |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 1 reply
-
|
You could start with https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers with a call to a function in |
Beta Was this translation helpful? Give feedback.
-
|
ah yeah right , stupid me, thanks for the pointer, I will try to come up with something and put it here in case somebody wonders the same thing |
Beta Was this translation helpful? Give feedback.
-
|
ok I've hacked this and it seems to work pretty well if DISPLAY_TRACEBACK_ON_500:
@app.exception_handler(Exception)
async def debug_exception_handler(request: Request, exc: Exception):
import traceback
return Response(
content="".join(
traceback.format_exception(
etype=type(exc), value=exc, tb=exc.__traceback__
)
)
)
|
Beta Was this translation helpful? Give feedback.
-
|
You may want to close the issue if it works for you :) |
Beta Was this translation helpful? Give feedback.
-
|
haha sure |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @phy25 ! 🍰 🙇 Thanks @allan-simon for coming back to close it 👍 |
Beta Was this translation helpful? Give feedback.
-
|
Starlette has a built-in server exception middleware - use |
Beta Was this translation helpful? Give feedback.
-
The fact that you can set debug=true is mentioned in several places, but not where to or how to. I finally saw your reply after 2hrs of googling. Would be great to get this into the fastapi docs. |
Beta Was this translation helpful? Give feedback.
-
|
I can get FastAPI to print stack trace with |
Beta Was this translation helpful? Give feedback.
-
|
Surprisingly (to me) its late 2021 and the starlette debug traceback response whilst pretty, still does not show the local vars - as other frameworks do (eg. django).. and this was essential to me migrating some existing projects to fastApi. My crude workaround is as follows; |
Beta Was this translation helpful? Give feedback.
-
|
What are the pros/cons of this approach: import logging
from typing import Callable
from fastapi import FastAPI, Response
from fastapi.exceptions import HTTPException, RequestValidationError
from fastapi.routing import APIRoute
from starlette.requests import Request
class ServerErrorLoggingRoute(APIRoute):
'''Log uncaught exceptions.'''
def get_route_handler(self) -> Callable:
original_route_handler = super().get_route_handler()
async def custom_route_handler(request: Request) -> Response:
try:
return await original_route_handler(request)
# These exceptions are used for control flow rather to indicate a
# server error so we don't log these.
except RequestValidationError:
raise
except HTTPException:
raise
except Exception:
logging.exception('Uncaught exception')
raise
return custom_route_handler
app = FastAPI()
app.router.route_class = ServerErrorLoggingRoute |
Beta Was this translation helpful? Give feedback.
ok I've hacked this and it seems to work pretty well