Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/sentry_sdk/integrations/excepthook.py: 45%
39 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1import sys
3from sentry_sdk.hub import Hub
4from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
5from sentry_sdk.integrations import Integration
7from sentry_sdk._types import MYPY
9if MYPY: 9 ↛ 10line 9 didn't jump to line 10, because the condition on line 9 was never true
10 from typing import Callable
11 from typing import Any
12 from typing import Type
13 from typing import Optional
15 from types import TracebackType
17 Excepthook = Callable[
18 [Type[BaseException], BaseException, Optional[TracebackType]],
19 Any,
20 ]
23class ExcepthookIntegration(Integration):
24 identifier = "excepthook"
26 always_run = False
28 def __init__(self, always_run=False):
29 # type: (bool) -> None
31 if not isinstance(always_run, bool): 31 ↛ 32line 31 didn't jump to line 32, because the condition on line 31 was never true
32 raise ValueError(
33 "Invalid value for always_run: %s (must be type boolean)"
34 % (always_run,)
35 )
36 self.always_run = always_run
38 @staticmethod
39 def setup_once():
40 # type: () -> None
41 sys.excepthook = _make_excepthook(sys.excepthook)
44def _make_excepthook(old_excepthook):
45 # type: (Excepthook) -> Excepthook
46 def sentry_sdk_excepthook(type_, value, traceback):
47 # type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None
48 hub = Hub.current
49 integration = hub.get_integration(ExcepthookIntegration)
51 if integration is not None and _should_send(integration.always_run):
52 # If an integration is there, a client has to be there.
53 client = hub.client # type: Any
55 with capture_internal_exceptions():
56 event, hint = event_from_exception(
57 (type_, value, traceback),
58 client_options=client.options,
59 mechanism={"type": "excepthook", "handled": False},
60 )
61 hub.capture_event(event, hint=hint)
63 return old_excepthook(type_, value, traceback)
65 return sentry_sdk_excepthook
68def _should_send(always_run=False):
69 # type: (bool) -> bool
70 if always_run:
71 return True
73 if hasattr(sys, "ps1"):
74 # Disable the excepthook for interactive Python shells, otherwise
75 # every typo gets sent to Sentry.
76 return False
78 return True