forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcepthook.py
More file actions
41 lines (29 loc) · 1.2 KB
/
excepthook.py
File metadata and controls
41 lines (29 loc) · 1.2 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
import sys
from sentry_sdk.hub import Hub
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from sentry_sdk.integrations import Integration
class ExcepthookIntegration(Integration):
identifier = "excepthook"
@staticmethod
def setup_once():
sys.excepthook = _make_excepthook(sys.excepthook)
def _make_excepthook(old_excepthook):
def sentry_sdk_excepthook(exctype, value, traceback):
hub = Hub.current
integration = hub.get_integration(ExcepthookIntegration)
if integration is not None and _should_send():
with capture_internal_exceptions():
event, hint = event_from_exception(
(exctype, value, traceback),
client_options=hub.client.options,
mechanism={"type": "excepthook", "handled": False},
)
hub.capture_event(event, hint=hint)
return old_excepthook(exctype, value, traceback)
return sentry_sdk_excepthook
def _should_send():
if hasattr(sys, "ps1"):
# Disable the excepthook for interactive Python shells, otherwise
# every typo gets sent to Sentry.
return False
return True