-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to make Fastapi put the stacktrace in the body when handling 500 error #1241
Comments
You could start with https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers with a call to a function in |
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 |
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__
)
)
)
|
You may want to close the issue if it works for you :) |
haha sure |
Thanks for the help here @phy25 ! 🍰 🙇 Thanks @allan-simon for coming back to close it 👍 |
Starlette has a built-in server exception middleware - use |
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. |
I can get FastAPI to print stack trace with |
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;
|
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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First check
Description
During 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
The text was updated successfully, but these errors were encountered: