better exception
This commit is contained in:
parent
7a215dc457
commit
f5ce799a38
1 changed files with 18 additions and 3 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()
|
||||||
|
|
||||||
|
|
@ -56,6 +61,7 @@ def exception_handler(exc, context):
|
||||||
# در فایل middleware.py
|
# در فایل middleware.py
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
|
||||||
class ErrorMiddleware:
|
class ErrorMiddleware:
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
@ -76,6 +82,7 @@ class ErrorMiddleware:
|
||||||
status=500
|
status=500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
@ -86,8 +93,16 @@ class UnprocessableEntity(APIException):
|
||||||
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'
|
||||||
|
|
||||||
|
|
||||||
class ServiceUnavailable(APIException):
|
class ServiceUnavailable(APIException):
|
||||||
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||||
default_ = _('SERVICE_UNAVAILABLE')
|
default_ = _('SERVICE_UNAVAILABLE')
|
||||||
default_code = 'service_unavailable'
|
default_code = 'service_unavailable'
|
||||||
default_detail = 'Service Unavailable'
|
default_detail = 'Service Unavailable'
|
||||||
|
|
||||||
|
|
||||||
|
class Conflict(APIException):
|
||||||
|
status_code = status.HTTP_409_CONFLICT
|
||||||
|
default_ = _('CONFLICT')
|
||||||
|
default_code = 'conflict'
|
||||||
|
default_detail = 'Conflict'
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue