From 270479857dc9363b5048a05306702096bc8d5eb4 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Sat, 28 Oct 2023 20:32:05 +0200 Subject: [PATCH] improve logger --- rendercv/__main__.py | 30 +++++++++++++++++++++++------- rendercv/data_model.py | 2 +- rendercv/rendering.py | 9 ++++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/rendercv/__main__.py b/rendercv/__main__.py index b42d3a2..d960b38 100644 --- a/rendercv/__main__.py +++ b/rendercv/__main__.py @@ -10,6 +10,7 @@ import typer from jinja2 import Environment, PackageLoader from pydantic import ValidationError from pydantic_core import ErrorDetails +from ruamel.yaml.parser import ParserError logger = logging.getLogger(__name__) @@ -63,7 +64,7 @@ def user_friendly_errors(func: Callable) -> Callable: # Remove url: error["url"] = None - # Make sure the entries of loc are strings + # Make sure the entries of loc (location) are strings error["loc"] = [str(loc) for loc in error["loc"]] # Assign a custom error message if there is one @@ -75,12 +76,17 @@ def user_friendly_errors(func: Callable) -> Callable: if custom_message: ctx = error.get("ctx") - if ctx: - if ctx.get("error"): - error["msg"] = ctx["error"].args[0] - else: - error["msg"] = custom_message.format(**ctx) + ctx_error = ctx.get("error") if ctx else None + if ctx_error: + # This means that there is a custom validation error that + # comes from data_model.py + error["msg"] = ctx["error"].args[0] + elif ctx: + # Some Pydantic errors have a context, see the custom message + # for "literal_error" above + error["msg"] = custom_message.format(**ctx) else: + # If there is no context, just use the custom message error["msg"] = custom_message if error["input"] is not None: @@ -108,9 +114,19 @@ def user_friendly_errors(func: Callable) -> Callable: error_message = "\n\n ".join(error_messages) logger.error(error_message) + except ParserError as e: + # It is a YAML parser error + new_args = list(e.args) + new_args = [str(arg).strip() for arg in new_args] + new_args[0] = "There is a problem with your input file 🤦‍" + error_message = "\n\n ".join(new_args) + logger.error(error_message) + except Exception as e: # It is not a Pydantic error - logging.error(e) + new_args = list(e.args) + error_message = "\n\n ".join(new_args) + logger.error(error_message) return wrapper diff --git a/rendercv/data_model.py b/rendercv/data_model.py index 6445363..da6b226 100644 --- a/rendercv/data_model.py +++ b/rendercv/data_model.py @@ -1455,7 +1455,7 @@ def read_input_file(file_path: str) -> RenderCVDataModel: # check if the file exists: if not os.path.exists(file_path): - raise FileNotFoundError(f"The file {file_path} doesn't exist!") + raise FileNotFoundError(f"The file {file_path} doesn't exist 🙄") # check the file extension: accepted_extensions = [".yaml", ".yml", ".json", ".json5"] diff --git a/rendercv/rendering.py b/rendercv/rendering.py index bdc020b..80cbb0f 100644 --- a/rendercv/rendering.py +++ b/rendercv/rendering.py @@ -409,9 +409,12 @@ def run_latex(latex_file_path: str) -> str: break raise RuntimeError( - "Running TinyTeX has failed with the following error:\n\n" - f"{error_line}\n\nIf you can't solve the problem, please try to" - " re-install RenderCV, or open an issue on GitHub." + "Running TinyTeX has failed with the following error:", + f"{error_line}", + ( + "If you can't solve the problem, please try to re-install RenderCV," + " or open an issue on GitHub." + ), ) # check if the PDF file is generated: