Skip to content

Add support for add_note() #3056

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

Closed
Closed
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
6 changes: 5 additions & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,15 @@ def get_errno(exc_value):

def get_error_message(exc_value):
# type: (Optional[BaseException]) -> str
return (
value = (
getattr(exc_value, "message", "")
or getattr(exc_value, "detail", "")
or safe_str(exc_value)
)
notes = getattr(exc_value, "__notes__", [])
if notes:
value = "\n".join([value] + [safe_str(note) for note in notes])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure whether it makes sense to add the note to the exception message; perhaps setting this as extra data would be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I responded in the pull request conversation.

I think the notes add context that developers expect to be shown directly with the error message. That is what CPython does in the console.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this safe_str stuff here, notes should anyways just be strings. This is guaranteed when using add_note. Although you correctly point out in your test that it is possible to add non-string values to the __notes__ list manually, this is likely a misuse of __notes__, and is not something we need to support; although, we should still not crash if there is an invalid value in the __notes__.

I would suggest we just filter out any non-string values from the __notes__.

return value


def single_exception_from_error_tuple(
Expand Down
44 changes: 44 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,47 @@ 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")
raise e
except Exception:
capture_exception()

(event,) = events

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


@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")
raise e
except Exception:
capture_exception()

(event,) = events

assert (
event["exception"]["values"][0]["value"]
== "aha!\nnote 1\n<broken repr>\nnote 3"
)
Loading