forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunraisablehook.py
More file actions
52 lines (40 loc) · 1.68 KB
/
Copy pathunraisablehook.py
File metadata and controls
52 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys
import sentry_sdk
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
)
from sentry_sdk.integrations import Integration
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Callable
from typing import Any
class UnraisablehookIntegration(Integration):
identifier = "unraisablehook"
@staticmethod
def setup_once() -> None:
sys.unraisablehook = _make_unraisable(sys.unraisablehook)
def _make_unraisable(
old_unraisablehook: "Callable[[sys.UnraisableHookArgs], Any]",
) -> "Callable[[sys.UnraisableHookArgs], Any]":
def sentry_sdk_unraisablehook(unraisable: "sys.UnraisableHookArgs") -> None:
integration = sentry_sdk.get_client().get_integration(UnraisablehookIntegration)
# Note: If we replace this with ensure_integration_enabled then
# we break the exceptiongroup backport;
# See: https://github.com/getsentry/sentry-python/issues/3097
if integration is None:
return old_unraisablehook(unraisable)
if unraisable.exc_value and unraisable.exc_traceback:
with capture_internal_exceptions():
event, hint = event_from_exception(
(
unraisable.exc_type,
unraisable.exc_value,
unraisable.exc_traceback,
),
client_options=sentry_sdk.get_client().options,
mechanism={"type": "unraisablehook", "handled": False},
)
sentry_sdk.capture_event(event, hint=hint)
return old_unraisablehook(unraisable)
return sentry_sdk_unraisablehook