From 53cba3cfa23d7c86badb0f85606a26a3e2391aec Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Fri, 26 Feb 2010 21:38:42 +0000 Subject: [PATCH] THRIFT-689. java: Notify client of recoverable protocol errors on java server When a protocol error occurs, the server will now send back a message containing the exception. Clients should be able to detect and rethrow these exceptions as appropriate. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@916825 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_java_generator.cc | 20 ++++++++++++++++++- .../apache/thrift/TApplicationException.java | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc index bb59ab8b..949d0628 100644 --- a/compiler/cpp/src/generate/t_java_generator.cc +++ b/compiler/cpp/src/generate/t_java_generator.cc @@ -2538,7 +2538,25 @@ void t_java_generator::generate_process_function(t_service* tservice, f_service_ << indent() << argsname << " args = new " << argsname << "();" << endl << - indent() << "args.read(iprot);" << endl << + indent() << "try {" << endl; + indent_up(); + f_service_ << + indent() << "args.read(iprot);" << endl; + indent_down(); + f_service_ << + indent() << "} catch (TProtocolException e) {" << endl; + indent_up(); + f_service_ << + indent() << "iprot.readMessageEnd();" << endl << + indent() << "TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());" << endl << + indent() << "oprot.writeMessageBegin(new TMessage(\"" << tfunction->get_name() << "\", TMessageType.EXCEPTION, seqid));" << endl << + indent() << "x.write(oprot);" << endl << + indent() << "oprot.writeMessageEnd();" << endl << + indent() << "oprot.getTransport().flush();" << endl << + indent() << "return;" << endl; + indent_down(); + f_service_ << indent() << "}" << endl; + f_service_ << indent() << "iprot.readMessageEnd();" << endl; t_struct* xs = tfunction->get_xceptions(); diff --git a/lib/java/src/org/apache/thrift/TApplicationException.java b/lib/java/src/org/apache/thrift/TApplicationException.java index f8403923..c294fc38 100644 --- a/lib/java/src/org/apache/thrift/TApplicationException.java +++ b/lib/java/src/org/apache/thrift/TApplicationException.java @@ -44,6 +44,7 @@ public class TApplicationException extends TException { public static final int BAD_SEQUENCE_ID = 4; public static final int MISSING_RESULT = 5; public static final int INTERNAL_ERROR = 6; + public static final int PROTOCOL_ERROR = 7; protected int type_ = UNKNOWN; -- 2.17.1