better exception
This commit is contained in:
parent
c35accc8ac
commit
3dd62a0173
1 changed files with 9 additions and 4 deletions
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue