2025-06-16 08:29:17 +03:30
|
|
|
from rest_framework.exceptions import APIException
|
2025-08-31 11:48:14 +03:30
|
|
|
from django.http import JsonResponse
|
2025-06-16 08:29:17 +03:30
|
|
|
from rest_framework import status
|
2025-05-24 15:01:55 +03:30
|
|
|
from django.db import connection
|
2025-09-03 15:32:11 +03:30
|
|
|
from Rasaddam_Backend import settings
|
2025-08-31 11:48:14 +03:30
|
|
|
import traceback
|
2025-05-24 15:01:55 +03:30
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
2025-08-31 11:48:14 +03:30
|
|
|
class Json500Middleware:
|
|
|
|
|
""" return all 500 status errors in json response """
|
2025-05-24 15:01:55 +03:30
|
|
|
|
|
|
|
|
def __init__(self, get_response):
|
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
2025-08-31 11:48:14 +03:30
|
|
|
def __call__(self, request, *args, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
return self.get_response(request)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
|
|
|
|
if settings.DEBUG:
|
|
|
|
|
raise
|
2025-05-24 15:01:55 +03:30
|
|
|
|
2025-08-31 11:48:14 +03:30
|
|
|
return JsonResponse({
|
|
|
|
|
"message": str(exc),
|
|
|
|
|
"status_code": 500,
|
|
|
|
|
"error_type": exc.__class__.__name__,
|
|
|
|
|
"traceback": traceback.format_exc().splitlines(),
|
|
|
|
|
}, status=500)
|