20 lines
544 B
Python
20 lines
544 B
Python
import logging
|
|
|
|
from django.http import JsonResponse
|
|
from django.utils.deprecation import MiddlewareMixin
|
|
|
|
from utils.json_response import Response
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CustomExceptionMiddleware(MiddlewareMixin):
|
|
def process_exception(self, request, exception):
|
|
"""
|
|
捕获所有异常并处理
|
|
"""
|
|
# 日志记录
|
|
logger.error(f"Unhandled Exception: {str(exception)}", exc_info=True)
|
|
|
|
return JsonResponse(Response(message=str(exception), status=500).data, status=500)
|