def MWAPIWrapper(func):
@wraps(func)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>self = args[<NUM_LIT:0>]<EOL>try:<EOL><INDENT>result = func(*args, **kwargs)<EOL>return result<EOL><DEDENT>except ConnectionError:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, host=self.host)<EOL><DEDENT>except HTTPError as e:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__,<EOL>host=self.host, detail=e.message)<EOL><DEDENT>except Timeout:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, host=self.host)<EOL><DEDENT>except TooManyRedirects:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, host=self.host)<EOL><DEDENT>except ValueError as e:<EOL><INDENT>if e.message.find('<STR_LIT>') >= <NUM_LIT:0>:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, host=self.host)<EOL><DEDENT>else:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, msg=e.message)<EOL>self.log.error(e, exc_info=True)<EOL><DEDENT><DEDENT>except KeyError as e:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = '<STR_LIT>'.format(name=func.__name__, key=e.message)<EOL>self.log.error(e, exc_info=True)<EOL><DEDENT>except MWAPIException as e:<EOL><INDENT>err_title = '<STR_LIT>'<EOL>err_message = e.message<EOL><DEDENT>self.log.error('<STR_LIT>', err_title, err_message)<EOL>return {'<STR_LIT:success>': False, '<STR_LIT>': err_title, '<STR_LIT>': err_message}<EOL><DEDENT>return wrapper<EOL>
MWAPIWrapper 控制API请求异常的装饰器
根据requests库定义的异常来控制请求返回的意外情况