Suppress verbose tracebacks by default, show only with -vv#852
Conversation
Install a custom sys.excepthook in the fre() entry point that suppresses Python tracebacks at default and -v verbosity levels. At -vv (DEBUG), the full traceback is shown. At lower levels, only the exception type and message are printed, along with a hint to use 'fre -vv ...' for more detail. Closes #793 Agent-Logs-Url: https://github.com/NOAA-GFDL/fre-cli/sessions/d594a572-8566-467b-9a96-4b233d712582 Co-authored-by: ilaflott <6273252+ilaflott@users.noreply.github.com>
Agent-Logs-Url: https://github.com/NOAA-GFDL/fre-cli/sessions/d594a572-8566-467b-9a96-4b233d712582 Co-authored-by: ilaflott <6273252+ilaflott@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #852 +/- ##
==========================================
- Coverage 84.35% 84.32% -0.03%
==========================================
Files 71 71
Lines 4997 5003 +6
==========================================
+ Hits 4215 4219 +4
- Misses 782 784 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
|
@ilaflott is this ready for review? Seems like it could be pretty helpful in generating easier-to-look at output! |
|
untested and i haven't checked copilot's output very closely |
There was a problem hiding this comment.
Pull request overview
This PR changes the fre CLI’s default behavior for unexpected/unhandled exceptions to reduce noisy stack traces, showing a brief ExceptionType: message output by default (and with -v), while preserving full tracebacks when users opt into -vv (debug verbosity).
Changes:
- Add a custom
sys.excepthookin thefreclick group entrypoint to suppress tracebacks unless debug logging is enabled. - Add subprocess-based tests to validate default,
-v, and-vvtraceback behavior. - Document the new traceback behavior under the
-v[v]flag in the tools documentation.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
fre/fre.py |
Installs a brief sys.excepthook when not in debug verbosity; updates -v help text to mention traceback behavior. |
fre/tests/test_fre_cli.py |
Adds subprocess tests to exercise real interpreter-level exception printing and verify suppression vs full tracebacks. |
docs/tools.rst |
Documents that default / -v suppress tracebacks and -vv shows full tracebacks. |
| # install custom exception hook to suppress tracebacks unless -vv is used | ||
| if log_level > logging.DEBUG: | ||
| def _brief_excepthook(exc_type, exc_value, exc_tb): | ||
| click.echo(f"{exc_type.__name__}: {exc_value}", err=True) | ||
| click.echo("(use 'fre -vv ...' for the full traceback)", err=True) | ||
| sys.excepthook = _brief_excepthook |
| fre_logger.debug('click entry-point function call done.') | ||
|
|
||
| # install custom exception hook to suppress tracebacks unless -vv is used | ||
| if log_level > logging.DEBUG: | ||
| def _brief_excepthook(exc_type, exc_value, exc_tb): | ||
| click.echo(f"{exc_type.__name__}: {exc_value}", err=True) | ||
| click.echo("(use 'fre -vv ...' for the full traceback)", err=True) | ||
| sys.excepthook = _brief_excepthook |
| @click.option( '-v', '--verbose', default = 0, required = False, count = True, type = int, | ||
| help = "Increment logging verbosity from default (logging.WARNING) to logging.INFO. " + \ | ||
| "use -vv for logging.DEBUG. will be overridden by -q/--quiet" ) | ||
| "use -vv for logging.DEBUG and to show full tracebacks on errors. " + \ | ||
| "will be overridden by -q/--quiet" ) |
| def test_traceback_suppressed_by_default(): | ||
| '''fre run function - default verbosity should suppress traceback''' | ||
| result = _run_fre_subprocess(["fre", "run", "function"]) | ||
| assert result.returncode != 0 | ||
| assert "NotImplementedError" in result.stderr | ||
| assert "Traceback" not in result.stderr | ||
| assert "fre -vv" in result.stderr | ||
|
|
||
| def test_traceback_suppressed_with_single_v(): | ||
| '''fre -v run function - single -v should still suppress traceback''' | ||
| result = _run_fre_subprocess(["fre", "-v", "run", "function"]) | ||
| assert result.returncode != 0 | ||
| assert "NotImplementedError" in result.stderr | ||
| assert "Traceback" not in result.stderr | ||
| assert "fre -vv" in result.stderr | ||
|
|
||
| def test_traceback_shown_with_vv(): | ||
| '''fre -vv run function - double -v should show full traceback''' | ||
| result = _run_fre_subprocess(["fre", "-vv", "run", "function"]) | ||
| assert result.returncode != 0 | ||
| assert "NotImplementedError" in result.stderr | ||
| assert "Traceback" in result.stderr | ||
| assert "fre -vv" not in result.stderr |
Describe your changes
Unhandled exceptions currently dump full Python stack traces, burying the actual error message in noise. This installs a custom
sys.excepthookin thefre()entry point that shows only the error type and message at default/-vverbosity, with a hint to use-vvfor the full traceback.Before (default):
After (default):
After (
fre -vv): full traceback, same as current behavior.Changes:
fre/fre.py: Setsys.excepthookto a brief formatter whenlog_level > DEBUG. Updated-vhelp text.fre/tests/test_fre_cli.py: Three subprocess-based tests covering default,-v, and-vvtraceback behavior.docs/tools.rst: Document traceback suppression under the-v[v]flag.Issue ticket number and link (if applicable)
Checklist before requesting a review