Skip to content

Commit d4bbf5e

Browse files
untitakermitsuhiko
authored andcommitted
feat: Add debug flag (#37)
1 parent 33115bf commit d4bbf5e

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

sentry_sdk/consts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ignore_errors": [],
2525
"request_bodies": "medium",
2626
"before_send": None,
27+
"debug": False,
2728
}
2829

2930
SDK_INFO = {"name": "sentry-python", "version": VERSION}

sentry_sdk/hub.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from ._compat import with_metaclass
66
from .scope import Scope
7-
from .utils import exc_info_from_error, event_from_exception, ContextVar
7+
from .utils import exc_info_from_error, event_from_exception, get_logger, ContextVar
88

99

1010
_local = ContextVar("sentry_current_hub")
1111

12+
logger = get_logger(__name__)
13+
1214

1315
@contextmanager
1416
def _internal_exceptions():
@@ -134,7 +136,9 @@ def capture_exception(self, error=None):
134136
def capture_internal_exception(self, exc_info):
135137
"""Capture an exception that is likely caused by a bug in the SDK
136138
itself."""
137-
pass
139+
client = self.client
140+
if client is not None and client.options["debug"]:
141+
logger.debug("Internal error in sentry_sdk", exc_info=exc_info)
138142

139143
def add_breadcrumb(self, *args, **kwargs):
140144
"""Adds a breadcrumb."""

sentry_sdk/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import linecache
4+
import logging
45

56
from datetime import datetime
67
from collections import Mapping, Sequence
@@ -520,6 +521,14 @@ def strip_string(value, assume_length=None, max_length=512):
520521
return value[:max_length]
521522

522523

524+
def get_logger(name):
525+
rv = logging.getLogger(name)
526+
if not rv.handlers:
527+
rv.addHandler(logging.StreamHandler(sys.stderr))
528+
rv.setLevel(logging.DEBUG)
529+
return rv
530+
531+
523532
try:
524533
from contextvars import ContextVar
525534
except ImportError:

tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616

1717
@pytest.fixture(autouse=True)
18-
def reraise_internal_exceptions(monkeypatch):
18+
def reraise_internal_exceptions(request, monkeypatch):
19+
if "tests_internal_exceptions" in request.keywords:
20+
return
21+
1922
def capture_internal_exception(exc_info):
2023
reraise(*exc_info)
2124

tests/test_client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def callback(scope):
138138
assert not calls
139139

140140

141-
def test_transport_works(sentry_init, httpserver, request, capsys):
141+
def test_transport_works(httpserver, request, capsys):
142142
httpserver.serve_content("ok", 200)
143143

144144
client = Client("http://foobar@{}/123".format(httpserver.url[len("http://") :]))
@@ -152,3 +152,21 @@ def test_transport_works(sentry_init, httpserver, request, capsys):
152152
out, err = capsys.readouterr()
153153
assert not err and not out
154154
assert httpserver.requests
155+
156+
157+
@pytest.mark.tests_internal_exceptions
158+
def test_client_debug_option_enabled(sentry_init, caplog):
159+
sentry_init(debug=True)
160+
161+
Hub.current.capture_internal_exception((ValueError, ValueError("OK"), None))
162+
assert "OK" in caplog.text
163+
164+
165+
@pytest.mark.tests_internal_exceptions
166+
@pytest.mark.parametrize("with_client", (True, False))
167+
def test_client_debug_option_disabled(with_client, sentry_init, caplog):
168+
if with_client:
169+
sentry_init()
170+
171+
Hub.current.capture_internal_exception((ValueError, ValueError("OK"), None))
172+
assert "OK" not in caplog.text

0 commit comments

Comments
 (0)