improve logger

This commit is contained in:
Sina Atalay 2023-10-28 20:32:05 +02:00
parent 9352ba2bb9
commit 270479857d
3 changed files with 30 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import typer
from jinja2 import Environment, PackageLoader from jinja2 import Environment, PackageLoader
from pydantic import ValidationError from pydantic import ValidationError
from pydantic_core import ErrorDetails from pydantic_core import ErrorDetails
from ruamel.yaml.parser import ParserError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -63,7 +64,7 @@ def user_friendly_errors(func: Callable) -> Callable:
# Remove url: # Remove url:
error["url"] = None 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"]] error["loc"] = [str(loc) for loc in error["loc"]]
# Assign a custom error message if there is one # Assign a custom error message if there is one
@ -75,12 +76,17 @@ def user_friendly_errors(func: Callable) -> Callable:
if custom_message: if custom_message:
ctx = error.get("ctx") ctx = error.get("ctx")
if ctx: ctx_error = ctx.get("error") if ctx else None
if ctx.get("error"): if ctx_error:
error["msg"] = ctx["error"].args[0] # This means that there is a custom validation error that
else: # comes from data_model.py
error["msg"] = custom_message.format(**ctx) 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: else:
# If there is no context, just use the custom message
error["msg"] = custom_message error["msg"] = custom_message
if error["input"] is not None: if error["input"] is not None:
@ -108,9 +114,19 @@ def user_friendly_errors(func: Callable) -> Callable:
error_message = "\n\n ".join(error_messages) error_message = "\n\n ".join(error_messages)
logger.error(error_message) 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: except Exception as e:
# It is not a Pydantic error # 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 return wrapper

View File

@ -1455,7 +1455,7 @@ def read_input_file(file_path: str) -> RenderCVDataModel:
# check if the file exists: # check if the file exists:
if not os.path.exists(file_path): 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: # check the file extension:
accepted_extensions = [".yaml", ".yml", ".json", ".json5"] accepted_extensions = [".yaml", ".yml", ".json", ".json5"]

View File

@ -409,9 +409,12 @@ def run_latex(latex_file_path: str) -> str:
break break
raise RuntimeError( raise RuntimeError(
"Running TinyTeX has failed with the following error:\n\n" "Running TinyTeX has failed with the following error:",
f"{error_line}\n\nIf you can't solve the problem, please try to" f"{error_line}",
" re-install RenderCV, or open an issue on GitHub." (
"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: # check if the PDF file is generated: