Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: catch exceptions
Browse files Browse the repository at this point in the history
sergkudinov committed Oct 26, 2023
1 parent 1e93df1 commit 2665e42
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tdp/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -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.
@@ -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",
10 changes: 10 additions & 0 deletions tdp/cli/utils.py
Original file line number Diff line number Diff line change
@@ -217,3 +217,13 @@ 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:
print("Error:", e)

0 comments on commit 2665e42

Please sign in to comment.