Skip to content

Commit

Permalink
Add a debugging mode controlled by AUGUR_DEBUG
Browse files Browse the repository at this point in the history
At the moment, this only enables printing of stack traces and the full
exception chain for handled (i.e. anticipated) errors, which otherwise
were not printed.  In the future, this mode can also control the output
of verbose debugging/troubleshooting logging for more commands.

Commit contents and message based on similar commits I made to
Nextstrain CLI.¹

Resolves <#1308>.

¹ <nextstrain/cli@229f2e8>
  <nextstrain/cli@b6d9290>
  <nextstrain/cli#208>
  • Loading branch information
tsibley committed Aug 14, 2024
1 parent 92c2926 commit 67551f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* A new command, `augur merge`, now allows for generalized merging of two or more metadata tables. [#1563][] (@tsibley)
* Two new commands, `augur read-file` and `augur write-file`, now allow external programs to do i/o like Augur by piping from/to these new commands. They provide handling of compression formats and newlines consistent with the rest of Augur. [#1562][] (@tsibley)
* A new debugging mode can be enabled by setting the `AUGUR_DEBUG` environment variable to `1` (or another truthy value). Currently the only effect is to print more information about handled (i.e. anticipated) errors. For example, stack traces and parent exceptions in an exception chain are normally omitted for handled errors, but setting this env var includes them. Future debugging and troubleshooting features, like verbose operation logging, will likely also condition on this new debugging mode. [#1563][] (@tsibley)

### Bug Fixes

Expand Down
15 changes: 15 additions & 0 deletions augur/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from types import SimpleNamespace
from treetime import TreeTimeError, TreeTimeUnknownError

from .debug import DEBUGGING
from .errors import AugurError
from .io.print import print_err
from .argparse_ import add_command_subparsers, add_default_command
Expand Down Expand Up @@ -68,21 +69,35 @@ def run(argv):
try:
return args.__command__.run(args)
except AugurError as e:
if DEBUGGING:
traceback.print_exc(file=sys.stderr)
print_err(f"ERROR: {e}")
sys.exit(2)
except RecursionError:
if DEBUGGING:
traceback.print_exc(file=sys.stderr)
print_err("FATAL: Maximum recursion depth reached. You can set the env variable AUGUR_RECURSION_LIMIT to adjust this (current limit: {})".format(sys.getrecursionlimit()))
sys.exit(2)
except FileNotFoundError as e:
if DEBUGGING:
traceback.print_exc(file=sys.stderr)
print_err(f"ERROR: {e.strerror}: '{e.filename}'")
sys.exit(2)
except TreeTimeUnknownError as e:
# TreeTime already prints the traceback (and some other verbiage) in
# TreeTime.run()¹, so don't duplicate it. This is also why the "(see
# above)" in our message below makes sense.
# -trs, 14 Aug 2024
#
# ¹ <https://github.com/neherlab/treetime/blob/531f776bcad3b8b11fd470a403a9c06c78b07e36/treetime/treetime.py#L54-L73>
print_err(dedent("""\
ERROR from TreeTime: An error occurred in TreeTime (see above). This may be due to an issue with TreeTime or Augur.
Please report you are calling TreeTime via Augur.
"""))
sys.exit(2)
except TreeTimeError as e:
if DEBUGGING:
traceback.print_exc(file=sys.stderr)
print_err(f"ERROR: {e}")
print_err("\n")
print_err(dedent("""\
Expand Down
12 changes: 12 additions & 0 deletions augur/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Debug flags and utilities.
.. envvar:: AUGUR_DEBUG
Set to a truthy value (e.g. 1) to print more information about (handled)
errors. For example, when this is not set or falsey, stack traces and
parent exceptions in an exception chain are omitted from handled errors.
"""
from os import environ

DEBUGGING = bool(environ.get("AUGUR_DEBUG"))

0 comments on commit 67551f1

Please sign in to comment.