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

1import functools 

2from collections.abc import Callable 

3 

4import rich.panel 

5import typer 

6from rich import print 

7 

8from rendercv.exception import RenderCVUserError 

9 

10 

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. 

13 

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. 

18 

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 ``` 

27 

28 Args: 

29 function: CLI command function to wrap. 

30 

31 Returns: 

32 Wrapped function with error handling. 

33 """ 

34 

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 

50 

51 return wrapper