mirror of https://github.com/eyhc1/rendercv.git
improve user_communicator
This commit is contained in:
parent
c33875240c
commit
1aeebcaa04
|
@ -1,4 +1,4 @@
|
||||||
from typing import Callable
|
from typing import Callable, Optional
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import rich
|
import rich
|
||||||
|
@ -49,31 +49,59 @@ def information(text):
|
||||||
console.print(f"[bold green]{text}")
|
console.print(f"[bold green]{text}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_error_message_and_location_and_value_from_a_custom_error(
|
||||||
|
error_string: str,
|
||||||
|
) -> Optional[tuple[str, str, str]]:
|
||||||
|
pattern = r"\('(.*)', '(.*)', '(.*)'\)"
|
||||||
|
match = re.search(pattern, error_string)
|
||||||
|
if match:
|
||||||
|
return match.group(1), match.group(2), match.group(3)
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def handle_validation_error(exception: pydantic.ValidationError):
|
def handle_validation_error(exception: pydantic.ValidationError):
|
||||||
error_dictionary: dict[str, str] = {
|
error_dictionary: dict[str, str] = {
|
||||||
r"Value error, Invalid isoformat string: '(.*)'": (
|
"Input should be 'present'": (
|
||||||
"This is not a valid date! Please use either YYYY-MM-DD, YYYY-MM, or"
|
"This is not a valid date! Please use either YYYY-MM-DD, YYYY-MM, or YYYY"
|
||||||
" YYYY format."
|
' format or "present"!'
|
||||||
),
|
),
|
||||||
r"URL scheme should be 'http' or 'https'": "This is not a valid URL!",
|
"Input should be a valid integer, unable to parse string as an integer": (
|
||||||
r"Field( )required": "This field is required!",
|
"This is not a valid date! Please use either YYYY-MM-DD, YYYY-MM, or YYYY"
|
||||||
r"value is not a valid phone number": "This is not a valid phone number!",
|
" format!"
|
||||||
r"String should match pattern '\\d\+\\\.\?\\d\* \*\(cm\|in\|pt\|mm\|ex\|em\)'": (
|
|
||||||
'This is not a valid dimension! For example, use "2 cm" or "3.5 in"'
|
|
||||||
),
|
),
|
||||||
|
"String should match pattern '\\d{4}-\\d{2}(-\\d{2})?'": (
|
||||||
|
"This is not a valid date! Please use either YYYY-MM-DD, YYYY-MM, or YYYY"
|
||||||
|
" format!"
|
||||||
|
),
|
||||||
|
"URL scheme should be 'http' or 'https'": "This is not a valid URL!",
|
||||||
|
"Field required": "This field is required!",
|
||||||
|
"value is not a valid phone number": "This is not a valid phone number!",
|
||||||
}
|
}
|
||||||
new_errors: list[pydantic_core.ErrorDetails] = []
|
new_errors: list[dict[str, str]] = []
|
||||||
for error_object in exception.errors():
|
for error_object in exception.errors():
|
||||||
for key, value in error_dictionary.items():
|
message = error_object["msg"]
|
||||||
match = re.match(key, error_object["msg"])
|
location = ".".join([str(loc) for loc in error_object["loc"]])
|
||||||
if match:
|
input = error_object["input"]
|
||||||
error_object["msg"] = value
|
|
||||||
try:
|
|
||||||
error_object["input"] = match.group(1)
|
|
||||||
except IndexError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
new_errors.append(error_object)
|
custom_error = get_error_message_and_location_and_value_from_a_custom_error(
|
||||||
|
message
|
||||||
|
)
|
||||||
|
if custom_error is None:
|
||||||
|
if message in error_dictionary:
|
||||||
|
message = error_dictionary[message]
|
||||||
|
else:
|
||||||
|
message = message
|
||||||
|
else:
|
||||||
|
message = custom_error[0]
|
||||||
|
location = f"{location}.{custom_error[1]}"
|
||||||
|
input = custom_error[2]
|
||||||
|
|
||||||
|
new_errors.append({
|
||||||
|
"loc": str(location),
|
||||||
|
"msg": message,
|
||||||
|
"input": str(input),
|
||||||
|
})
|
||||||
|
|
||||||
table = rich.table.Table(
|
table = rich.table.Table(
|
||||||
title="[bold red]\nThere are some errors in the input file!\n",
|
title="[bold red]\nThere are some errors in the input file!\n",
|
||||||
|
@ -86,7 +114,7 @@ def handle_validation_error(exception: pydantic.ValidationError):
|
||||||
|
|
||||||
for error_object in new_errors:
|
for error_object in new_errors:
|
||||||
table.add_row(
|
table.add_row(
|
||||||
".".join([str(i) for i in error_object["loc"]]),
|
error_object["loc"],
|
||||||
error_object["input"],
|
error_object["input"],
|
||||||
error_object["msg"],
|
error_object["msg"],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue