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

feat: catch exceptions #482

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion tdp/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tdp.cli.commands.validate import validate
from tdp.cli.commands.vars import vars
from tdp.cli.logger import setup_logging
from tdp.cli.utils import CatchGroup

# Add `-h` shortcut to print the help for the whole cli.
# Click only uses `--help` by default.
Expand All @@ -35,7 +36,7 @@ def load_env(ctx: click.Context, param: click.Parameter, value: Path) -> Optiona
logging.warning(f"Environment file {value} does not exist.")


@click.group("tdp", context_settings=CONTEXT_SETTINGS)
@click.group("tdp", context_settings=CONTEXT_SETTINGS, cls=CatchGroup)
@click.option(
"--env",
default=".env",
Expand Down
11 changes: 11 additions & 0 deletions tdp/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ def decorator(fn: FC) -> FC:
return decorator
else:
return decorator(func)


class CatchGroup(click.Group):
"""Catch exceptions and print them to stderr."""

def __call__(self, *args, **kwargs):
try:
return self.main(*args, **kwargs)

except Exception as e:
click.echo(f"Error: {e}", err=True)