Date: Friday, November 25, 2005 @ 16:11:45
  Author: marc
    Path: /cvsroot/carob/carob/test

   Added: CarobProtector.hpp (1.1)

quick & dirty (but working & useful) sketch of a generic CarobException catcher 
for CPPUnit 


--------------------+
 CarobProtector.hpp |  105 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 105 insertions(+)


Index: carob/test/CarobProtector.hpp
diff -u /dev/null carob/test/CarobProtector.hpp:1.1
--- /dev/null   Fri Nov 25 16:11:45 2005
+++ carob/test/CarobProtector.hpp       Fri Nov 25 16:11:45 2005
@@ -0,0 +1,105 @@
+/**
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Emic Networks
+ * Contact: [EMAIL PROTECTED]
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Marc Herbert
+ * Contributor(s): 
+ */
+
+
+#include "CarobException.hpp"
+#include <cppunit/Protector.h>
+
+
+static const wstring BEPREFIX(L"BackendException catched by CarobProtector: ");
+static const wstring CEPREFIX(L"CarobException   catched by CarobProtector: ");
+static const wstring UEPREFIX(L"UnexpectedException catched by CarobProtector: 
");
+
+
+static void cerrStackTrace(const wstring& prefix, const CarobException& cex)
+{
+  StackTrace st = cex.getStackTrace();
+  for (StackTrace::iterator s = st.begin(); s != st.end(); s++)
+    wcerr << prefix << "            : "
+          << (*s)->declaringClass << ":" << (*s)->methodName << "(...)" << 
endl;;
+}
+
+
+static void cerrBackTrace(const wstring& prefix, const CarobException& cex)
+{
+#ifdef GLIB_BACKTRACE
+  BackTrace stvect = cex.getBackTrace();
+  for (BackTrace::iterator btframe = stvect.begin(); btframe != stvect.end(); 
btframe++) {
+    wcerr << BEPREFIX; cerr << *btframe << endl;
+  }
+#endif
+}
+
+  
+/** This class is a quick & dirty (but working & useful) sketch of a
+ * generic CarobException catcher for CPPUnit */
+
+class CarobProtector : public CppUnit::Protector
+{
+
+  bool protect( const CppUnit::Functor &functor,
+                const CppUnit::ProtectorContext &context )
+
+  {
+    try {
+      return functor();
+    }
+    catch (const BackendException& be)
+    {
+#if 1
+      // log
+      logError(L"fctname", L"BackendException "+be.description());
+      cerrBackTrace(BEPREFIX, be);
+      wcerr << BEPREFIX << "state: " << be.getSQLState() << endl;
+      wcerr << BEPREFIX << "remote stack: " << endl;
+
+      cerrStackTrace(BEPREFIX, be);
+      const CarobException* nextex_p = &be;
+      while ((nextex_p = nextex_p->getNext()) != NULL) {
+        wcerr << "  Stack of next CarobException" << endl;
+        cerrStackTrace(CEPREFIX, *nextex_p);
+      }
+#endif
+      // CPPunit report
+      string exmsg;
+      wstring sqlState = be.getSQLState();
+      reportError( context, CppUnit::Message("BackendException state: "  + 
toString(sqlState)));
+
+    }
+    catch (const UnexpectedException& ue)
+    {
+      logError(L"fctName", L"Catched unexpected exception");
+      // TODO
+      cerrBackTrace(UEPREFIX, ue);
+      reportError( context, CppUnit::Message("Unexp excp") );
+    }
+    return false; // meaning: failed?
+  }
+
+};
+
+/*
+ * Local Variables:
+ * c-file-style: "bsd"
+ * c-basic-offset: 2
+ * indent-tabs-mode: nil
+ * End:
+ */

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to