Skip to content

Commit 5980908

Browse files
committed
feat: add excepthook handler by default
1 parent 2a941a1 commit 5980908

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

sentry_sdk/client.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
_most_recent_exception = ContextVar("sentry_most_recent_exception")
1515

1616

17+
def _get_default_integrations():
18+
from .integrations.logging import LoggingIntegration
19+
from .integrations.excepthook import ExcepthookIntegration
20+
21+
yield LoggingIntegration
22+
yield ExcepthookIntegration
23+
24+
1725
class Client(object):
1826
def __init__(self, dsn=None, *args, **kwargs):
1927
passed_dsn = dsn
@@ -40,15 +48,12 @@ def __init__(self, dsn=None, *args, **kwargs):
4048
elif passed_dsn is not None and self._transport is not None:
4149
raise ValueError("Cannot pass DSN and a custom transport.")
4250

43-
from .integrations import logging as logging_integration
44-
4551
integrations = list(options.pop("integrations") or ())
4652

47-
logging_configured = any(
48-
isinstance(x, logging_integration.LoggingIntegration) for x in integrations
49-
)
50-
if not logging_configured and options["default_integrations"]:
51-
integrations.append(logging_integration.LoggingIntegration())
53+
if options["default_integrations"]:
54+
for cls in _get_default_integrations():
55+
if not any(isinstance(x, cls) for x in integrations):
56+
integrations.append(cls())
5257

5358
for integration in integrations:
5459
integration(self)

sentry_sdk/integrations/excepthook.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from sentry_sdk import capture_exception
4+
from sentry_sdk.hub import _internal_exceptions
5+
6+
from . import Integration
7+
8+
9+
class ExcepthookIntegration(Integration):
10+
identifier = "excepthook"
11+
12+
def __init__(self):
13+
pass
14+
15+
def install(self, client):
16+
sys.excepthook = _make_excepthook(sys.excepthook)
17+
18+
19+
def _make_excepthook(old_excepthook):
20+
def sentry_sdk_excepthook(exctype, value, traceback):
21+
with _internal_exceptions():
22+
capture_exception((exctype, value, traceback))
23+
24+
return old_excepthook(exctype, value, traceback)
25+
26+
return sentry_sdk_excepthook
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pytest
2+
import sys
3+
import subprocess
4+
5+
from textwrap import dedent
6+
7+
8+
def test_excepthook(tmpdir):
9+
app = tmpdir.join("app.py")
10+
app.write(
11+
dedent(
12+
"""
13+
from sentry_sdk import init, transport
14+
15+
def send_event(pool, event, auth):
16+
print("capture event was called")
17+
print(event)
18+
19+
transport.send_event = send_event
20+
21+
init("http://foobar@localhost/123")
22+
23+
frame_value = "LOL"
24+
25+
1/0
26+
"""
27+
)
28+
)
29+
30+
with pytest.raises(subprocess.CalledProcessError) as excinfo:
31+
subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT)
32+
33+
output = excinfo.value.output
34+
35+
assert b"ZeroDivisionError" in output
36+
assert b"LOL" in output
37+
assert b"capture event was called" in output

0 commit comments

Comments
 (0)