Skip to content

Commit 7f25928

Browse files
tolgahanuzununtitaker
authored andcommitted
Added the always_run option in excepthook integration (getsentry#364)
* Added the always_run option in excepthook integration * Fixed contribution scheme * Test add for always_run
1 parent a1d7772 commit 7f25928

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.15
2+
3+
* Add the always_run option in excepthook integration.
4+
15
## 0.7.14
26

37
* Fix crash when using Celery integration (`TypeError` when using

sentry_sdk/integrations/excepthook.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
class ExcepthookIntegration(Integration):
1212
identifier = "excepthook"
1313

14+
always_run = False
15+
16+
def __init__(self, always_run=False):
17+
# type: (bool) -> None
18+
19+
if not isinstance(always_run, bool):
20+
raise ValueError(
21+
"Invalid value for always_run: %s (must be type boolean)"
22+
% (always_run,)
23+
)
24+
self.always_run = always_run
25+
1426
@staticmethod
1527
def setup_once():
1628
# type: () -> None
@@ -23,7 +35,7 @@ def sentry_sdk_excepthook(exctype, value, traceback):
2335
hub = Hub.current
2436
integration = hub.get_integration(ExcepthookIntegration)
2537

26-
if integration is not None and _should_send():
38+
if integration is not None and _should_send(integration.always_run):
2739
with capture_internal_exceptions():
2840
event, hint = event_from_exception(
2941
(exctype, value, traceback),
@@ -37,7 +49,10 @@ def sentry_sdk_excepthook(exctype, value, traceback):
3749
return sentry_sdk_excepthook
3850

3951

40-
def _should_send():
52+
def _should_send(always_run=False):
53+
if always_run:
54+
return True
55+
4156
if hasattr(sys, "ps1"):
4257
# Disable the excepthook for interactive Python shells, otherwise
4358
# every typo gets sent to Sentry.

tests/integrations/excepthook/test_excepthook.py

+38
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,41 @@ def send_event(self, event):
3636
assert b"ZeroDivisionError" in output
3737
assert b"LOL" in output
3838
assert b"capture event was called" in output
39+
40+
41+
def test_always_value_excepthook(tmpdir):
42+
app = tmpdir.join("app.py")
43+
app.write(
44+
dedent(
45+
"""
46+
import sys
47+
from sentry_sdk import init, transport
48+
from sentry_sdk.integrations.excepthook import ExcepthookIntegration
49+
50+
def send_event(self, event):
51+
print("capture event was called")
52+
print(event)
53+
54+
transport.HttpTransport._send_event = send_event
55+
56+
sys.ps1 = "always_value_test"
57+
init("http://foobar@localhost/123",
58+
integrations=[ExcepthookIntegration(always_run=True)]
59+
)
60+
61+
frame_value = "LOL"
62+
63+
1/0
64+
"""
65+
)
66+
)
67+
68+
with pytest.raises(subprocess.CalledProcessError) as excinfo:
69+
subprocess.check_output([sys.executable, str(app)], stderr=subprocess.STDOUT)
70+
71+
output = excinfo.value.output
72+
print(output)
73+
74+
assert b"ZeroDivisionError" in output
75+
assert b"LOL" in output
76+
assert b"capture event was called" in output

0 commit comments

Comments
 (0)