gpt4 book ai didi

java - thrift/java 中的异常类型层次结构

转载 作者:行者123 更新时间:2023-12-02 04:44:35 25 4
gpt4 key购买 nike

是否有一种方法可以添加类型层次结构信息来生成 Java 的 Thrift 异常类型。

我想定义一些人们可以重用的通用异常集。经常发生的情况是,客户端不想处理单个异常,而是想要处理一组异常。例如,假设 Java 客户端想要捕获所有错误请求并打印异常消息。客户端不想处理单个异常,例如 TooBigParamException、MissingParamException 等,而只想捕获这些异常的父异常:BadRequestException。有没有办法更改 TooBigParamException、MissingParamException 等的生成代码以添加扩展关系?

我尝试了 Facebook swift(Java -> Thrift 转换),但看起来它不能与 Apache thrift 无缝协作。

最佳答案

在当前状态下,Apache Thrift 不支持继承,无论是 struct/union 还是 exception。我的猜测是,这是因为有些语言的继承过于复杂而无法实现,因为它实际上并不是该语言的一部分。

我想到的一种解决方案是将异常转换为结构,只有一个 Thrift 异常,并将异常数据union到该结构中。客户端捕获“常见”异常并重新引发该包中的任何内容。

struct OverflowError {
1: double operator1
2: double operator2
3: string operation
}

struct DivByZeroError {
1: double numerator
}

union AllTheErrors {
1: OverflowError over
2: DivByZeroError div0
// more to come
}

exception MathError {
1: AllTheErrors theError
}


service ExceptionalCalculator {
double Add(1: double arg1, 2: double arg2) throws (1: MathError mex)
double Subtract(1: double arg1, 2: double arg2) throws (1: MathError mex)
double Divide(1: double arg1, 2: double arg2) throws (1: MathError mex)
double PriceOfDowJonesNextThursday() throws (1: MathError mex)
}

涉及相当多的工作,但可能是一个解决方案。

那边有一个related discussion about protobuf ,这也可能有帮助。

关于java - thrift/java 中的异常类型层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29770501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com