fix(api): Convert Django Exceptions to DRF API Exception equivilent

ref: #849 #854
This commit is contained in:
2025-07-04 12:58:02 +09:30
parent 1f4ce92623
commit 77cf598e4d

View File

@ -1,5 +1,7 @@
import django
import importlib
import logging
import rest_framework
from django.utils.safestring import mark_safe
@ -88,21 +90,12 @@ class Create(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -145,21 +138,12 @@ class Destroy(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -203,21 +187,12 @@ class List(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -264,21 +239,12 @@ class Retrieve(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -345,21 +311,12 @@ class Update(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -421,21 +378,12 @@ class Update(
if not isinstance(e, APIException):
response = Response(
data = {
'server_error': str(e)
},
status = 501
)
e = self._django_to_api_exception(e)
self.get_log().exception(e)
else:
response = Response(
data = str(e.get_full_details()['message']),
status = e.status_code
)
response = Response(
data = e.get_full_details(),
status = e.status_code
)
return response
@ -454,6 +402,37 @@ class CommonViewSet(
viewsets (class): Django Rest Framework base class.
"""
def _django_to_api_exception( self, exc ):
"""Convert Django exception to DRF Exception
Args:
exc (Django.core.exceptions.*): Django exception to convert
Raises:
rest_framework.exceptions.ValidationError: Exception to return
Returns:
None: Exception not converted
"""
rtn_exception = None
if isinstance(exc, django.core.exceptions.ValidationError):
try:
raise rest_framework.exceptions.ValidationError(exc.error_dict)
except Exception as e:
return e
return None
@property
def allowed_methods(self):
"""Allowed HTTP Methods