moebot/__init__.py
def MWAPIWrapper(func):
    """
    MWAPIWrapper 控制API请求异常的装饰器
    根据requests库定义的异常来控制请求返回的意外情况
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        self = args[0]
        try:
            result = func(*args, **kwargs)
            return result
        except ConnectionError:
            err_title = '连接错误'
            err_message = '[{name}] 连接错误，网络状况异常'.format(name=func.__name__, host=self.host)
        except HTTPError as e:
            err_title = 'HTTP响应错误'
            err_message = '[{name}] 目标服务器"{host}" HTTP响应错误({detail})'.format(name=func.__name__,
                                                                             host=self.host, detail=e.message)
        except Timeout:
            err_title = '请求超时'
            err_message = '[{name}] 目标服务器"{host}" 请求超时'.format(name=func.__name__, host=self.host)
        except TooManyRedirects:
            err_title = '过多重定向'
            err_message = '[{name}] 目标服务器"{host}" 过多重定向'.format(name=func.__name__, host=self.host)
        except ValueError as e:
            if e.message.find('JSON') >= 0:
                err_title = 'API JSON返回值异常'
                err_message = '[{name}] 目标服务器"{host}" API JSON返回值异常'.format(name=func.__name__, host=self.host)
            else:
                err_title = '值错误'
                err_message = '[{name}] 存在ValueError：{msg}'.format(name=func.__name__, msg=e.message)
                self.log.error(e, exc_info=True)
        except KeyError as e:
            err_title = '键错误'
            err_message = '[{name}] 存在KeyError，错误键为{key}'.format(name=func.__name__, key=e.message)
            self.log.error(e, exc_info=True)

        except MWAPIException as e:
            err_title = 'Mediawiki API 异常'
            err_message = e.message

        self.log.error('%s:%s', err_title, err_message)
        return {'success': False, 'errtitle': err_title, 'errmsg': err_message}
    return wrapper
def MWAPIWrapper(func):
    """
    MWAPIWrapper 控制API请求异常的装饰器
    根据requests库定义的异常来控制请求返回的意外情况
    """
    @wraps(func)
    def wrapper(*args, **kwargs):
        self = args[0]
        try:
            result = func(*args, **kwargs)
            return result
        except ConnectionError:
            err_title = '连接错误'
            err_message = '[{name}] 连接错误，网络状况异常'.format(name=func.__name__, host=self.host)
        except HTTPError as e:
            err_title = 'HTTP响应错误'
            err_message = '[{name}] 目标服务器"{host}" HTTP响应错误({detail})'.format(name=func.__name__,
                                                                             host=self.host, detail=e.message)
        except Timeout:
            err_title = '请求超时'
            err_message = '[{name}] 目标服务器"{host}" 请求超时'.format(name=func.__name__, host=self.host)
        except TooManyRedirects:
            err_title = '过多重定向'
            err_message = '[{name}] 目标服务器"{host}" 过多重定向'.format(name=func.__name__, host=self.host)
        except ValueError as e:
            if e.message.find('JSON') >= 0:
                err_title = 'API JSON返回值异常'
                err_message = '[{name}] 目标服务器"{host}" API JSON返回值异常'.format(name=func.__name__, host=self.host)
            else:
                err_title = '值错误'
                err_message = '[{name}] 存在ValueError：{msg}'.format(name=func.__name__, msg=e.message)
                self.log.error(e, exc_info=True)
        except KeyError as e:
            err_title = '键错误'
            err_message = '[{name}] 存在KeyError，错误键为{key}'.format(name=func.__name__, key=e.message)
            self.log.error(e, exc_info=True)

        except MWAPIException as e:
            err_title = 'Mediawiki API 异常'
            err_message = e.message

        self.log.error('%s:%s', err_title, err_message)
        return {'success': False, 'errtitle': err_title, 'errmsg': err_message}
    return wrapper
MWAPIWrapper 控制API请求异常的装饰器
    根据requests库定义的异常来控制请求返回的意外情况