summaryrefslogtreecommitdiff
path: root/compiler/cpp/src/thrift/generate
diff options
context:
space:
mode:
authorYuxuan 'fishy' Wang <yuxuan.wang@reddit.com>2021-09-01 14:17:31 -0700
committerYuxuan 'fishy' Wang <fishywang@gmail.com>2021-09-03 22:24:25 -0700
commitcdbcff99958c024d224de98a0dfc2e98d6d779b6 (patch)
tree08dce8dffe9b9cb7e80af5a35bca72159049dca6 /compiler/cpp/src/thrift/generate
parentf83ebeae9d0a7b6c0f1005b430184ba02d9b8d50 (diff)
downloadthrift-cdbcff99958c024d224de98a0dfc2e98d6d779b6.tar.gz
THRIFT-5459: Fix breaking issue when adding a new exception
Client: go Currently in the compiler generated go code, adding a new exception to an existing endpoint can cause unexpected behaviors when the client isn't updated. Fix the issue. Will be cherry-picked into 0.15.0 after merged.
Diffstat (limited to 'compiler/cpp/src/thrift/generate')
-rw-r--r--compiler/cpp/src/thrift/generate/t_go_generator.cc18
1 files changed, 17 insertions, 1 deletions
diff --git a/compiler/cpp/src/thrift/generate/t_go_generator.cc b/compiler/cpp/src/thrift/generate/t_go_generator.cc
index afed5ac99..910eed398 100644
--- a/compiler/cpp/src/thrift/generate/t_go_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_go_generator.cc
@@ -2295,7 +2295,23 @@ void t_go_generator::generate_service_client(t_service* tservice) {
f_types_ << indent() << "}" << endl << endl;
}
- if (!(*f_iter)->get_returntype()->is_void()) {
+ if ((*f_iter)->get_returntype()->is_struct()) {
+ // Check if the result is nil, which likely means we have a new
+ // exception added but unknown to the client yet
+ // (e.g. client hasn't updated the thrift file).
+ // Sadly this check can only be reliable done when the return type is a
+ // struct in go.
+ std::string retName = tmp("_ret");
+ f_types_ << indent() << "if " << retName << " := " << resultName
+ << ".GetSuccess(); " << retName << " != nil {" << endl;
+ indent_up();
+ f_types_ << indent() << "return " << retName << ", nil" << endl;
+ indent_down();
+ f_types_ << indent() << "}" << endl;
+ f_types_ << indent() << "return nil, "
+ << "thrift.NewTApplicationException(thrift.MISSING_RESULT, \""
+ << method << " failed: unknown result\")" << endl;
+ } else if (!(*f_iter)->get_returntype()->is_void()) {
f_types_ << indent() << "return " << resultName << ".GetSuccess(), nil" << endl;
} else {
f_types_ << indent() << "return nil" << endl;