|
| 1 | +import sentry_sdk |
1 | 2 | import typer |
| 3 | +from sentry_sdk.integrations import Integration |
| 4 | +from sentry_sdk.utils import ( |
| 5 | + capture_internal_exceptions, |
| 6 | + event_from_exception, |
| 7 | +) |
2 | 8 |
|
3 | 9 | from .commands.deploy import deploy |
4 | 10 | from .commands.env import env_app |
5 | 11 | from .commands.login import login |
6 | 12 | from .commands.whoami import whoami |
7 | 13 |
|
8 | | -app = typer.Typer(rich_markup_mode="rich") |
| 14 | + |
| 15 | +def _make_excepthook(old_excepthook): |
| 16 | + def sentry_sdk_excepthook(type_, value, traceback): |
| 17 | + integration = sentry_sdk.get_client().get_integration(TyperIntegration) |
| 18 | + |
| 19 | + if integration is None: |
| 20 | + return old_excepthook(type_, value, traceback) |
| 21 | + |
| 22 | + with capture_internal_exceptions(): |
| 23 | + event, hint = event_from_exception( |
| 24 | + (type_, value, traceback), |
| 25 | + client_options=sentry_sdk.get_client().options, |
| 26 | + mechanism={"type": "excepthook", "handled": False}, |
| 27 | + ) |
| 28 | + sentry_sdk.capture_event(event, hint=hint) |
| 29 | + |
| 30 | + return old_excepthook(type_, value, traceback) |
| 31 | + |
| 32 | + return sentry_sdk_excepthook |
| 33 | + |
| 34 | + |
| 35 | +def patch_typer(): |
| 36 | + typer.main.except_hook = _make_excepthook(typer.main.except_hook) |
| 37 | + |
| 38 | + |
| 39 | +class TyperIntegration(Integration): |
| 40 | + identifier = "typer" |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def setup_once(): |
| 44 | + patch_typer() |
| 45 | + |
| 46 | + |
| 47 | +sentry_sdk.init( |
| 48 | + dsn="https://230250605ea4b58a0b69c768e9ec1168@o4506985151856640.ingest.us.sentry.io/4508449198899200", |
| 49 | + traces_sample_rate=1.0, |
| 50 | + integrations=[TyperIntegration()], |
| 51 | +) |
| 52 | + |
| 53 | + |
| 54 | +app = typer.Typer() |
9 | 55 |
|
10 | 56 |
|
11 | 57 | # TODO: use the app structure |
|
0 commit comments