drf框架的异常处理

drf框架对自己内部定义的方法做了特殊处理 比如:频率控制组件 返回特殊的信息

如何在drf异常处理的基础上再重新包装异常

from rest_framework.views import exception_handler
from rest_framework.response import Response
import logging

error_log = logging.getLogger('')


def custom_exception_handler(exc, context):
    '''重定义异常(使异常返回值符合规范)'''
    # 先让drf内部做处理
    response = exception_handler(exc, context)
    # drf处理过得异常 可以按照自己的规范来返回
    if response:
        '''drf内部错误'''
        print(response.data)
        detail = response.data.get('detail')
        non_field_errors = response.data.get('non_field_errors')
        if detail:
            return Response({"code": 1001, "msg": detail})

        if non_field_errors:
            return Response({"code": 1001, "msg": non_field_errors[0]})
        return response
    # 没有经过drf处理的异常比如代码报错 我们可以捕捉下来写入日志
    else:
        '''异常写入日志'''
        error_views = context['view']
        error_info = repr(exc)
        error_line = []
        trace = exc.__traceback__
        while trace:
            error = '"%s"  line %s' % (trace.tb_frame.f_code.co_filename, trace.tb_lineno)
            print('33[31;0mERROR: %s 33[0m' % error)
            error_line.append(error)
            trace = trace.tb_next
        print('33[31;0m%s 33[0m' % error_info)
        # error_log.error(f'error_views:{error_views} | error_info:{error_info} | error_line:{error_line[::-1]}')
        error = '服务器内部错误'

        return Response({"code": 1001, "msg": error}, status=500)
# 在配置文件中配置生效

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'background.utils.exception.custom_exception_handler',
}
原文地址:https://www.cnblogs.com/sw-z/p/11248489.html