Skip to content

Commit

Permalink
feat: catch exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sergkudinov committed Oct 26, 2023
1 parent 1e93df1 commit 581bf85
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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:
print("Error:", e)

0 comments on commit 581bf85

Please sign in to comment.