-
Notifications
You must be signed in to change notification settings - Fork 548
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
Conversation
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]) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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__
.
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you remove the label "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you remove the label "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
The use case for the notes is to add text to the exception massage. I think def failure():
raise ValueError("Nope.")
MESSAGE = """\
Nope. It's wrong!
See https://example.com/why-nope.html for more info
"""
def notes_old():
try:
failure()
except Exception as e:
raise RuntimeError(f"{e}\n{MESSAGE}\n") from e
def notes_new():
try:
failure()
except Exception as e:
e.add_note(MESSAGE)
raise
I tried to have BTW python cli renders the notes after the Exception message as well:
|
This issue has gone three weeks without activity. In another week, I will close it. But! If you comment or otherwise update it, I will reset the clock, and if you remove the label "A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀 |
def get_foo(direction):
D = dict(up=6, down=4)
try:
return D[direction]
except KeyError as e:
e.add_note(f"{direction!r} is not a valid direction")
raise
I think sentry should render this as: KeyError
|
4bcaf56
to
194ef32
Compare
194ef32
to
9a04d67
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #3056 +/- ##
==========================================
- Coverage 84.30% 84.25% -0.06%
==========================================
Files 133 133
Lines 13902 13906 +4
Branches 2933 2935 +2
==========================================
- Hits 11720 11716 -4
- Misses 1444 1455 +11
+ Partials 738 735 -3
|
Even though the PR says it can be edited by maintainers, I am getting a 403 when I try to push to this PR branch. So, I am closing this PR in favor of #3620 |
See issue #3050
This adds support for
__notes__
and adds them to the exception value.