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
    

      

  • 相关阅读:
    Codeforces Round #271 (Div. 2) C. Captain Marmot
    Codeforces Round #271 (Div. 2) F. Ant colony
    hdu 5002 Tree
    hdu 5052 Yaoge’s maximum profit
    hdu5044 Tree
    Codeforces Round #267 (Div. 2) D. Fedor and Essay
    hdu 4991 Ordered Subsequence
    暑假
    hdu4632 Palindrome subsequence
    【Oracle】【32】TRUNC 截取
  • 原文地址:https://www.cnblogs.com/rsapaper/p/12633590.html
Copyright © 2011-2022 走看看