zoukankan      html  css  js  c++  java
  • assert False 与 try 结合 在开发中的使用

    让错误抛出

    发现其中的问题

    # coding=utf-8
    
    from rest_framework.views import exception_handler
    from rest_framework.exceptions import ErrorDetail
    from share.dj_customer.rest_api_exception import CustomerError
    
    
    def custom_exception_handler(exc, context):
        # Call REST framework's default exception handler first,
        # to get the standard error response.
        ....
            if isinstance(old_data, dict):
                if old_data.get('detail') is not None:
                    if isinstance(old_data['detail'], tuple):
                        new_data['code'], new_data['message'] = old_data['detail'][0:2]
                    elif isinstance(old_data['detail'], str):
                        new_data['message'] = old_data['detail']
                else:
                    try:
                        # e.g. user_name [ErrorDetail(string='Ensure this field has no more than 16 characters.', code='max_length')]
                        msg = ''
                        for serializer_field, v in old_data.items():
                            if isinstance(serializer_field, str) and isinstance(v, list):
                                if isinstance(v[0], ErrorDetail):
                                    msg += serializer_field + ':' + v[0].__str__()
                                else:
                                    assert False
                            else:
                                assert False
                        # e.g.
                        # {
                        #     "success": false,
                        #     "code": 1001,
                        #     "message": "参数错误 : user_name:Ensure this field has no more than 16 characters.age:A valid integer is required."
                        # }
                        new_data['code'], new_data['message'] = CustomerError.PARAM_ERROR
                        new_data['message'] += ' : ' + msg
                    except:
                        assert False
    

      

  • 相关阅读:
    颜色混合openglglBlendFunc函数
    Types of Maps
    ogre 检测显卡gpu支持参数
    (转载)lua和c/c++互相调用实例分析
    光照模型
    阴影(转载)
    eval()解析JSON
    android中The connection to adb is down,问题和解决 AndroidEclipseAntXML
    比较android中的像素单位dp、px、pt、sp
    区别:DOM Core 与 HTMLDOM
  • 原文地址:https://www.cnblogs.com/rsapaper/p/12633590.html
Copyright © 2011-2022 走看看