2025-05-27 15:09:22 +03:30
|
|
|
from django.http import JsonResponse
|
|
|
|
|
from rest_framework.views import exception_handler
|
2025-08-31 11:48:14 +03:30
|
|
|
from django.conf import settings
|
|
|
|
|
import traceback
|
2025-05-27 15:09:22 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
def custom_exception_handler(exc, context):
|
|
|
|
|
response = exception_handler(exc, context)
|
|
|
|
|
|
|
|
|
|
if response is not None:
|
|
|
|
|
response.data['status_code'] = response.status_code
|
2025-06-07 09:18:27 +03:30
|
|
|
if response.data.get('detail'):
|
|
|
|
|
response.data['message'] = response.data.get('detail', str(exc))
|
|
|
|
|
del response.data['detail']
|
2025-05-27 15:09:22 +03:30
|
|
|
else:
|
2025-08-31 11:48:14 +03:30
|
|
|
if settings.DEBUG:
|
|
|
|
|
raise
|
|
|
|
|
response = JsonResponse({
|
|
|
|
|
"message": str(exc),
|
|
|
|
|
"status_code": 500,
|
|
|
|
|
"error_type": exc.__class__.__name__,
|
|
|
|
|
"traceback": traceback.format_exc().splitlines(),
|
|
|
|
|
}, status=500)
|
2025-05-27 15:09:22 +03:30
|
|
|
response.status_code = 500
|
|
|
|
|
return response
|