Skip to content

Commit 4b2a1f5

Browse files
committed
feat: catch exceptions
1 parent f8dacce commit 4b2a1f5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

tdp/cli/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from tdp.cli.commands.validate import validate
2121
from tdp.cli.commands.vars import vars
2222
from tdp.cli.logger import setup_logging
23+
from tdp.cli.utils import CatchGroup
2324

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

3738

38-
@click.group("tdp", context_settings=CONTEXT_SETTINGS)
39+
@click.group("tdp", context_settings=CONTEXT_SETTINGS, cls=CatchGroup)
3940
@click.option(
4041
"--env",
4142
default=".env",

tdp/cli/utils.py

+11
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,14 @@ def decorator(fn: FC) -> FC:
217217
return decorator
218218
else:
219219
return decorator(func)
220+
221+
222+
class CatchGroup(click.Group):
223+
"""Catch exceptions and print them to stderr."""
224+
225+
def __call__(self, *args, **kwargs):
226+
try:
227+
return self.main(*args, **kwargs)
228+
229+
except Exception as e:
230+
click.echo(f"Error: {e}", err=True)

0 commit comments

Comments
 (0)