You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
808 B
Python
24 lines
808 B
Python
import logging
|
|
from rest_framework.views import exception_handler as drf_exception_handler # drf原生处理异常函数取别名
|
|
from rest_framework.views import Response
|
|
from rest_framework import status
|
|
|
|
|
|
logger = logging.getLogger('mylogger')
|
|
|
|
|
|
def exception_handler(exc, context):
|
|
# drf的exception_handler做基础处理
|
|
response = drf_exception_handler(exc, context)
|
|
# 为空,进行自定义二次处理
|
|
logger.error(str(exc))
|
|
if response is None:
|
|
# print(exc) # 错误原因
|
|
# print(context) # 错误信息
|
|
# print('%s - %s - %s' % (context['view'], context['request'].method, exc))
|
|
return Response({
|
|
'detail': '服务器错误'
|
|
}, status=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=True)
|
|
return response
|
|
|