Skip to content

Commit

Permalink
Add nicer error messages for unhandled exceptions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jfrost-mo committed Nov 5, 2024
1 parent df53dd7 commit 2c4f83d
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/CSET/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 2c4f83d

Please sign in to comment.