From 35ea9941a227e81d4158b409defb439daf1597a8 Mon Sep 17 00:00:00 2001 From: Sanjay Sikdar <52757722+sannjayy@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:07:27 +0530 Subject: [PATCH] Update exceptionhandler.py --- utils/exceptionhandler.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/utils/exceptionhandler.py b/utils/exceptionhandler.py index e58d3c5..eaca14d 100644 --- a/utils/exceptionhandler.py +++ b/utils/exceptionhandler.py @@ -40,5 +40,20 @@ def _handle_authentication_error(exc, context, response): return response -def _handle_generic_error(exc, context, response): +def _handle_generic_error(exc, context, response): + if isinstance(exc, ValidationError): + # Access the `detail` attribute for structured error information + error_detail = exc.detail + if isinstance(error_detail, list) and len(error_detail) > 0 and isinstance(error_detail[0], ErrorDetail): + error_detail = str(error_detail[0]) + else: + error_detail = str(error_detail) + else: + # For non-ValidationError exceptions, fallback to string + error_detail = str(exc) + + response.data = { + 'success': response.status_code in [200, 201], + 'detail': error_detail + } return response