Tighten integration with Ruby exceptions by tying in Exception#message
authorDavid Reiss <dreiss@apache.org>
Tue, 26 Feb 2008 06:40:25 +0000 (06:40 +0000)
committerDavid Reiss <dreiss@apache.org>
Tue, 26 Feb 2008 06:40:25 +0000 (06:40 +0000)
For Thrift exceptions that contain only a single string field, integrate
these even more tightly with Ruby exceptions by aliasing Ruby's
Exception#message field with the Thrift field.

Ruby exception objects created by Thrift will now display properly in
Ruby backtraces, etc. without any special handling by client code.

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665503 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_rb_generator.cc

index 7faa736..bb9c4d1 100644 (file)
@@ -303,12 +303,18 @@ void t_rb_generator::generate_rb_simple_exception_constructor(std::ofstream& out
     vector<t_field*>::const_iterator m_iter = members.begin();
 
     if ((*m_iter)->get_type()->is_string()) {
+      string name = (*m_iter)->get_name();
+
       indent(out) << "def initialize(message=nil)" << endl;
       indent_up();
       indent(out) << "super()" << endl;
-      indent(out) << "self." << (*m_iter)->get_name() << " = message" << endl;
+      indent(out) << "self." << name << " = message" << endl;
       indent_down();
       indent(out) << "end" << endl << endl;
+
+      if (name != "message") {
+        indent(out) << "def message; " << name << " end" << endl << endl;
+      }
     }
   }
 }