zoukankan      html  css  js  c++  java
  • 异常组件

    异常组件项目使用:记录异常信息到日志文件

    exception.py

    from rest_framework.views import exception_handler as drf_exception_handler
    from rest_framework.response import Response
    def exception_handler(exc, context):
        # 只处理客户端异常,不处理服务器异常,
        # 如果是客户端异常,response就是可以直接返回给前台的Response对象
        response = drf_exception_handler(exc, context)
    
        if response is None:
            # 没有处理的服务器异常,处理一下
            # 其实给前台返回 服务器异常 几个字就行了
            # 那我们处理异常模块的目的是 不管任何错误,都有必要进行日志记录(线上项目只能通过记录的日志查看出现过的错误)
            response = Response({'detail': '%s' % exc})
    
        # 需要结合日志模块进行日志记录的
        return response
    

    settings.py

    REST_FRAMEWORK = {
        # ...
        # 异常模块
        # 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',  # 原来的,只处理客户端异常
        'EXCEPTION_HANDLER': 'api.exception.exception_handler',
    }
    
  • 相关阅读:
    String类
    try catch异常捕获
    while循环语句
    编程中穷举法的运用
    for循环例题
    编程中的 if ()else() 语句
    代位符
    运算符_及_运算符优先级
    C#中的类型转换
    Asp.net基础知识
  • 原文地址:https://www.cnblogs.com/kai-/p/12363215.html
Copyright © 2011-2022 走看看