我有一个在 Ruby 中运行的简单 gRPC 服务。在某些情况下,我想以受控方式向客户端返回错误,例如带有错误消息的 HTTP 状态代码。我没有从文档中看到任何明确的方法来做到这一点;正确的做法是什么?
最佳答案
经过一段时间的研究,我认为正确的答案是使用 GRPC::BadStatus
,如 here 所述。 :
BadStatus is an exception class that indicates that an error occurred at either end of a GRPC connection. When raised, it indicates that a status error should be returned to the other end of a
GRPC
connection; when caught it means that this end received a status error.There is also subclass of BadStatus in this module for each
GRPC
status. E.g., theGRPC::Cancelled
class corresponds to statusCANCELLED
.See https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/status.h for detailed descriptions of each status code.
因此,引发这样的错误
raise GRPC::BadStatus.new_status_exception(CANCELLED)
应该可以解决问题。
关于ruby - 如何从 gRPC 服务返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48671754/