Abnormal module

Abnormal module (Key)

Rewriting module object is abnormal abnormality record information (item on line)

1, the configuration in drf configuration settings in EXCEPTION_HANDLER, pointing exception_handler custom function
2, drf abnormal, and will callback exception_handler function, carry exception object and exception information content,
complete exception information in exception_handler function returns, and exception information logging log

Core: logging exception information needs to be recorded, so you need to customize; drf only deal with clients abnormal, the server to require manual processing, unified treatment results

usage

  1. Is mainly used for error logging system for easy modification, is particularly important, abnormal will be recorded.
  2. Rewrite abnormal

Configuration settings.py

REST_FRAMEWORK = {
    # 异常
    # 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',#系统异常
    'EXCEPTION_HANDLER': 'api.utils.exception_handler',#重写异常

}
#测试
from rest_framework.response import Response
def exception_handler(exc, condent):
    print(exc)
    print(condent)
    return Response({
        'msg':"异常"
    })

Starter Edition

from rest_framework.response import Response

def exception_handler(exc, context):
    # 开发阶段一定要记录日志
    # logging.error(exc)
    return Response('%s - %s' % (context['view'].__class__.__name__, exc))

Upgraded version

from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    if response is None: # drf没有处理的异常
        response = Response({'detail': '%s' % exc})

    # 项目阶段,要记录到日志文件
    return response

Ultimate Edition

"""
返回数据类型
response = {
    'status': 7,
    'exc': '异常信息'
}
"""
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework import status
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)

    if response is None: # drf没有处理的异常(服务器异常)
        return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={
            'status': 7,
            'exc': '%s' % exc
        })
#设置状态码
    # 项目阶段,要记录到日志文件
    return Response(status=response.status_code, data={
        'status': 7,
        # drf处理的客户端异常,原始处理方式是将异常信息放在response对象的data中,data的格式是{'datail': '具体的异常信息'}
        'exc': '%s' % response.data.get('detail')
    })

Source code analysis

1, the dispatch method APIView, there are a large try ... except ..., exceptions to the code running abnormality processing module self.handle_exception (EXC)
2, a configuration which is mapped from the exception handling function (custom exception module is configured to point to your own custom function): self.get_exception_handler ()
3, abnormal function exception_handler (exc, context) handle the exception, it will go its own:
the first system to handle (abnormal client) The system did not handle (abnormal server), and then deal with their own

Guess you like

Origin www.cnblogs.com/SkyOceanchen/p/11895477.html