在當前狀態下,Apache Thrift不支持繼承,既不支持struct
/union
也不支持exception
。我的猜測是,這是因爲有些語言在那裏繼承太複雜,不能實現,因爲它不是語言的一部分。這在我腦海
一種解決方案是將異常轉換成結構,只有一個例外,節儉和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,其可以是有幫助。