-
Notifications
You must be signed in to change notification settings - Fork 0
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
Rework error handling #2
Conversation
erlend-aasland
commented
Jul 29, 2023
- Introduce ClinicError and ClinicWarning
- fail() raises ClinicError
- warn() uses warnings.warn(msg, ClinicWarning)
- the CLI runs main(), catches ClinicError, formats the error message prints to stderr and exits with an error
- adapt the test suite to work with ClinicError
- Introduce ClinicError and ClinicWarning - fail() raises ClinicError - warn() uses warnings.warn(msg, ClinicWarning) - the CLI runs main(), catches ClinicError, formats the error message prints to stderr and exits with an error - adapt the test suite to work with ClinicError
@AlexWaygood, what do you think of this approach? |
Can be broken up like this:
|
(BTW, since I touched pretty much every test that checks failure of some kind, I just normalised variable naming for them all) |
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.
Looks great overall. A few comments below, but nothing major:
I have a few more thoughts after you've taken a look at my first round of review ;) But again, nothing major! |
Since you agree with the overall approach, let's take this to the cpython repo in smaller steps :) |
Yep. One more thing I would say is that it would be nice to restructure the top-level def main(argv: list[str] | None = None) -> None:
cli = create_cli()
try:
args = cli.parse_args(argv)
run_clinic(args)
sys.exit(0)
except CLIError as exc:
if msg := str(exc):
sys.stderr.write(f"Usage error: {msg}\n")
cli.print_usage()
sys.exit(1)
except ClinicError as exc:
msg = textwrap.dedent(f"""\
Error in file {exc.filename!r} on line {exc.lineno}:
{exc}
""")
sys.stderr.write(str(exc))
sys.exit(1)
if __name__ == "__main__":
main() ( If we did that, it would be very easy to do end-to-end tests of the CLI without using subprocesses. (The tests got a fair bit slower for me locally on Windows since we started using subprocesses -- they have a lot of overhead on Windows, sadly 😞). When the CLI is actually run, |
That's a very good idea. Reducing CI (and local dev workflow) time is a very good thing. |