如何在gRPC python中定义全局错误处理程序 [英] How to define a global error handler in gRPC python
问题描述
我正在尝试捕获任何服务程序中引发的任何异常,因此我可以确保只传播已知的异常,而不传播ValueError,TypeError等意外异常。
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.
我希望能够捕获任何引发的错误,并将其格式化或将其转换为其他错误,以更好地控制所公开的信息。
I'd like to be able to catch any raised error, and format them or convert them to other errors to better control the info that is exposed.
我不不想用try / except包围每个服务程序方法。
I don't want to have to enclose every servicer method with try/except.
我尝试过使用拦截器,但无法在那里捕获错误。
I've tried with an interceptor, but im not able to catch the errors there.
是否可以为grpc服务器指定错误处理程序?像您对flask或任何其他http服务器进行的操作一样?
Is there a way of specifying an error handler for the grpc Server? like what you do with flask or any other http server?
推荐答案
gRPC Python当前不支持服务器端全局错误处理程序。拦截器不会在 intercept_service
函数内部执行服务器处理程序,因此无法尝试/例外。
gRPC Python currently don't support server-side global error handler. The interceptor won't execute the server handler inside the intercept_service
function, so there is no way to try/except.
此外,我发现gRPC Python服务器拦截器实现不同于他们在 L13-Python-Interceptors.md#server-interceptors 。如果实现坚持原始设计,则可以通过 handler
和 request
/ <轻松地将拦截器用作全局错误处理程序code> request_iterator 。
Also, I found the gRPC Python server interceptor implementation is different from what they proposed original at L13-Python-Interceptors.md#server-interceptors. If the implementation stick to the original design, we can use interceptor as global error handler easily with handler
and request
/request_iterator
.
# Current Implementation
intercept_service(self, continuation, handler_call_details)
# Original Design
intercept_unary_unary_handler(self, handler, method, request, servicer_context)
intercept_unary_stream_handler(self, handler, method, request, servicer_context)
intercept_stream_unary_handler(self, handler, method, request_iterator, servicer_context)
intercept_stream_stream_handler(self, handler, method, request_iterator, servicer_context)
请向 https://github.com/grpc/提交功能请求问题grpc /问题。
这篇关于如何在gRPC python中定义全局错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!