Skip to content

Commit 09dc0b2

Browse files
committed
fix: un-expose Event type
1 parent 45399e6 commit 09dc0b2

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

sentry_sdk/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import random
44
import atexit
55

6-
from .utils import Dsn, SkipEvent, ContextVar
6+
from .utils import Dsn, SkipEvent, ContextVar, Event
77
from .transport import Transport
88
from .consts import DEFAULT_OPTIONS, SDK_INFO
99
from .stripping import strip_event, flatten_metadata
@@ -121,6 +121,8 @@ def capture_event(self, event, scope=None):
121121
"""Captures an event."""
122122
if self._transport is None:
123123
return
124+
if not isinstance(event, Event):
125+
event = Event(event)
124126
try:
125127
self._check_should_capture(event)
126128
event = self._prepare_event(event, scope)

sentry_sdk/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,15 @@ def exceptions_from_error_tuple(exc_type, exc_value, tb, with_locals=True):
316316
class Event(Mapping):
317317
__slots__ = ("_data", "_exc_value")
318318

319-
def __init__(self):
319+
def __init__(self, data={}):
320320
self._data = {
321321
"event_id": uuid.uuid4().hex,
322322
"timestamp": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ"),
323323
"level": "error",
324324
}
325+
326+
self._data.update(data)
327+
325328
self._exc_value = None
326329

327330
def get_json(self):

tests/test_client.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
from sentry_sdk.transport import Transport
99
from sentry_sdk.utils import Event, Dsn
1010

11+
class EventCaptured(Exception):
12+
pass
13+
14+
class _TestTransport(Transport):
15+
def __init__(self, *a, **kw):
16+
pass
17+
18+
def start(self):
19+
pass
20+
21+
def capture_event(self, event):
22+
raise EventCaptured()
23+
1124

1225
def test_transport_option(monkeypatch):
1326
dsn = "https://[email protected]/123"
@@ -30,25 +43,18 @@ def e(exc_type):
3043
rv._exc_value = exc_type()
3144
return rv
3245

33-
class EventCaptured(Exception):
34-
pass
35-
36-
class TestTransport(Transport):
37-
def __init__(self, *a, **kw):
38-
pass
39-
40-
def start(self):
41-
pass
42-
43-
def capture_event(self, event):
44-
raise EventCaptured()
45-
46-
c = Client(ignore_errors=[Exception], transport=TestTransport())
46+
c = Client(ignore_errors=[Exception], transport=_TestTransport())
4747
c.capture_event(e(Exception))
4848
c.capture_event(e(ValueError))
4949
pytest.raises(EventCaptured, lambda: c.capture_event(e(BaseException)))
5050

5151

52+
def test_capture_event_works():
53+
c = Client(transport=_TestTransport())
54+
pytest.raises(EventCaptured, lambda: c.capture_event({}))
55+
pytest.raises(EventCaptured, lambda: c.capture_event(Event()))
56+
57+
5258
@pytest.mark.parametrize("num_messages", [10, 20])
5359
def test_atexit(tmpdir, monkeypatch, num_messages):
5460
app = tmpdir.join("app.py")

0 commit comments

Comments
 (0)