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