[MOAB-dev] commit/MOAB: danwu: Error handling enhancement for MOAB (initial submission), with src/moab/ErrorHandler.hpp and src/ErrorHandler.cpp created. Like moab::Range class, the new interface for error handling can be consumed by MOAB and user applications later.

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Jan 8 14:30:27 CST 2014


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/ca3f5dca05a2/
Changeset:   ca3f5dca05a2
Branch:      error_handling_enhancement
User:        danwu
Date:        2014-01-08 21:27:01
Summary:     Error handling enhancement for MOAB (initial submission), with src/moab/ErrorHandler.hpp and src/ErrorHandler.cpp created. Like moab::Range class, the new interface for error handling can be consumed by MOAB and user applications later.

Affected #:  4 files

diff --git a/src/ErrorHandler.cpp b/src/ErrorHandler.cpp
new file mode 100644
index 0000000..18c7150
--- /dev/null
+++ b/src/ErrorHandler.cpp
@@ -0,0 +1,28 @@
+#include "moab/ErrorHandler.hpp"
+
+#include <iostream>
+
+using namespace std;
+
+namespace moab {
+
+void MBTraceBackErrorHandler(int line, const char* func, const char* file, const char* err_msg, ErrorType err_type)
+{
+  // Print the error message
+  if (MB_ERROR_TYPE_NEW == err_type) {
+    if (err_msg)
+      cout << "[Error message]: " << err_msg << endl;
+  }
+
+  // Print a line of stack trace
+  cout << func << "() at line " << line << ", in file " << file << endl;
+}
+
+ErrorInfo MBError(int line, const char* func, const char* file, const ErrorInfo& err_info, ErrorType err_type)
+{
+  MBTraceBackErrorHandler(line, func, file, err_info.get_error_msg(), err_type);
+
+  return err_info;
+}
+
+} // namespace moab

diff --git a/src/Makefile.am b/src/Makefile.am
index 5772d12..0ec5d2c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -56,6 +56,7 @@ libMOAB_la_SOURCES = \
   ElementSequence.hpp \
   EntitySequence.cpp \
   EntitySequence.hpp \
+  ErrorHandler.cpp \
   Factory.cpp \
   FBEngine.cpp \
   FileOptions.cpp \
@@ -161,6 +162,7 @@ nobase_libMOAB_la_include_HEADERS = \
   moab/HomXform.hpp \
   moab/EntityType.hpp \
   moab/EntityHandle.hpp \
+  moab/ErrorHandler.hpp \
   moab/FBEngine.hpp \
   moab/FileOptions.hpp \
   moab/FindPtFuncs.h \

