Replies: 2 comments
-
I dont think it's possible to customize to that level since what you get in the extra/message comes from msgspec under the hood (someone may correct me on this Im not 100% sure), |
Beta Was this translation helpful? Give feedback.
-
You can use custom exception handler to achive this: app = Litestar(
exception_handlers=[...], # your exception handler
) Notice that once you use this option, the defaul exception handlers (404, 405 etc.) and unknown exception handler will no longer be available :( , so you should manually process them, like this: # imports omitted
def not_found_exception_handler(_: Request, __: NotFoundException) -> Response:
return Response(b"", status_code=HTTP_404_NOT_FOUND)
def method_not_allowed_exception_handler(
_: Request, __: MethodNotAllowedException
) -> Response:
return Response(b"", status_code=HTTP_405_METHOD_NOT_ALLOWED)
def unknown_exception_handler(request: Request, exception: Exception) -> Response:
... # your customized logic
EXCEPTION_HANDLERS: ExceptionHandlersMap = {
NotFoundException: not_found_exception_handler,
MethodNotAllowedException: method_not_allowed_exception_handler,
Exception: unknown_exception_handler,
} And then, add def validation_exception_handler(
request: Request, exception: ValidationException
) -> Response:
... # your customized logic |
Beta Was this translation helpful? Give feedback.
-
The above is a test code. When the string passed in the current segment is greater than 5, the verification fails. The failure message is not user-friendly. Is there any way to customize it and return a user-friendly prompt message? In China, I will return a "user name length cannot exceed 5 characters" or “用户名称不能超过5个字符串”
![image](https://private-user-images.githubusercontent.com/131358285/405461691-d23a98a8-f73c-4737-bae6-3d0074b3a5e9.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkwMzI4NzUsIm5iZiI6MTczOTAzMjU3NSwicGF0aCI6Ii8xMzEzNTgyODUvNDA1NDYxNjkxLWQyM2E5OGE4LWY3M2MtNDczNy1iYWU2LTNkMDA3NGIzYTVlOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjA4JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIwOFQxNjM2MTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1lM2NjMmNjMmEwMzgyZjc5ZGVkNmMxZmM1ZGM1OTgzOWVlYjBmZmJkZTY4MDZmNzJhNDVhZjc4ZjI1MGY1YjJmJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.41ltaqJAj5X-m-s5JrQnCCVrHqfcA5WFwv8xE--_CpA)
You can change the message field in the screenshot above to a custom message.
Beta Was this translation helpful? Give feedback.
All reactions