Skip to content

feat: Add __notes__ support #3620

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,21 @@ def get_errno(exc_value):

def get_error_message(exc_value):
# type: (Optional[BaseException]) -> str
return (
message = (
getattr(exc_value, "message", "")
or getattr(exc_value, "detail", "")
or safe_str(exc_value)
)
) # type: str

# __notes__ should be a list of strings when notes are added
# via add_note, but can be anything else if __notes__ is set
# directly. We only support strings in __notes__, since that
# is the correct use.
notes = getattr(exc_value, "__notes__", None) # type: object
if isinstance(notes, list) and len(notes) > 0:
message += "\n" + "\n".join(note for note in notes if isinstance(note, str))

return message


def single_exception_from_error_tuple(
Expand Down
43 changes: 43 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,46 @@ def test_hub_current_deprecation_warning():
def test_hub_main_deprecation_warnings():
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
Hub.main


@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
def test_notes(sentry_init, capture_events):
sentry_init()
events = capture_events()
try:
e = ValueError("aha!")
e.add_note("Test 123")
e.add_note("another note")
raise e
except Exception:
capture_exception()

(event,) = events

assert event["exception"]["values"][0]["value"] == "aha!\nTest 123\nanother note"


@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
def test_notes_safe_str(sentry_init, capture_events):
class Note2:
def __repr__(self):
raise TypeError

def __str__(self):
raise TypeError

sentry_init()
events = capture_events()
try:
e = ValueError("aha!")
e.add_note("note 1")
e.__notes__.append(Note2()) # type: ignore
e.add_note("note 3")
e.__notes__.append(2) # type: ignore
raise e
except Exception:
capture_exception()

(event,) = events

assert event["exception"]["values"][0]["value"] == "aha!\nnote 1\nnote 3"
Loading