Skip to content

Commit 4bcaf56

Browse files
committed
Add support for add_note()
1 parent 851bb43 commit 4bcaf56

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
@@ -688,11 +688,15 @@ def get_errno(exc_value):
688688

689689
def get_error_message(exc_value):
690690
# type: (Optional[BaseException]) -> str
691-
return (
691+
value = (
692692
getattr(exc_value, "message", "")
693693
or getattr(exc_value, "detail", "")
694694
or safe_str(exc_value)
695695
)
696+
notes = getattr(exc_value, "__notes__", [])
697+
if notes:
698+
value = "\n".join([value] + [safe_str(note) for note in notes])
699+
return value
696700

697701

698702
def single_exception_from_error_tuple(

tests/test_basics.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,3 +955,47 @@ def test_hub_current_deprecation_warning():
955955
def test_hub_main_deprecation_warnings():
956956
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
957957
Hub.main
958+
959+
960+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
961+
def test_notes(sentry_init, capture_events):
962+
sentry_init()
963+
events = capture_events()
964+
try:
965+
e = ValueError("aha!")
966+
e.add_note("Test 123")
967+
raise e
968+
except Exception:
969+
capture_exception()
970+
971+
(event,) = events
972+
973+
assert event["exception"]["values"][0]["value"] == "aha!\nTest 123"
974+
975+
976+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
977+
def test_notes_safe_str(sentry_init, capture_events):
978+
class Note2:
979+
def __repr__(self):
980+
raise TypeError
981+
982+
def __str__(self):
983+
raise TypeError
984+
985+
sentry_init()
986+
events = capture_events()
987+
try:
988+
e = ValueError("aha!")
989+
e.add_note("note 1")
990+
e.__notes__.append(Note2()) # type: ignore
991+
e.add_note("note 3")
992+
raise e
993+
except Exception:
994+
capture_exception()
995+
996+
(event,) = events
997+
998+
assert (
999+
event["exception"]["values"][0]["value"]
1000+
== "aha!\nnote 1\n<broken repr>\nnote 3"
1001+
)

0 commit comments

Comments
 (0)