cli: refactor

This commit is contained in:
Sina Atalay 2024-05-05 17:13:02 +03:00
parent 53d8328e07
commit d349171f22
1 changed files with 10 additions and 4 deletions

View File

@ -157,7 +157,7 @@ def handle_validation_error(exception: pydantic.ValidationError):
"Field required": "This field is required!", "Field required": "This field is required!",
"value is not a valid phone number": "This is not a valid phone number!", "value is not a valid phone number": "This is not a valid phone number!",
"month must be in 1..12": "The month must be between 1 and 12!", "month must be in 1..12": "The month must be between 1 and 12!",
"Value error, day is out of range for month": ( "day is out of range for month": (
"The day is out of range for the month!" "The day is out of range for the month!"
), ),
"Extra inputs are not permitted": ( "Extra inputs are not permitted": (
@ -169,6 +169,11 @@ def handle_validation_error(exception: pydantic.ValidationError):
), ),
} }
unwanted_texts = [
"value is not a valid email address: ",
"Value error, "
]
# Check if this is a section error. If it is, we need to handle it differently. # Check if this is a section error. If it is, we need to handle it differently.
# This is needed because how dm.validate_section_input function raises an exception. # This is needed because how dm.validate_section_input function raises an exception.
# This is done to tell the user which which EntryType RenderCV excepts to see. # This is done to tell the user which which EntryType RenderCV excepts to see.
@ -226,14 +231,15 @@ def handle_validation_error(exception: pydantic.ValidationError):
location = f"{location}.{custom_location}" location = f"{location}.{custom_location}"
input = custom_input_value input = custom_input_value
# Don't show unwanted texts in the error message:
for unwanted_text in unwanted_texts:
message = message.replace(unwanted_text, "")
# Convert the error message to a more user-friendly message if it's in the # Convert the error message to a more user-friendly message if it's in the
# error_dictionary: # error_dictionary:
if message in error_dictionary: if message in error_dictionary:
message = error_dictionary[message] message = error_dictionary[message]
# Don't show "Value error, ", since the message is already clear.
message = message.replace("Value error, ", "")
# Special case for end_date because Pydantic returns multiple end_date errors # Special case for end_date because Pydantic returns multiple end_date errors
# since it has multiple valid formats: # since it has multiple valid formats:
if "end_date." in location: if "end_date." in location: