better exception

This commit is contained in:
Sayyid Hamid Mahdavi 2026-04-25 02:10:47 +03:30
parent c35accc8ac
commit 3dd62a0173

View file

@ -6,6 +6,8 @@ from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler as drf_exception_handler
logger = logging.getLogger(__name__)
def exception_handler(exc, context):
logger.exception(exc)
# پاسخ پیش‌فرض DRF را دریافت می‌کنیم
@ -15,8 +17,8 @@ def exception_handler(exc, context):
response_data = {
'success': False,
'status_code': response.status_code,
'status_message': str(exc.default_detail),
'details': exc.detail,
'status_message': str(getattr(exc, 'default_detail', exc)),
'details': getattr(exc, 'detail', None),
}
if isinstance(exc, APIException):
@ -44,7 +46,10 @@ def exception_handler(exc, context):
# message = error
#
# response_data['message'] = message
response_data['details'] = exc.detail if isinstance(exc.detail, dict) else {}
if isinstance(exc.detail, dict):
response_data['details'] = exc.detail
else:
response_data['details'] = {"message": exc.detail}
response_data['details']['error'] = error
response_data['details']['timestamp'] = timezone.now().isoformat()
@ -84,7 +89,7 @@ from django.utils.translation import gettext_lazy as _
class UnprocessableEntity(APIException):
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
status_code = 422
default_detail = 'The request was well-formed but cannot be processed due to semantic errors.'
default_code = 'unprocessable_entity'