-
Notifications
You must be signed in to change notification settings - Fork 511
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
Sentry won't work properly with typer #1604
Comments
Hello @birdhackor ! Thanks for bringing this to our attention! We have not yet tried typer, but we are big fans of @tiangolo who is also doing FastAPI :-) As typer is probably doing some error handling on its own, we probably have to create a typer integration to make it work properly. Anyone who reads this: If you want to see a typer integration, please thumb-up this issue! |
I have also created a discussion for this topic: #1612 |
For those who need a quick solution - Option 1: Option 2: def callback():
sentry_sdk.init(...)
app = typer.Typer(callback=callback) |
...until sentry has an integration: getsentry/sentry-python#1604 (comment)
@danielbraun89 do you mind posting what version of python, sentry-sdk and typer you're using? I've tried both methods you suggested and neither produce Sentry errors for me. /edit I got it working by combining both options in typer 0.9. I assume you meant to say those two options each should have done the trick on their own? |
Hi, I believe this issue occurs when an unhandled exception is raised. If you report the exception using methods like Sentry captures unhandled exceptions by overriding sentry-python/sentry_sdk/integrations/excepthook.py Lines 23 to 41 in e864eab
By default, Typer handles unhandled exceptions without using This default behavior can interfere with Sentry's error notification process when an unhandled exception occurs. As @danielbraun89 mentioned in Option 1, disabling Typer's exception handler should resolve this issue and allow Sentry's error notifications to work properly. Additionally, we can use the app = typer.Typer(pretty_exceptions_enable=False) https://typer.tiangolo.com/tutorial/exceptions/#disable-pretty-exceptions |
For me, it also works when initializing Sentry inside the Typer command: import sentry_sdk
import typer
app = typer.Typer()
@app.command()
def main():
sentry_sdk.init(dsn="SNIP")
raise Exception("Oh no!")
if __name__ == "__main__":
app() This probably has the same effect as the callback method above |
Thanks everyone for all the information. I guess we will add a typer integration to make it work automagically or at least update the docs to help people set it up correctly |
I'm a big fan of Sentry, I don't know how it does its internal magic to make things work so I'm not sure what is needed from the Typer side, but I would change the internal implementation in Typer to allow an easier integration with Sentry (or expose some types of hooks, or something). The custom error handling in Typer is just to not show a lot of useless internals from Typer and Click to developers and users of CLIs, as the errors are (normally) just in their own code, to make it easier to understand and fix those errors. But apart from being able to have pretty errors in the terminal, I would change things internally if that helps Sentry. I also want to implement nicer/pretty errors in FastAPI, so it would be nice to figure out the right way to do this correctly before that. @antonpirker, if I don't reply here, please reach out to me via DM, email, or something, it could get lost in my thousands of GitHub notifications. 😅 |
That's great to hear @tiangolo ! Thanks for offering to change internals if it makes our lives easier! I will get back to you as soon as we will pick this up! (Will take some time, because we are doing a Python SDK 2.0 right now) |
How do you use Sentry?
Sentry Saas (sentry.io)
Version
1.9.8
Steps to Reproduce
I try to use sentry with typer.
Seems sentry will not catch uphandled exception with them.
With typer method 1:
With typer method 2:
Both way don't work
Expected Result
Seems sentry should record error in both way.
Actual Result
Seems sentry don't record error in either way.
The text was updated successfully, but these errors were encountered: