-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathhandle_errors.py
More file actions
35 lines (24 loc) · 881 Bytes
/
handle_errors.py
File metadata and controls
35 lines (24 loc) · 881 Bytes
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
from typing import Optional
import click
import typer
from cycode.cli.models import CliError, CliErrors
def handle_errors(
ctx: typer.Context, err: BaseException, cli_errors: CliErrors, *, return_exception: bool = False
) -> Optional['CliError']:
printer = ctx.obj.get('console_printer')
printer.print_exception(err)
if type(err) in cli_errors:
error = cli_errors[type(err)].enrich(additional_message=str(err))
if error.soft_fail is True:
ctx.obj['soft_fail'] = True
if return_exception:
return error
printer.print_error(error)
return None
if isinstance(err, click.ClickException):
raise err
unknown_error = CliError(code='unknown_error', message=str(err))
if return_exception:
return unknown_error
printer.print_error(unknown_error)
raise typer.Exit(1)