Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide stacktrace unless verbose mode is enabled #148

Merged
merged 7 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions aiidalab_launch/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import getpass
import logging
import socket
import sys
from pathlib import Path
from textwrap import wrap

Expand Down Expand Up @@ -67,6 +68,13 @@
pass_app_state = click.make_pass_decorator(ApplicationState, ensure=True)


def exception_handler(exception_type, exception, traceback): # noqa: U100
csadorf marked this conversation as resolved.
Show resolved Hide resolved
click.echo(f"Unexpected {exception_type.__name__}: {exception}", err=True)
click.echo(
"Use verbose mode `aiidalab-launch --verbose` to see full stacktrace", err=True
danielhollas marked this conversation as resolved.
Show resolved Hide resolved
)


def with_profile(cmd):
def callback(ctx, param, value): # noqa: U100
app_state = ctx.ensure_object(ApplicationState)
Expand Down Expand Up @@ -100,6 +108,10 @@ def cli(app_state, verbose):
err=True,
)

# Hide stack traces by default
danielhollas marked this conversation as resolved.
Show resolved Hide resolved
if verbose == 0:
sys.excepthook = exception_handler

LOGGER.info(f"Configuration file path: {app_state.config_path}")

latest_version = get_latest_version(timeout=0.1)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ def test_version_verbose_logging():
assert "Verbose logging is enabled." in result.output.strip()


def test_hidden_stacktrace():
"""
Arrange/Act: Run `profiles edit invalid` subcommand.
Assert: The output does not contain stacktrace.
danielhollas marked this conversation as resolved.
Show resolved Hide resolved
"""
runner: CliRunner = CliRunner()
with pytest.raises(ValueError):
result: Result = runner.invoke(
cli.cli, ["profiles", "show", "invalid"], catch_exceptions=False
)
result: Result = runner.invoke(
cli.cli,
["profiles", "show", "invalid"],
)
assert isinstance(result.exception, ValueError)


def test_list_profiles():
runner: CliRunner = CliRunner()
result: Result = runner.invoke(cli.cli, ["profiles", "list"])
Expand Down