From 2c4f83d31a5cb9d73c3d057ac5e2470003156154 Mon Sep 17 00:00:00 2001 From: James Frost Date: Thu, 31 Oct 2024 17:24:32 +0000 Subject: [PATCH] Add nicer error messages for unhandled exceptions This prevents the sudden wall of tracebacks when you have just typoed a file path. The full traceback is still output for DEBUG logging (-vv). Fixes #581 --- src/CSET/__init__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/CSET/__init__.py b/src/CSET/__init__.py index 4e4f5641e..89e47dfdd 100644 --- a/src/CSET/__init__.py +++ b/src/CSET/__init__.py @@ -26,7 +26,11 @@ def main(): - """CLI entrypoint.""" + """CLI entrypoint. + + Handles argument parsing, setting up logging, top level error capturing, + and execution of the desired subcommand. + """ parser = argparse.ArgumentParser( prog="cset", description="Convective Scale Evaluation Tool" ) @@ -159,9 +163,18 @@ def main(): # Execute the specified subcommand. args.func(args, unparsed_args) except ArgumentError as err: - logging.error(err) + # Error message for when needed template variables are missing. + print(err, file=sys.stderr) parser.print_usage() - sys.exit(3) + sys.exit(127) + except Exception as err: + # Provide slightly nicer error messages for unhandled exceptions. + print(err, file=sys.stderr) + # Display the time and full traceback when debug logging. + logging.debug("An unhandled exception occurred.") + if logging.root.isEnabledFor(logging.DEBUG): + raise + sys.exit(1) def calculate_loglevel(args) -> int: