如何使用 Thrift 在 RPC 中传播一般异常?

How do I propagate generic exceptions in RPC using Thrift?

我们必须使用 Thrift 框架使用 RPC 将异常从一个节点传播到另一个节点。我知道可以像这样在 Thrift 中声明异常:

exception SampleException {
    1: optional string cause
}

这会生成一个扩展通用 TException 的异常。

我想创建 TExceptions,其原因是另一个可能不会扩展 TException 的异常,并在另一个节点中接收它。这可能吗?

如果不可能,那么最好传播不扩展 TException 的通用异常。如果我们可以传播 RuntimeExceptions.

加分

I want to create TExceptions that have as a cause another exception, and to receive that in the other node. Is this possible?

exception bar {
    1 : i32 nix
}

exception foo {
    1 : bar bar
}

And that generates an exception that extends the generic TException.

是的,它应该是这样的。这并不是说 TException 本身不能继承自 RuntimeException.

Also, it would be nice to have TExceptions that are RuntimeExceptions

Thrift 是开源的。你可以拥有你想要的。 ;-) 还有 this JIRA ticket

but it needs to be a TException also. Can it be a generic one?

您绑定到 Thrift 支持的基本数据类型,以及您使用 struct、union、map<>、set<>、list<>、exception 和 typedef 从中创建的任何复杂类型。如果您需要特殊行为,那么我建议序列化数据并根据这些数据在客户端引发异常。在幕后,TException 的工作原理是一样的。如果您认为当前的行为可以改进,请在邮件列表中讨论 and/or 发送请求请求。