From 94cbc629cd0a8c660688637870c71d7db3679cf4 Mon Sep 17 00:00:00 2001 From: Wu Clan Date: Wed, 24 Jan 2024 10:17:20 +0800 Subject: [PATCH] Fix custom validator exception serialization in dev mode (#278) * Fix custom validator exception serialization in dev mode * Fix formatting judgment conditions --- backend/app/common/exception/exception_handler.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/backend/app/common/exception/exception_handler.py b/backend/app/common/exception/exception_handler.py index c702ecae..60444e73 100644 --- a/backend/app/common/exception/exception_handler.py +++ b/backend/app/common/exception/exception_handler.py @@ -31,7 +31,15 @@ async def _validation_exception_handler(request: Request, e: RequestValidationEr custom_message = CUSTOM_VALIDATION_ERROR_MESSAGES.get(error['type']) if custom_message: ctx = error.get('ctx') - error['msg'] = custom_message.format(**ctx) if ctx else custom_message + if not ctx: + error['msg'] = custom_message + else: + error['msg'] = custom_message.format(**ctx) + ctx_error = ctx.get('error') + if ctx_error: + error['ctx']['error'] = ( + ctx_error.__str__().replace("'", '"') if isinstance(ctx_error, Exception) else None + ) errors.append(error) error = errors[0] if error.get('type') == 'json_invalid':