#!/usr/bin/env python # -*- encoding: utf-8 -*- """ @Version: 1.0 @Python Version:3.6.6 @Author: ludq1 @Email: ludq1@chinaunicom.cn @date: 2023/04/07 11:40:00 @Description: """ import logging from .globalconst import SkyGlobalConst from .my_stringutils import MyStringUtils from .my_utils import MyUtils class MyBaseException(Exception): """ 自定义的异常类, import 请使用 global """ ret_val: str = MyStringUtils.EMPTY ret_code: str = 'ERROR' submitted_webarg: str = None def __init__(self, err_msg: str): r''' 初始化异常 Args: err_msg: 错误信息,不能为None或空字符串 ret_code: 错误码 submitted_webarg: ''' # 判断 err_msg Exception.__init__(self, err_msg) self.ret_val = err_msg def __str__(self): r''' 字符串表示,使用 ret_val , 不会返回None,至少时 EMPTY字符串 Returns: ''' return MyStringUtils.to_str(self.ret_val) def __repr__(self): r''' 字符串表示,使用 __str__ , 不会返回None,至少时 EMPTY字符串 Returns: ''' return self.__str__() def gen_err(self) -> dict: r''' 生成带有 RetCode,RetVal,SubmittedWebArg的标准dict Returns: ''' ret_dict = MyBaseException.format_to_standard_dict(self.ret_val, self.ret_code) if self.submitted_webarg is not None: ret_dict[SkyGlobalConst.RETKEY_SUBMITTEDWEBARG] = MyStringUtils.to_str( MyUtils.do_filter_xss(self.submitted_webarg)) return ret_dict @classmethod def format_to_standard_dict(cls, ret_val: str, ret_code: str = SkyGlobalConst.RETCODE_COMMON_ERROR, submitted_msg: str = None) -> dict: r""" 将ret_val和ret_code格式化成 ryy项目组使用的标准json 字符串, 即 { "RetCode": ret_code , "RetVal": ret_val } Args: ret_val:string 返回结果 , 自动对其进行 filterXss操作 ret_code:string 返回码 Returns: json 返回json数据 """ ret_dict = {} ret_dict[SkyGlobalConst.RETKEY_RET_CODE] = MyStringUtils.to_str(MyUtils.do_filter_xss(ret_code)) ret_dict[SkyGlobalConst.RETKEY_RET_VAL] = MyStringUtils.to_str(MyUtils.do_filter_xss(ret_val)) if submitted_msg is not None: ret_dict[SkyGlobalConst.RETKEY_SUBMITTEDWEBARG] = MyStringUtils.to_str(MyUtils.do_filter_xss(submitted_msg)) return ret_dict @classmethod def gen_ok_dict(cls) -> dict: r""" 生成标准的 返回给前端的jsonObject,ok Args: Returns: json 返回json数据 """ ret_dict = {} ret_dict[SkyGlobalConst.RETKEY_RET_CODE] = SkyGlobalConst.RETCODE_SUCESS_CODE ret_dict[SkyGlobalConst.RETKEY_RET_VAL] = 'success' return ret_dict @classmethod def format_exception_to_standard_dict(cls, any_exception: Exception) -> dict: r''' 将异常格式化为标准的带有 RetCode和RetVal的字典 Args: any_exception: Returns: ''' if any_exception is None: raise ValueError("要格式化的异常对象为None") logging.exception(msg="exception") if MyUtils.has_attr(any_exception, 'gen_err', must_be_method=True): # 尝试使用 gen_err方法返回 return any_exception.gen_err() else: if isinstance(any_exception, (NameError, ValueError,)): return MyBaseException.format_to_standard_dict(str(any_exception)) else: return MyBaseException.format_to_standard_dict( '{}:{}'.format(type(any_exception), any_exception) ) def create_base_exception( err_msg, ret_code='ERROR', submitted_webarg=None ) -> MyBaseException: r''' 初始化一个 MyBaseException Args: err_msg: ret_code: submitted_webarg: Returns: :rtype: MyBaseException ''' ret_excep = MyBaseException(err_msg) ret_excep.ret_code = ret_code ret_excep.submitted_webarg = submitted_webarg return ret_excep def create_base_exception_with_dict( err_dict: dict, ret_code: str = 'ERROR', submitted_webarg: str = None ) -> MyBaseException: r''' 初始化一个 MyBaseException, 以 err_dict 中的RetVal为errMsg, 以 err_dict 中的 SubmittedWebArg + submitted_webarg 为新的 submitted_webarg Args: err_dict: ret_code: submitted_webarg: Returns: :rtype: MyBaseException ''' # 确保 err_dict 是dict if not isinstance(err_dict, dict): raise ValueError("err_dict不是字典") # 获取 err_dict 中的 retVal err_msg = err_dict.get(SkyGlobalConst.RETKEY_RET_VAL) # 获取 err_dict 中的 SubmittedWebArg ori_submitted_webarg = err_dict.get(SkyGlobalConst.RETKEY_SUBMITTEDWEBARG) ret_excep = MyBaseException(err_msg) ret_excep.ret_code = ret_code if MyStringUtils.is_empty(ori_submitted_webarg): ret_excep.submitted_webarg = submitted_webarg else: if MyStringUtils.is_empty(submitted_webarg): ret_excep.submitted_webarg = ori_submitted_webarg else: ret_excep.submitted_webarg = '{};;;{}'.format(ori_submitted_webarg, submitted_webarg) return ret_excep