zoukankan      html  css  js  c++  java
  • Django 异常处理

    我们新建一个py文件

    # 在restful中导入exception_handler
    from rest_framework.views import exception_handler
    from django.db import DatabaseError
    from rest_framework.response import Response
    from rest_framework import status
    
    import logging
    logger = logging.getLogger("django") # 与settings中一致
    
    
    def custom_exception_handler(exc, context):
        """
        自定义的异常处理
        :param exc:     本次请求发送的异常信息
        :param context: 本次请求发送异常的执行上下文【本次请求的request对象,异常发送的时间,行号等....】
        :return:
        """
        response = exception_handler(exc, context)
        if response is None:
            """来到这只有2中情况,要么程序没出错,要么就是出错了而Django或者restframework不识别"""
            view = context['view']
            if isinstance(exc, DatabaseError):
                # 数据库异常
                """有5个方法发debug info error critical warning"""
                logger.error('[%s] %s' % (view, exc))
                response = Response({'message': '服务器内部错误,请联系客服工作人员!'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)
        return response
    

      

  • 相关阅读:
    Visual Studio 2015 密钥
    Vue-next源码新鲜出炉一
    vue2.0基础整理
    Nest
    Nest
    Nest
    Nest
    Nest快速上手
    element-plus源码分析第一节
    获取视频第一帧,作为封面图
  • 原文地址:https://www.cnblogs.com/a438842265/p/11839703.html
Copyright © 2011-2022 走看看