Skip to content

Commit 9a04d67

Browse files
committed
Add support for add_note()
1 parent 205591e commit 9a04d67

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

sentry_sdk/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,15 @@ def get_errno(exc_value):
713713

714714
def get_error_message(exc_value):
715715
# type: (Optional[BaseException]) -> str
716-
return (
716+
value = (
717717
getattr(exc_value, "message", "")
718718
or getattr(exc_value, "detail", "")
719719
or safe_str(exc_value)
720720
)
721+
notes = getattr(exc_value, "__notes__", [])
722+
if notes:
723+
value = "\n".join([value] + [safe_str(note) for note in notes])
724+
return value
721725

722726

723727
def single_exception_from_error_tuple(

tests/test_basics.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,47 @@ def test_hub_current_deprecation_warning():
999999
def test_hub_main_deprecation_warnings():
10001000
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
10011001
Hub.main
1002+
1003+
1004+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
1005+
def test_notes(sentry_init, capture_events):
1006+
sentry_init()
1007+
events = capture_events()
1008+
try:
1009+
e = ValueError("aha!")
1010+
e.add_note("Test 123")
1011+
raise e
1012+
except Exception:
1013+
capture_exception()
1014+
1015+
(event,) = events
1016+
1017+
assert event["exception"]["values"][0]["value"] == "aha!\nTest 123"
1018+
1019+
1020+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
1021+
def test_notes_safe_str(sentry_init, capture_events):
1022+
class Note2:
1023+
def __repr__(self):
1024+
raise TypeError
1025+
1026+
def __str__(self):
1027+
raise TypeError
1028+
1029+
sentry_init()
1030+
events = capture_events()
1031+
try:
1032+
e = ValueError("aha!")
1033+
e.add_note("note 1")
1034+
e.__notes__.append(Note2()) # type: ignore
1035+
e.add_note("note 3")
1036+
raise e
1037+
except Exception:
1038+
capture_exception()
1039+
1040+
(event,) = events
1041+
1042+
assert (
1043+
event["exception"]["values"][0]["value"]
1044+
== "aha!\nnote 1\n<broken repr>\nnote 3"
1045+
)

0 commit comments

Comments
 (0)