• 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
    

      

  • 相关阅读:
    POJ
    luogu- P1373 小a和uim之大逃离 DP 四维,其中一维记录差值
    牛客国庆集训派对Day3 B Tree(树形dp + 组合计数)
    【CF 1059C】 Sequence Transformation 数学
    POJ
    牛客国庆集训派对Day6 A Birthday 费用流
    Treap + 无旋转Treap 学习笔记
    牛客2018国庆集训派对Day3 I Metropolis 多源最短路径
    Gym
    CodeForces
  • 原文地址:https://www.cnblogs.com/rsapaper/p/12633590.html
Copyright © 2020-2023  润新知