Python装饰程序错误处理

Python装饰程序错误处理,python,plugins,decorator,bottle,Python,Plugins,Decorator,Bottle,我正在创建瓶子RESTAPI,我想在一些端点使用函数装饰器来认证用户。装饰器代码为: def authenticating_decorator(func): def wrapper(): try: ''' auth user before execution of the required code if user is not authenticated bottle.HTTPError is ra

我正在创建瓶子RESTAPI,我想在一些端点使用函数装饰器来认证用户。装饰器代码为:

def authenticating_decorator(func):
    def wrapper():
        try:
            '''
            auth user before execution of the required code
            if user is not authenticated bottle.HTTPError is raised
            '''
            auth()  
            return func
        except HTTPError as e:
            return handle_auth_error  

    return wrapper()

return authenticating_decorator
Handle auth error函数:

def handle_auth_error(error):
    return {
        "code": error.status_code,
        "name": error.body.get('name'),
        "description": error.body.get('description')
    }
一切正常,除了我安装了瓶子插件来捕获异常并将其转换为所需的JSON,API响应的内容为typeapplication/JSON

当异常发生在auth方法中时,API以已知的html格式返回错误,因为它以某种方式跳过了我的错误插件。(当同时使用插件和装饰器时,我不能完全理解应用程序流)

我的错误插件的调用方法:

def __call__(self, callback):
    def wrapper(*a, **kw):
        try:
            rv = callback(*a, **kw)
            return rv
        except HTTPError as e:
            response.status = e.status_code
            return {
                "code": e.status_code,
                "name": e.body.get('name'),
                "description": e.body.get('description')
            }
    return wrapper
我的观点是,由于行rv=callback(*a,**kw)

由于我在decorator中的auth()方法中有多种类型的异常,我想将异常作为参数传递给decorator中的
handle\u auth\u error

但是如果我键入
returnhandle\u auth\u error(e)
函数将返回dict,而不是函数,并且我得到异常dict对象不可在代码行
rv=callback(*a,**kw)

我如何从decorator返回带有参数的函数,而不在decorator中调用它,而是在plugin中调用它? 或者如何将异常作为参数传递给插件

可能的解决方案是使用基于异常名称的“switch”语句创建自己的函数来处理每一个可能的异常,但我想用更编程的方式来实现:

 return {
     'HEADER_MISSING': handle_header_missing_exception,
     'TOKEN_EXPIRED': handle_expired_token_exception,
      etc ... : etc...
            }.get(e.body.get('name'))

我认为你的装潢师写得不对,不是吗:

def authenticating_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kw):
        try:
             '''
            auth user before execution of the required code
            if user is not authenticated bottle.HTTPError is raised
            '''
            auth()  
            return func(*args, **kw) #calling func here
        except HTTPError as e:
            return handle_auth_error(e)  #calling the handle_auto_error here

return wrapper #notice - no call here, just return the wrapper

我认为你的装潢师写得不对,不是吗:

def authenticating_decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kw):
        try:
             '''
            auth user before execution of the required code
            if user is not authenticated bottle.HTTPError is raised
            '''
            auth()  
            return func(*args, **kw) #calling func here
        except HTTPError as e:
            return handle_auth_error(e)  #calling the handle_auto_error here

return wrapper #notice - no call here, just return the wrapper