Is there a way of adding type hierarchy information to generated thrift exception types for Java.
I want to define some common set of exceptions, that people can reuse. It often happens that clients instead of handling individual exceptions, want to handle a group of exceptions. For example, lets say a java client wants to catch all bad requests and print the exception message. The client does not want to handle the individual exceptions like TooBigParamException, MissingParamException etc, but instead just wants to catch a parent of these: BadRequestException. Is there a way to to change the generated codes of TooBigParamException, MissingParamException etc to add an extends relationship?
I tried Facebook swift (Java -> Thrift conversion), but it looks like it does not work seamlessly with Apache thrift.
In its current state Apache Thrift does not support inheritance, neither with struct
/ union
nor with exception
. My guess would be that this is because there are languages out there where inheritance would be too complex to implement because it is not really part of the language.
One solution that comes to my mind is to convert the exceptions into structs, having only one Thrift exception and union
the exception data into that one. The client side catches the "common" exception and re-raises whatever it is in that package.
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)
}
Involves quite a bit of work, but could be a solution.
Over there is a related discussion about protobuf , which may be helpful as well.
I found one decent solution. Though I would not say that there are no hacks in that.
When I began, there were only 2 options with me:
I was able to workaround these problems, by combining the above 2. I first wrote the java exceptions and then generated IDL using swift. Then generated another version of java exceptions from idl. I used modelmapper to convert between my original java types and generated java types. My service-core uses the first version of exceptions and my service -thrift layer, converts them to their thrift version using modelmapper.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.