diff --git a/src/moab/ErrorHandler.hpp b/src/moab/ErrorHandler.hpp
new file mode 100644
index 0000000..60ef0fd
--- /dev/null
+++ b/src/moab/ErrorHandler.hpp
@@ -0,0 +1,141 @@
+#ifndef MOAB_ERROR_HANDLER_HPP
+#define MOAB_ERROR_HANDLER_HPP
+
+#include "moab/Types.hpp"
+
+#include <sstream>
+
+namespace moab {
+
+enum ErrorType { MB_ERROR_TYPE_NEW = 0, MB_ERROR_TYPE_EXISTING = 1 };
+
+class ErrorInfo {
+  ErrorCode mErrorCode;
+  std::ostringstream mErrorMsg;
+
+public:
+  ErrorInfo(ErrorCode err_code = MB_FAILURE, const char* err_msg = "") : mErrorCode(err_code)
+  {
+    if (err_msg)
+      mErrorMsg << err_msg;
+  }
+
+  ErrorInfo(const ErrorInfo& err_info)
+  : mErrorCode(err_info.mErrorCode)
+  {
+    mErrorMsg << err_info.mErrorMsg.str();
+  }
+
+  void set_error_code(ErrorCode err_code)
+  {
+    mErrorCode = err_code;
+  }
+
+  ErrorCode get_error_code() const
+  {
+    return mErrorCode;
+  }
+
+  void set_error_msg(const char* err_msg)
+  {
+    if (err_msg) {
+      mErrorMsg.str("");
+      mErrorMsg << err_msg;
+    }
+  }
+
+  const char* get_error_msg() const
+  {
+    return mErrorMsg.str().c_str();
+  }
+
+  const ErrorInfo& operator=(const ErrorInfo& err_info)
+  {
+    if (this != &err_info) {
+      mErrorCode = err_info.mErrorCode;
+      mErrorMsg.str("");
+      mErrorMsg << err_info.mErrorMsg.str();
+    }
+
+    return *this;
+  }
+
+  // Use C++ string stream to build up an error message
+  template<typename T>
+  ErrorInfo& operator<<(T input)
+  {
+    mErrorMsg << input;
+    return *this;
+  }
+
+  // A conversion operator for converting ErrorInfo to ErrorCode
+  operator ErrorCode() const
+  {
+    return mErrorCode;
+  }
+};
+
+// Errors are handled through routine MBError()
+ErrorInfo MBError(int line, const char* func, const char* file, const ErrorInfo& err_info, ErrorType err_type);
+
+#define ERROR_INFO_SUCCESS ErrorInfo(MB_SUCCESS)
+
+#define SET_ERR(err_info) \
+  return MBError(__LINE__, __func__, __FILE__, err_info, MB_ERROR_TYPE_NEW)
+
+#define SET_ERR1(err_code, err_msg) \
+  return MBError(__LINE__, __func__, __FILE__, ErrorInfo(err_code, err_msg), MB_ERROR_TYPE_NEW)
+
+#define CHK_ERR(err_info) \
+  do { \
+    if (MB_SUCCESS != err_info) \
+      return MBError(__LINE__, __func__, __FILE__, err_info, MB_ERROR_TYPE_EXISTING); \
+  } while (false)
+
+#define CHK_ERR1(err_info, err_msg_to_set) \
+  do { \
+    if (MB_SUCCESS != err_info) { \
+      err_info.set_error_msg(err_msg_to_set); \
+      SET_ERR(err_info); \
+    } \
+  } while (false)
+
+#define CHK_ERR2(err_info, err_code_to_set, err_msg_to_set) \
+  do { \
+    if (MB_SUCCESS != err_info) { \
+      err_info.set_error_code(err_code_to_set); \
+      err_info.set_error_msg(err_msg_to_set); \
+      SET_ERR(err_info); \
+    } \
+  } while (false)
+
+#define CHK_EQL(err_info, exp_err_code) \
+  do { \
+    if (exp_err_code != err_info) { \
+      err_info.set_error_code(MB_FAILURE); \
+      err_info.set_error_msg("Returned error code is not expected."); \
+      SET_ERR(err_info); \
+    } \
+  } while (false)
+
+#define CHK_EQL1(err_info, exp_err_code, err_msg_to_set) \
+  do { \
+    if (exp_err_code != err_info) { \
+      err_info.set_error_code(MB_UNEXPECTED_ERROR_CODE); \
+      err_info.set_error_msg(err_msg_to_set); \
+      SET_ERR(err_info); \
+    } \
+  } while (false)
+
+#define CHK_EQL2(err_info, exp_err_code, err_code_to_set, err_msg_to_set) \
+  do { \
+    if (exp_err_code != err_info) { \
+      err_info.set_error_code(err_code_to_set); \
+      err_info.set_error_msg(err_msg_to_set); \
+      SET_ERR(err_info); \
+    } \
+  } while (false)
+
+} // namespace moab
+
+#endif

diff --git a/src/moab/Interface.hpp b/src/moab/Interface.hpp
index 09f4219..8ffe2a3 100644
--- a/src/moab/Interface.hpp
+++ b/src/moab/Interface.hpp
@@ -43,6 +43,7 @@
 #include "moab/Forward.hpp"
 #include "moab/Range.hpp"
 #include "moab/Compiler.hpp"
+#include "moab/ErrorHandler.hpp"
 
 // include files
 #include <string>

Repository URL: https://bitbucket.org/fathomteam/moab/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the moab-dev mailing list