67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
@author: 猿小天
|
|
@contact: QQ:1638245306
|
|
@Created on: 2021/6/2 002 16:06
|
|
@Remark: 自定义异常处理
|
|
"""
|
|
import logging
|
|
import traceback
|
|
|
|
from django.db.models import ProtectedError
|
|
from django.http import Http404
|
|
from rest_framework.exceptions import APIException as DRFAPIException, AuthenticationFailed
|
|
from rest_framework.status import HTTP_407_PROXY_AUTHENTICATION_REQUIRED, HTTP_401_UNAUTHORIZED
|
|
from rest_framework.views import set_rollback, exception_handler
|
|
|
|
from dvadmin.utils.json_response import ErrorResponse
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def CustomExceptionHandler(ex, context):
|
|
"""
|
|
统一异常拦截处理
|
|
目的:(1)取消所有的500异常响应,统一响应为标准错误返回
|
|
(2)准确显示错误信息
|
|
:param ex:
|
|
:param context:
|
|
:return:
|
|
"""
|
|
msg = ''
|
|
code = 4000
|
|
# 调用默认的异常处理函数
|
|
response = exception_handler(ex, context)
|
|
if isinstance(ex, AuthenticationFailed):
|
|
# 如果是身份验证错误
|
|
if response and response.data.get('detail') =="Given token not valid for any token type":
|
|
code = 401
|
|
msg = ex.detail
|
|
elif response and response.data.get('detail') =="Token is blacklisted":
|
|
# token在黑名单
|
|
return ErrorResponse(status=HTTP_401_UNAUTHORIZED)
|
|
else:
|
|
code = 401
|
|
msg = ex.detail
|
|
elif isinstance(ex,Http404):
|
|
code = 400
|
|
msg = "接口地址不正确"
|
|
elif isinstance(ex, DRFAPIException):
|
|
set_rollback()
|
|
msg = ex.detail
|
|
if isinstance(msg,dict):
|
|
for k, v in msg.items():
|
|
for i in v:
|
|
msg = "%s:%s" % (k, i)
|
|
elif isinstance(ex, ProtectedError):
|
|
set_rollback()
|
|
msg = "无法删除:该条数据与其他数据有相关绑定"
|
|
# elif isinstance(ex, DatabaseError):
|
|
# set_rollback()
|
|
# msg = "接口服务器异常,请联系管理员"
|
|
elif isinstance(ex, Exception):
|
|
logger.exception(traceback.format_exc())
|
|
msg = str(ex)
|
|
return ErrorResponse(msg=msg, code=code)
|