-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy patherror.py
More file actions
36 lines (29 loc) · 1.12 KB
/
error.py
File metadata and controls
36 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import typing as t
import logging
from functools import wraps
import click
import sys
logger = logging.getLogger(__name__)
def cli_global_error_handler(
func: t.Callable[..., t.Any],
) -> t.Callable[..., t.Any]:
@wraps(func)
def wrapper(*args: t.List[t.Any], **kwargs: t.Any) -> t.Any:
try:
return func(*args, **kwargs)
except Exception as ex:
# these imports are deliberately deferred to avoid the penalty of importing the `sqlmesh`
# package up front for every CLI command
from sqlmesh.utils.errors import SQLMeshError
from sqlglot.errors import SqlglotError
if isinstance(ex, (SQLMeshError, SqlglotError, ValueError)):
click.echo(click.style("Error: " + str(ex), fg="red"))
sys.exit(1)
else:
raise
return wrapper
class ErrorHandlingGroup(click.Group):
def add_command(self, cmd: click.Command, name: t.Optional[str] = None) -> None:
if cmd.callback:
cmd.callback = cli_global_error_handler(cmd.callback)
super().add_command(cmd, name=name)