From 066a6a4e9be549f5bc79d8438acf46b317cb29ea Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Sat, 24 Feb 2024 20:07:35 +0100 Subject: [PATCH] add exception handling for UnicodeDecodeError in handle_exceptions function --- rendercv/cli.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/rendercv/cli.py b/rendercv/cli.py index 0f488e6..11efa1a 100644 --- a/rendercv/cli.py +++ b/rendercv/cli.py @@ -303,6 +303,19 @@ def handle_exceptions(function: Callable) -> Callable: error("There is a YAML error in the input file!", e) except FileNotFoundError as e: error(e) + except UnicodeDecodeError as e: + # find the problematic character that cannot be decoded with utf-8 + bad_character = str(e.object[e.start : e.end]) + try: + bad_character_context = str(e.object[e.start - 16 : e.end + 16]) + except IndexError: + bad_character_context = "" + + error( + "The input file contains a character that cannot be decoded with" + f" UTF-8 ({bad_character}):\n {bad_character_context}", + ) + except ValueError as e: error(e) except RuntimeError as e: @@ -405,11 +418,14 @@ def cli_command_render( str, typer.Argument(help="Path to the YAML input file as a string"), ], - use_local_latex: bool = typer.Option( - False, - "--use-local-latex", - help="Use the local LaTeX installation instead of the RenderCV's TinyTeX.", - ), + use_local_latex: Annotated[ + bool, + typer.Option( + False, + "--use-local-latex", + help="Use the local LaTeX installation instead of the RenderCV's TinyTeX.", + ), + ] = False, ): """Generate a $\\LaTeX$ CV from a YAML input file.