How to define a global error handler in gRPC python

后端 未结 3 1850
眼角桃花
眼角桃花 2021-02-09 11:11

Im trying to catch any exception that is raised in any servicer so I can make sure that I only propagate known exceptions and not unexpected ones like ValueError, TypeError etc.

3条回答
  •  没有蜡笔的小新
    2021-02-09 11:46

    As some of the previous comments suggested, I tried the meta-class approach which works quite well.

    Attached is a simple example to demonstrate how to intercept the grpc calls. You could extend this by providing the metaclass a list of decorators which you could apply on each function.

    Also, it would be wise to be more selective regarding the methods you apply the wrapper to. A good option would be to list the methods of the autogenerated base class and only wrap those.

    from types import FunctionType
    from functools import wraps
    
    
    def wrapper(method):
        @wraps(method)
        def wrapped(*args, **kwargs):
            # do stuff here
            return method(*args, **kwargs)
    
        return wrapped
    
    
    class ServicerMiddlewareClass(type):
        def __new__(meta, classname, bases, class_dict):
            new_class_dict = {}
    
            for attribute_name, attribute in class_dict.items():
                if isinstance(attribute, FunctionType):
                    # replace it with a wrapped version
                    attribute = wrapper(attribute)
    
                new_class_dict[attribute_name] = attribute
    
            return type.__new__(meta, classname, bases, new_class_dict)
    
    
    # In order to use
    class MyGrpcService(grpc.MyGrpcServicer, metaclass=ServicerMiddlewareClass):
       ...
    

提交回复
热议问题