[MOAB-dev] commit/MOAB: danwu: The desired return type of an MOAB routine for error handling has not been confirmed yet. A macro

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Feb 10 16:41:50 CST 2014


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/30f1c5d4ac4f/
Changeset:   30f1c5d4ac4f
Branch:      error_handling_enhancement
User:        danwu
Date:        2014-02-10 23:40:01
Summary:     The desired return type of an MOAB routine for error handling has not been confirmed yet. A macro
USE_ERROR_INFO_CLASS is now introduced to separate two versions of error handling interfaces, one
based on existing ErrorCode enum type and the other based on new ErrorInfo class type.

We will temporally use the ErrorCode based interfaces to implement and test enhanced error handling
inside MOAB, as using ErrorInfo class is likely to affect existing code more significantly.

Affected error handling examples are updated accordingly.

Affected #:  4 files

diff --git a/examples/TestErrorHandling.cpp b/examples/TestErrorHandling.cpp
index 79901c5..fbb8007 100644
--- a/examples/TestErrorHandling.cpp
+++ b/examples/TestErrorHandling.cpp
@@ -12,6 +12,7 @@ using namespace moab;
 using namespace std;
 
 // Call hierarchy: A calls B, and B calls C
+#ifdef USE_ERROR_INFO_CLASS
 ErrorInfo FunctionC(int n)
 {
   if (1 == n) {
@@ -70,6 +71,64 @@ ErrorInfo TestErrorHandling_3()
 
   return ERROR_INFO_SUCCESS;
 }
+#else
+ErrorCode FunctionC(int n)
+{
+  if (1 == n) {
+    // No error occurs
+    return MB_SUCCESS;
+  }
+  else if (2 == n) {
+    // Simulate MB_FILE_WRITE_ERROR
+    SET_ERR(MB_FILE_WRITE_ERROR, "FunctionC() created a contrived error MB_FILE_WRITE_ERROR");
+  }
+  else if (3 == n) {
+    // Simulate MB_NOT_IMPLEMENTED
+    SET_ERR_STR(MB_NOT_IMPLEMENTED, "n = " << n << ", FunctionC() created a contrived MB_NOT_IMPLEMENTED (this message is built with C++ streaming)");
+  }
+
+  return MB_SUCCESS;
+}
+
+ErrorCode FunctionB(int n)
+{
+  ErrorCode err_code = FunctionC(n);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+ErrorCode FunctionA(int n)
+{
+  ErrorCode err_code = FunctionB(n);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+// In this test case, no error occurs in the call hierarchy
+ErrorCode TestErrorHandling_1()
+{
+  ErrorCode err_code = FunctionA(1);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+// In this test case, MB_FILE_WRITE_ERROR is returned by FunctionC
+ErrorCode TestErrorHandling_2()
+{
+  ErrorCode err_code = FunctionA(2);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+// In this test case, MB_NOT_IMPLEMENTED is returned by FunctionC
+// The error message is built with C++ streaming
+ErrorCode TestErrorHandling_3()
+{
+  ErrorCode err_code = FunctionA(3);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+#endif
 
 int main()
 {

diff --git a/examples/TestErrorHandlingPar.cpp b/examples/TestErrorHandlingPar.cpp
index a9bc4d8..99f9c9f 100644
--- a/examples/TestErrorHandlingPar.cpp
+++ b/examples/TestErrorHandlingPar.cpp
@@ -14,6 +14,7 @@
 using namespace moab;
 
 // Call hierarchy: A calls B, and B calls C
+#ifdef USE_ERROR_INFO_CLASS
 ErrorInfo FunctionC(int rank)
 {
   // Simulate MB_FILE_WRITE_ERROR
@@ -45,6 +46,37 @@ ErrorInfo TestErrorHandlingPar(int rank)
 
   return ERROR_INFO_SUCCESS;
 }
+#else
+ErrorCode FunctionC(int rank)
+{
+  // Simulate MB_FILE_WRITE_ERROR
+  SET_ERR_STR(MB_FILE_WRITE_ERROR, "FunctionC() created a contrived error MB_FILE_WRITE_ERROR in processor " << rank);
+
+  return MB_SUCCESS;
+}
+
+ErrorCode FunctionB(int rank)
+{
+  ErrorCode err_code = FunctionC(rank);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+ErrorCode FunctionA(int rank)
+{
+  ErrorCode err_code = FunctionB(rank);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+
+// In this test case, MB_FILE_WRITE_ERROR is returned by FunctionC in each processor
+ErrorCode TestErrorHandlingPar(int rank)
+{
+  ErrorCode err_code = FunctionA(rank);CHK_ERR(err_code);
+
+  return MB_SUCCESS;
+}
+#endif
 
 int main(int argc, char** argv)
 {

diff --git a/src/ErrorHandler.cpp b/src/ErrorHandler.cpp
index 6754b7e..1b6ee56 100644
--- a/src/ErrorHandler.cpp
+++ b/src/ErrorHandler.cpp
@@ -43,11 +43,20 @@ void MBTraceBackErrorHandler(int line, const char* func, const char* file, const
     errorOutput->printf("%s() line %d in %s%s\n", func, line, dir, file);
 }
 
+#ifdef USE_ERROR_INFO_CLASS
 ErrorInfo MBError(int line, const char* func, const char* file, const char* dir, const ErrorInfo& err_info, ErrorType err_type)
 {
   MBTraceBackErrorHandler(line, func, file, dir, err_info.get_error_msg(), err_type);
 
   return err_info;
 }
+#else
+ErrorCode MBError(int line, const char* func, const char* file, const char* dir, ErrorCode err_code, const char* err_msg, ErrorType err_type)
+{
+  MBTraceBackErrorHandler(line, func, file, dir, err_msg, err_type);
+
+  return err_code;
+}
+#endif
 
 } // namespace moab

diff --git a/src/moab/ErrorHandler.hpp b/src/moab/ErrorHandler.hpp
index efb117f..f387f29 100644
--- a/src/moab/ErrorHandler.hpp
+++ b/src/moab/ErrorHandler.hpp
@@ -6,10 +6,20 @@
 #include <sstream>
 #include <string.h>
 
+/* The desired return type of an MOAB routine for error handling has not been confirmed yet. A macro
+ * USE_ERROR_INFO_CLASS is now introduced to separate two versions of error handling interfaces, one
+ * based on existing ErrorCode enum type and the other based on new ErrorInfo class type.
+ *
+ * We will temporally use the ErrorCode based interfaces to implement and test enhanced error handling
+ * inside MOAB, as using ErrorInfo class is likely to affect existing code more significantly.
+ */
+#undef USE_ERROR_INFO_CLASS
+
 namespace moab {
 
 enum ErrorType { MB_ERROR_TYPE_NEW = 0, MB_ERROR_TYPE_EXISTING = 1 };
 
+#ifdef USE_ERROR_INFO_CLASS
 //! This class will replace the ErrorCode return codes in most cases
 class ErrorInfo {
   ErrorCode mErrorCode;
@@ -76,6 +86,7 @@ public:
     return mErrorCode;
   }
 };
+#endif
 
 //! Initialize MOAB error handler (e.g. create a utility object for printing error output)
 void MBErrorHandler_Init();
@@ -87,7 +98,11 @@ void MBErrorHandler_Finalize();
 bool MBErrorHandler_Initialized();
 
 //! Routine that is called when an error has been detected
+#ifdef USE_ERROR_INFO_CLASS
 ErrorInfo MBError(int line, const char* func, const char* file, const char* dir, const ErrorInfo& err_info, ErrorType err_type);
+#else
+ErrorCode MBError(int line, const char* func, const char* file, const char* dir, ErrorCode err_code, const char* err_msg, ErrorType err_type);
+#endif
 
 #define ERROR_INFO_SUCCESS ErrorInfo(MB_SUCCESS)
 
@@ -102,6 +117,7 @@ ErrorInfo MBError(int line, const char* func, const char* file, const char* dir,
 #define __SDIR__ ""
 #endif
 
+#ifdef USE_ERROR_INFO_CLASS
 #define SET_ERR(err_info) \
   return MBError(__LINE__, __func__, __FILENAME__, __SDIR__, err_info, MB_ERROR_TYPE_NEW)
 
@@ -143,7 +159,7 @@ ErrorInfo MBError(int line, const char* func, const char* file, const char* dir,
 #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_code(MB_FAILURE); \
       err_info.set_error_msg(err_msg_to_set); \
       SET_ERR(err_info); \
     } \
@@ -157,6 +173,56 @@ ErrorInfo MBError(int line, const char* func, const char* file, const char* dir,
       SET_ERR(err_info); \
     } \
   } while (false)
+#else
+#define SET_ERR(err_code, err_msg) \
+  return MBError(__LINE__, __func__, __FILENAME__, __SDIR__, err_code, err_msg, MB_ERROR_TYPE_NEW)
+
+#define SET_ERR_STR(err_code, err_msg_str) \
+  std::ostringstream ostr; \
+  ostr << err_msg_str; \
+  return MBError(__LINE__, __func__, __FILENAME__, __SDIR__, err_code, ostr.str().c_str(), MB_ERROR_TYPE_NEW)
+
+#define CHK_ERR(err_code) \
+  do { \
+    if (MB_SUCCESS != err_code) \
+      return MBError(__LINE__, __func__, __FILENAME__, __SDIR__, err_code, "", MB_ERROR_TYPE_EXISTING); \
+  } while (false)
+
+#define CHK_ERR1(err_code, err_msg_to_set) \
+  do { \
+    if (MB_SUCCESS != err_code) { \
+      SET_ERR(err_code, err_msg_to_set); \
+    } \
+  } while (false)
+
+#define CHK_ERR2(err_code, err_code_to_set, err_msg_to_set) \
+  do { \
+    if (MB_SUCCESS != err_code) { \
+      SET_ERR(err_code_to_set, err_msg_to_set); \
+    } \
+  } while (false)
+
+#define CHK_EQL(err_code, exp_err_code) \
+  do { \
+    if (exp_err_code != err_code) { \
+      SET_ERR(MB_FAILURE, "Returned error code is not expected"); \
+    } \
+  } while (false)
+
+#define CHK_EQL1(err_code, exp_err_code, err_msg_to_set) \
+  do { \
+    if (exp_err_code != err_code) { \
+      SET_ERR(MB_FAILURE, err_msg_to_set); \
+    } \
+  } while (false)
+
+#define CHK_EQL2(err_code, exp_err_code, err_code_to_set, err_msg_to_set) \
+  do { \
+    if (exp_err_code != err_code) { \
+      SET_ERR(err_code_to_set, err_msg_to_set); \
+    } \
+  } while (false)
+#endif
 
 } // namespace moab

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