Skip to content

Commit d295686

Browse files
committed
Update pydantic error message translation
1 parent d01e04d commit d295686

File tree

2 files changed

+15
-112
lines changed

2 files changed

+15
-112
lines changed

backend/common/exception/exception_handler.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from uvicorn.protocols.http.h11_impl import STATUS_PHRASES
99

1010
from backend.common.exception.errors import BaseExceptionMixin
11-
from backend.common.i18n import t
11+
from backend.common.i18n import i18n, t
1212
from backend.common.response.response_code import CustomResponseCode, StandardResponseCode
1313
from backend.common.response.response_schema import response_base
1414
from backend.core.conf import settings
@@ -44,18 +44,20 @@ async def _validation_exception_handler(request: Request, exc: RequestValidation
4444
"""
4545
errors = []
4646
for error in exc.errors():
47-
custom_message = t(f'pydantic.{error["type"]}')
48-
if custom_message:
49-
ctx = error.get('ctx')
50-
if not ctx:
51-
error['msg'] = custom_message
52-
else:
53-
ctx_error = ctx.get('error')
54-
if ctx_error:
55-
error['msg'] = custom_message.format(**ctx)
56-
error['ctx']['error'] = (
57-
ctx_error.__str__().replace("'", '"') if isinstance(ctx_error, Exception) else None
58-
)
47+
# 非 en-US 语言下,使用自定义错误信息
48+
if i18n.current_language != 'en-US':
49+
custom_message = t(f'pydantic.{error["type"]}')
50+
if custom_message:
51+
ctx = error.get('ctx')
52+
if not ctx:
53+
error['msg'] = custom_message
54+
else:
55+
ctx_error = ctx.get('error')
56+
if ctx_error:
57+
error['msg'] = custom_message.format(**ctx)
58+
error['ctx']['error'] = (
59+
ctx_error.__str__().replace("'", '"') if isinstance(ctx_error, Exception) else None
60+
)
5961
errors.append(error)
6062
error = errors[0]
6163
if error.get('type') == 'json_invalid':

backend/locale/en-US.json

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -142,105 +142,6 @@
142142
"size_exceeded": "Video exceeds maximum limit, please reselect"
143143
}
144144
},
145-
"pydantic": {
146-
"arguments_type": "Arguments must be a tuple, list or dict",
147-
"assertion_error": "Assertion failed, {error}",
148-
"bool_parsing": "Input should be a valid boolean, unable to interpret input",
149-
"bool_type": "Input should be a valid boolean",
150-
"bytes_too_long": "Data should have at most {max_length} bytes",
151-
"bytes_too_short": "Data should have at least {min_length} bytes",
152-
"bytes_type": "Input should be valid bytes",
153-
"callable_type": "Input should be a callable object",
154-
"dataclass_exact_type": "Input should be an instance of {class_name}",
155-
"dataclass_type": "Input should be a dict or an instance of {class_name}",
156-
"date_from_datetime_inexact": "Datetime provided to date should have zero time - e.g. be an exact date",
157-
"date_from_datetime_parsing": "Input should be a valid date or datetime, {error}",
158-
"date_future": "Date should be in the future",
159-
"date_past": "Date should be in the past",
160-
"date_parsing": "Input should be a valid date in YYYY-MM-DD format, {error}",
161-
"date_type": "Input should be a valid date",
162-
"datetime_future": "Input should be in the future",
163-
"datetime_object_invalid": "Invalid datetime object, got {error}",
164-
"datetime_past": "Input should be in the past",
165-
"datetime_parsing": "Input should be a valid datetime, {error}",
166-
"datetime_type": "Input should be a valid datetime",
167-
"decimal_max_digits": "Decimal input should have no more than {max_digits} digits in total",
168-
"decimal_max_places": "Decimal input should have no more than {decimal_places} decimal places",
169-
"decimal_parsing": "Input should be a valid decimal number",
170-
"decimal_type": "Decimal input should be an integer, float, string or Decimal object",
171-
"decimal_whole_digits": "Decimal input should have no more than {whole_digits} digits before the decimal point",
172-
"dict_type": "Input should be a valid dict",
173-
"email_parsing": "Input should be a valid email address, {error}",
174-
"email_type": "Input should be a valid email address",
175-
"enum": "Input should be {expected}",
176-
"extra_forbidden": "Extra inputs are not allowed",
177-
"finite_number": "Input should be a finite number",
178-
"float_parsing": "Input should be a valid number, unable to parse string as number",
179-
"float_type": "Input should be a valid number",
180-
"frozen_field": "Field is frozen",
181-
"frozen_instance": "Instance is frozen",
182-
"frozen_set_type": "Input should be a valid frozenset",
183-
"get_attribute_error": "Error getting attribute: {error}",
184-
"greater_than": "Input should be greater than {gt}",
185-
"greater_than_equal": "Input should be greater than or equal to {ge}",
186-
"int_from_float": "Input should be a valid integer, got a number with a decimal part",
187-
"int_parsing": "Input should be a valid integer, unable to parse string as integer",
188-
"int_parsing_size": "Unable to parse input string as integer, exceeds maximum size",
189-
"int_type": "Input should be a valid integer",
190-
"invalid_key": "Key should be a string",
191-
"is_instance_of": "Input should be an instance of {class}",
192-
"is_subclass_of": "Input should be a subclass of {class}",
193-
"iteration_error": "Error iterating object, error: {error}",
194-
"iterable_type": "Input should be iterable",
195-
"json_invalid": "Invalid JSON: {error}",
196-
"json_type": "JSON input should be a string, bytes or bytearray",
197-
"less_than": "Input should be less than {lt}",
198-
"less_than_equal": "Input should be less than or equal to {le}",
199-
"list_type": "Input should be a valid list",
200-
"literal_error": "Input should be {expected}",
201-
"mapping_type": "Input should be a valid mapping, error: {error}",
202-
"missing": "Field is required",
203-
"missing_argument": "Missing required argument",
204-
"missing_keyword_only_argument": "Missing required keyword-only argument",
205-
"missing_positional_only_argument": "Missing required positional-only argument",
206-
"model_attributes_type": "Input should be a valid dict or object with extractable fields",
207-
"model_type": "Input should be a valid dict or an instance of {class_name}",
208-
"multiple_argument_values": "Multiple values provided for argument",
209-
"multiple_of": "Input should be a multiple of {multiple_of}",
210-
"no_such_attribute": "Object has no attribute '{attribute}'",
211-
"none_required": "Input should be None",
212-
"recursion_loop": "Recursion error - circular reference detected",
213-
"set_type": "Input should be a valid set",
214-
"string_pattern_mismatch": "String should match pattern '{pattern}'",
215-
"string_sub_type": "Input should be a string, not an instance of a str subclass",
216-
"string_too_long": "String should have at most {max_length} characters",
217-
"string_too_short": "String should have at least {min_length} characters",
218-
"string_type": "Input should be a valid string",
219-
"string_unicode": "Input should be a valid string, unable to parse raw data as Unicode string",
220-
"time_delta_parsing": "Input should be a valid timedelta, {error}",
221-
"time_delta_type": "Input should be a valid timedelta",
222-
"time_parsing": "Input should be a valid time format, {error}",
223-
"time_type": "Input should be a valid time",
224-
"timezone_aware": "Input should contain timezone information",
225-
"timezone_naive": "Input should not contain timezone information",
226-
"timezone_offset": "Timezone offset should be {tz_expected}, got {tz_actual}",
227-
"too_long": "{field_type} should have at most {max_length} items after validation, not {actual_length}",
228-
"too_short": "{field_type} should have at least {min_length} items after validation, not {actual_length}",
229-
"tuple_type": "Input should be a valid tuple",
230-
"union_tag_invalid": "Input tag '{tag}' found using {discriminator} does not match any expected tags: {expected_tags}",
231-
"union_tag_not_found": "Unable to extract tag using discriminator {discriminator}",
232-
"unexpected_keyword_argument": "Unexpected keyword argument",
233-
"unexpected_positional_argument": "Unexpected positional argument",
234-
"url_parsing": "Input should be a valid URL, {error}",
235-
"url_scheme": "URL scheme should be {expected_schemes}",
236-
"url_syntax_violation": "Input violates strict URL syntax rules, {error}",
237-
"url_too_long": "URL should have at most {max_length} characters",
238-
"url_type": "URL input should be a string or URL",
239-
"uuid_parsing": "Input should be a valid UUID, {error}",
240-
"uuid_type": "UUID input should be a string, bytes or UUID object",
241-
"uuid_version": "Expected UUID version to be {expected_version}",
242-
"value_error": "Value error, {error}"
243-
},
244145
"response": {
245146
"error": "Request error",
246147
"server_error": "Server internal error",

0 commit comments

Comments
 (0)