From afbee03f26bd60059baf60de2537e6c4d39c6361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 28 Sep 2021 12:56:29 +0200 Subject: [PATCH] Suppress tracebacks for "expected" errors We shouldn't crash when network communication fails, or when a subprocess fails. The traceback is unsightly, and causes bug reports to be files. Let's just print the error. $ PYTHONPATH=$HOME/python/rust2rpm python -m rust2rpm ./asdfasdf.asdf error: the manifest-path must be a path to a Cargo.toml file Subcommand failed with code 101: cargo read-manifest --manifest-path=./asdfasdf.asdf $ PYTHONPATH=$HOME/python/rust2rpm python -m rust2rpm asdfasdf Failed to download metadata: 404 Client Error: Not Found for url: https://crates.io/api/v1/crates/asdfasdf/versions Fixes #145. (This does the relatively easy thing of printing the original message. In principle we could try to figure out what the exact error was and print uniform error messages. But that'd be quite a lot of work, and fairly brittle, because we'd need to cover all possible errors. So let's do this thing which should be good enough in 95% of cases.) --- rust2rpm/__main__.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py index 5d809ab..c945455 100644 --- a/rust2rpm/__main__.py +++ b/rust2rpm/__main__.py @@ -347,7 +347,7 @@ def main(): parser.error("required crate/path argument missing") crate, diff, metadata, doc_files, license_files = make_diff_metadata( - args.crate, args.version, patch=args.patch, store=args.store_crate) + args.crate, args.version, patch=args.patch, store=args.store_crate) JINJA_ENV.globals["normalize_deps"] = normalize_deps JINJA_ENV.globals["to_list"] = to_list @@ -468,4 +468,10 @@ def main(): fobj.writelines(diff) if __name__ == "__main__": - main() + try: + main() + except requests.exceptions.HTTPError as e: + sys.exit(f'Failed to download metadata: {e}') + except subprocess.CalledProcessError as e: + cmd = shlex.join(e.cmd) + sys.exit(f'Subcommand failed with code {e.returncode}: {cmd}')