Coverage for /usr/local/lib/python3.12/site-packages/prefect/cli/_utilities.py: 46%
33 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-12-05 11:21 +0000
1"""
2Utilities for Prefect CLI commands
3"""
5from __future__ import annotations 1a
7import functools 1a
8import traceback 1a
9from typing import Any, Callable, NoReturn 1a
11import typer 1a
12from click.exceptions import ClickException 1a
14from prefect.exceptions import MissingProfileError 1a
15from prefect.settings import get_current_settings 1a
18def exit_with_error(message: str | Exception, code: int = 1, **kwargs: Any) -> NoReturn: 1a
19 """
20 Utility to print a stylized error message and exit with a non-zero code
21 """
22 from prefect.cli.root import app
24 kwargs.setdefault("style", "red")
25 app.console.print(message, **kwargs)
26 raise typer.Exit(code)
29def exit_with_success(message: str, **kwargs: Any) -> NoReturn: 1a
30 """
31 Utility to print a stylized success message and exit with a zero code
32 """
33 from prefect.cli.root import app
35 kwargs.setdefault("style", "green")
36 app.console.print(message, **kwargs)
37 raise typer.Exit(0)
40def with_cli_exception_handling(fn: Callable[..., Any]) -> Callable[..., Any]: 1a
41 @functools.wraps(fn) 1a
42 def wrapper(*args: Any, **kwargs: Any) -> Any: 1a
43 try: 1a
44 return fn(*args, **kwargs) 1a
45 except (typer.Exit, typer.Abort, ClickException):
46 raise # Do not capture click or typer exceptions
47 except MissingProfileError as exc:
48 exit_with_error(exc)
49 except Exception:
50 if get_current_settings().testing.test_mode:
51 raise # Reraise exceptions during test mode
52 traceback.print_exc()
53 exit_with_error("An exception occurred.")
55 return wrapper 1a