Skip to content

Commit 2d2e548

Browse files
feat: Add __notes__ support (#3620)
* Add support for add_note() * Ignore non-str notes * minor tweaks --------- Co-authored-by: Arjen Nienhuis <[email protected]>
1 parent a31c54f commit 2d2e548

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Diff for: sentry_sdk/utils.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,21 @@ def get_errno(exc_value):
713713

714714
def get_error_message(exc_value):
715715
# type: (Optional[BaseException]) -> str
716-
return (
716+
message = (
717717
getattr(exc_value, "message", "")
718718
or getattr(exc_value, "detail", "")
719719
or safe_str(exc_value)
720-
)
720+
) # type: str
721+
722+
# __notes__ should be a list of strings when notes are added
723+
# via add_note, but can be anything else if __notes__ is set
724+
# directly. We only support strings in __notes__, since that
725+
# is the correct use.
726+
notes = getattr(exc_value, "__notes__", None) # type: object
727+
if isinstance(notes, list) and len(notes) > 0:
728+
message += "\n" + "\n".join(note for note in notes if isinstance(note, str))
729+
730+
return message
721731

722732

723733
def single_exception_from_error_tuple(

Diff for: tests/test_basics.py

+43
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,46 @@ 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+
e.add_note("another note")
1012+
raise e
1013+
except Exception:
1014+
capture_exception()
1015+
1016+
(event,) = events
1017+
1018+
assert event["exception"]["values"][0]["value"] == "aha!\nTest 123\nanother note"
1019+
1020+
1021+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
1022+
def test_notes_safe_str(sentry_init, capture_events):
1023+
class Note2:
1024+
def __repr__(self):
1025+
raise TypeError
1026+
1027+
def __str__(self):
1028+
raise TypeError
1029+
1030+
sentry_init()
1031+
events = capture_events()
1032+
try:
1033+
e = ValueError("aha!")
1034+
e.add_note("note 1")
1035+
e.__notes__.append(Note2()) # type: ignore
1036+
e.add_note("note 3")
1037+
e.__notes__.append(2) # type: ignore
1038+
raise e
1039+
except Exception:
1040+
capture_exception()
1041+
1042+
(event,) = events
1043+
1044+
assert event["exception"]["values"][0]["value"] == "aha!\nnote 1\nnote 3"

0 commit comments

Comments
 (0)