-
-
Notifications
You must be signed in to change notification settings - Fork 87
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
May need better integration with ASGI/WSGI servers #16
Comments
uvicorn is installing a bunch of loggers, which are handling exceptions. As such, pretty_errors is never getting to see the stack trace - its replacement of A simple albeit limited fix is to use an older version of pretty_errors: in its original form, pretty_errors did not hook into the exception system (as it correctly now does) - instead, it replaced pip install pretty_errors==1.0.10 |
Thanks for the quick reply! I'll stick with it and notify uvicorn developers about the situation Question: I read in here |
No, it's definitely sending to stderr: you can check by redirecting in the console. It's doesn't work because uvicorn is catching I've re-implemented the old way of doing it to handle this situation: update to 1.2.9 and you can call pretty_errors.replace_stderr() It should then work (though with slightly less functionality, as it no longer has access to the stack trace). Let me know how it goes! |
Ah! Did you patch it already? 😮 the |
Hi. I think the better solution exists for fastapi (and most of the other web frameworks): we can implement custom middleware or exception handler on the layer of the framework. In that case, the uvicorn will not receive an exception at all and we can freely print it with pretty_errors. See example for fastapi: import sys
import fastapi
import pydantic
class Settings(pydantic.BaseSettings):
use_pretty_errors: bool = False
settings = Settings()
app = fastapi.FastAPI()
@app.get('/test')
async def slices_list():
y = 1
x = 0
return {'result': y / x}
class PrettyErrors:
__slots__ = ('initialized',)
def __init__(self):
self.initialized = False
def initialize(self):
import pretty_errors
pretty_errors.configure(display_locals=True)
self.initialized = True
def print_exception(self):
import pretty_errors
if not self.initialized:
self.initialize()
pretty_errors.excepthook(*sys.exc_info())
pretty_errors_handler = PrettyErrors()
@app.middleware("http")
async def exception_handler(request: fastapi.Request, call_next):
try:
return await call_next(request)
except Exception:
if settings.use_pretty_errors:
pretty_errors_handler.print_exception()
else:
logging.exception('Unexpected error occured.')
return fastapi.responses.JSONResponse(content={'message': 'Unexpected error occured. We already fixin it.'},
status_code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR) |
I should have posted some appreciation for this instead of just linking from the readme: thanks @Tiendil! |
Note this does only work in the app i.e if you have some errors outside the app e.g import errors, database-errors on startup etc. then those will not be caugt. I'm actually having that issue atm, that I want to catch errors/warnings outside the app, but that seems rather impossible |
That’s exactly what I wanted, so I developed a package called fastapi-pretty-errors for FastAPI to make integration easier. |
Hello there,
I'd like to use pretty errors in my web development, but I have some issues.
I use the fastapi framework, which runs multi processes through the ASGI uvicorn server. Doesn't matter what I do (import, setup config, usercustomize.py etc.) errors do not get pretty in this context. They do when I run my normal scripts.
Can I ask for help/directions?
The text was updated successfully, but these errors were encountered: