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个字符串”
You can change the message field in the screenshot above to a custom message.
Beta Was this translation helpful? Give feedback.
All reactions