Coverage for src / rendercv / cli / error_handler.py: 100%
16 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
1import functools
2from collections.abc import Callable
4import rich.panel
5import typer
6from rich import print
8from rendercv.exception import RenderCVUserError
11def handle_user_errors[**P](function: Callable[P, None]) -> Callable[P, None]:
12 """Decorator that catches user errors and displays friendly messages without stack traces.
14 Why:
15 CLI commands should show clean error messages for expected user errors
16 (invalid YAML, missing files) while preserving stack traces for
17 unexpected errors. This decorator wraps all command functions.
19 Example:
20 ```py
21 @app.command()
22 @handle_user_errors
23 def my_command():
24 # Any RenderCVUserError gets caught and displayed cleanly
25 pass
26 ```
28 Args:
29 function: CLI command function to wrap.
31 Returns:
32 Wrapped function with error handling.
33 """
35 @functools.wraps(function)
36 def wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
37 try:
38 return function(*args, **kwargs)
39 except RenderCVUserError as e:
40 if e.message:
41 print(
42 rich.panel.Panel(
43 e.message,
44 title="[bold red]Error[/bold red]",
45 title_align="left",
46 border_style="bold red",
47 )
48 )
49 raise typer.Exit(code=1) from e
51 return wrapper