ruby - 如何从 gRPC 服务返回错误?

标签 ruby error-handling microservices grpc

我有一个在 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., the GRPC::Cancelled class corresponds to status CANCELLED.

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/

相关文章:

ruby - 使用 RubyLDAP ldap 进行客户端身份验证

ruby - 如何在 postgresql 中创建多列推荐引擎?

c# - 如何读取 ASP.NET NullReferenceException 消息中的堆栈跟踪

Azure 微服务性能见解 - 集体性能计数器报告

ruby - Ruby 中的“super”和继承

ruby-on-rails - 载波文件未从 S3 中删除

r - loadNamespace(name): there is no package called ‘Rcompression’ 中的错误

java - 在 ViewScop 上写入 404 并在 JAVA JSF 中不重定向显示错误文件

ssl - 是否真的需要在微服务架构中采用 ssl 传输层来进行内部 lan-only 服务到服务通信?

python - 使用单个 url docker-compose 访问多个服务