Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Display exception notes when raising ClickException #2740

Open
azmeuk opened this issue Jun 5, 2024 · 2 comments
Open

Display exception notes when raising ClickException #2740

azmeuk opened this issue Jun 5, 2024 · 2 comments

Comments

@azmeuk
Copy link

azmeuk commented Jun 5, 2024

Since 3.11 Python supports adding notes on exceptions. Basically this is completion of the exception message, and it can be useful to add context.

Notes are currently not displayed when ClickException are raised:

import click

@click.command()
def foobar():
    exc = click.ClickException("foo")
    exc.add_note("bar")
    raise exc

if __name__ == '__main__':
    foobar()
$ python foobar.py
Error: foo

I suggest to display the notes in addition to the main message.

What do you think?

@davidism
Copy link
Member

davidism commented Jun 5, 2024

What would be in these notes that wouldn't go in the message directly? The example in the docs is for errors raised elsewhere and caught, but that can also be done with raise New("") from old. And I don't think these errors are expected to be caught and modified either way.

@azmeuk
Copy link
Author

azmeuk commented Jun 5, 2024

What would be in these notes that wouldn't go in the message directly?

For sure this would not bring anything click can't already do. This would just be for convenience.

After having used add_note a bit, I would have naively expected click exception display to behave a bit like uncatched python exception display, i.e. to display the notes.

For the record, the situations where I find it useful is to re-raise an exception with more context:

try:
    something_dangerous()
except ClickException as exc:
    exc.add_note("additional context")
    raise exc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants