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
|
from rest_framework.views import exception_handler as drf_exception_handler
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def exception_handler(exc, context):
|
def exception_handler(exc, context):
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
# پاسخ پیشفرض DRF را دریافت میکنیم
|
# پاسخ پیشفرض DRF را دریافت میکنیم
|
||||||
|
|
@ -15,8 +17,8 @@ def exception_handler(exc, context):
|
||||||
response_data = {
|
response_data = {
|
||||||
'success': False,
|
'success': False,
|
||||||
'status_code': response.status_code,
|
'status_code': response.status_code,
|
||||||
'status_message': str(exc.default_detail),
|
'status_message': str(getattr(exc, 'default_detail', exc)),
|
||||||
'details': exc.detail,
|
'details': getattr(exc, 'detail', None),
|
||||||
}
|
}
|
||||||
|
|
||||||
if isinstance(exc, APIException):
|
if isinstance(exc, APIException):
|
||||||
|
|
@ -44,7 +46,10 @@ def exception_handler(exc, context):
|
||||||
# message = error
|
# message = error
|
||||||
#
|
#
|
||||||
# response_data['message'] = message
|
# 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']['error'] = error
|
||||||
response_data['details']['timestamp'] = timezone.now().isoformat()
|
response_data['details']['timestamp'] = timezone.now().isoformat()
|
||||||
|
|
||||||
|
|
@ -84,7 +89,7 @@ from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
class UnprocessableEntity(APIException):
|
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_detail = 'The request was well-formed but cannot be processed due to semantic errors.'
|
||||||
default_code = 'unprocessable_entity'
|
default_code = 'unprocessable_entity'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue