0

I'd like to throws some java built-in exception such IOException in the Thrift IDL.

like this:

service B{
     void removeLease() throws (1:ioexception e),
}

however, the Thrift compiler warns that ioexception doesn't be defined.

shuxiong
  • 103
  • 1
  • 7
  • It will become interesting when a Client connects which is not written in Java. What can he do with the payload Java exception other than just ignore it? Would that be a blocker? – JensG Apr 24 '13 at 00:43

2 Answers2

2

Every java exception is serializable, so it's possible to wrap it into thrift exception.

Thrift code:

exception SerializedException
{
    1: required binary payload
}

service MyService
{
    int method(1: required string param) throws (1: SerializedException serializedException);
}    

Java server code:

class MyServiceImpl implements MyService.Iface {
    int method(String param) throws SerializedException {
        try {
            ...
        } catch (IOException ex) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            new ObjectOutputStream(os).writeObject(ex);
            throw new SerializedException(os.toByteArray());
        }
    }
}

Java client code:

try {
    int r = myService.method("param");
} catch (SerializedException ex) {
    Exception nested = <Deserialize ex.payload via ByteArrayInputStream>
    ...
}

So, the client gets full exception together with stacktrace, etc. We use this approach is several projects, it works for sure.

Wildfire
  • 6,140
  • 2
  • 29
  • 50
  • Your Java server code, at the very least, isn't right for Java 6 (maybe it's Java 7, I'm not sure). – dmn Mar 01 '13 at 20:59
  • If you want to still have some non-Java clients, which won't be able to deserialize the exception without _a lot_ of extra work, it can be combined with at least exposing exception class name and/or message in the `SerializedException`. – Alexey Romanov Jan 03 '14 at 10:17
0

Thrift IDL is language agnostic.You cannot use built-in exceptions(like IOException in this case) You can define and use your own "ioexception"

exception ioexception
{
 1:string msg,
}
gt5050
  • 560
  • 6
  • 12
  • I am still quite doubted about this "ioexception". For I've generated related java-code for this "ioexception", and found there are some differences between this "ioexception" and the java built-in IOException. For example, IOException has more constructors than this one, which I am worry about, for there may be some situations using these missing Constructors. Thank you! – shuxiong Aug 01 '12 at 17:26