[MOAB-dev] commit/MOAB: danwu: Applied error handling code to Core::serial_load_file() for testing. If a file does not exist, or it is not recognized by MOAB, error messages and stack traces will be printed out (examples/TestErrorHandling.cpp is updated to demonstrate this feature).

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Feb 12 17:12:19 CST 2014


1 new commit in MOAB:

https://bitbucket.org/fathomteam/moab/commits/c1ebc8af255f/
Changeset:   c1ebc8af255f
Branch:      error_handling_enhancement
User:        danwu
Date:        2014-02-13 00:11:55
Summary:     Applied error handling code to Core::serial_load_file() for testing. If a file does not exist, or it is not recognized by MOAB, error messages and stack traces will be printed out (examples/TestErrorHandling.cpp is updated to demonstrate this feature).

Affected #:  2 files

diff --git a/examples/TestErrorHandling.cpp b/examples/TestErrorHandling.cpp
index fbb8007..f7f3cc9 100644
--- a/examples/TestErrorHandling.cpp
+++ b/examples/TestErrorHandling.cpp
@@ -1,16 +1,21 @@
 /** @example TestErrorHandling.cpp \n
  * Description: Demonstrates enhanced error handling of MOAB. \n
- * This example creates some contrived errors to test MOAB's trace back error handler. \n
+ * This example tests MOAB's trace back error handler. \n
  *
  * To run: ./TestErrorHandling \n
  */
 
-#include "moab/Core.hpp" // Note, moab::Core is not used in this example
+#include "moab/Core.hpp"
+#ifdef USE_MPI
+#include "moab_mpi.h"
+#endif
+
 #include <iostream>
 
 using namespace moab;
 using namespace std;
 
+// Functions that create and handle contrived errors
 // Call hierarchy: A calls B, and B calls C
 #ifdef USE_ERROR_INFO_CLASS
 ErrorInfo FunctionC(int n)
@@ -26,7 +31,7 @@ ErrorInfo FunctionC(int n)
   else if (3 == n) {
     // Simulate MB_NOT_IMPLEMENTED
     ErrorInfo err_info(MB_NOT_IMPLEMENTED);
-    err_info << "n = " << n << ", FunctionC() created a contrived MB_NOT_IMPLEMENTED (this message is built with C++ streaming)";
+    err_info << "n = " << n << ", FunctionC() created a contrived error MB_NOT_IMPLEMENTED (this message is built with C++ streaming)";
     SET_ERR(err_info);
   }
 
@@ -46,31 +51,6 @@ ErrorInfo FunctionA(int n)
 
   return ERROR_INFO_SUCCESS;
 }
-
-// In this test case, no error occurs in the call hierarchy
-ErrorInfo TestErrorHandling_1()
-{
-  ErrorInfo err_info = FunctionA(1);CHK_ERR(err_info);
-
-  return ERROR_INFO_SUCCESS;
-}
-
-// In this test case, MB_FILE_WRITE_ERROR is returned by FunctionC
-ErrorInfo TestErrorHandling_2()
-{
-  ErrorInfo err_info = FunctionA(2);CHK_ERR(err_info);
-
-  return ERROR_INFO_SUCCESS;
-}
-
-// In this test case, MB_NOT_IMPLEMENTED is returned by FunctionC
-// The error message is built with C++ streaming
-ErrorInfo TestErrorHandling_3()
-{
-  ErrorInfo err_info = FunctionA(3);CHK_ERR(err_info);
-
-  return ERROR_INFO_SUCCESS;
-}
 #else
 ErrorCode FunctionC(int n)
 {
@@ -84,7 +64,7 @@ ErrorCode FunctionC(int n)
   }
   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)");
+    SET_ERR_STR(MB_NOT_IMPLEMENTED, "n = " << n << ", FunctionC() created a contrived error MB_NOT_IMPLEMENTED (this message is built with C++ streaming)");
   }
 
   return MB_SUCCESS;
@@ -103,7 +83,34 @@ ErrorCode FunctionA(int n)
 
   return MB_SUCCESS;
 }
+#endif
 
+#ifdef USE_ERROR_INFO_CLASS
+// In this test case, no error occurs in the call hierarchy
+ErrorInfo TestErrorHandling_1()
+{
+  ErrorInfo err_info = FunctionA(1);CHK_ERR(err_info);
+
+  return ERROR_INFO_SUCCESS;
+}
+
+// In this test case, a contrived error MB_FILE_WRITE_ERROR is returned by FunctionC
+ErrorInfo TestErrorHandling_2()
+{
+  ErrorInfo err_info = FunctionA(2);CHK_ERR(err_info);
+
+  return ERROR_INFO_SUCCESS;
+}
+
+// In this test case, a contrived error MB_NOT_IMPLEMENTED is returned by FunctionC
+// The error message is built with C++ streaming
+ErrorInfo TestErrorHandling_3()
+{
+  ErrorInfo err_info = FunctionA(3);CHK_ERR(err_info);
+
+  return ERROR_INFO_SUCCESS;
+}
+#else
 // In this test case, no error occurs in the call hierarchy
 ErrorCode TestErrorHandling_1()
 {
@@ -112,7 +119,7 @@ ErrorCode TestErrorHandling_1()
   return MB_SUCCESS;
 }
 
-// In this test case, MB_FILE_WRITE_ERROR is returned by FunctionC
+// In this test case, a contrived error MB_FILE_WRITE_ERROR is returned by FunctionC
 ErrorCode TestErrorHandling_2()
 {
   ErrorCode err_code = FunctionA(2);CHK_ERR(err_code);
@@ -120,7 +127,7 @@ ErrorCode TestErrorHandling_2()
   return MB_SUCCESS;
 }
 
-// In this test case, MB_NOT_IMPLEMENTED is returned by FunctionC
+// In this test case, a contrived error MB_NOT_IMPLEMENTED is returned by FunctionC
 // The error message is built with C++ streaming
 ErrorCode TestErrorHandling_3()
 {
@@ -128,10 +135,38 @@ ErrorCode TestErrorHandling_3()
 
   return MB_SUCCESS;
 }
+
+// In this test case, a real error MB_FILE_DOES_NOT_EXIST is returned by MOAB
+ErrorCode TestErrorHandling_4()
+{
+  Core moab;
+  Interface& mb = moab;
+
+  // Load a file that does not exist
+  ErrorCode rval = mb.load_file("no_such_file.nc");CHK_ERR(rval);
+
+  return MB_SUCCESS;
+}
+
+// In this test case, a real error MB_INDEX_OUT_OF_RANGE is returned by MOAB
+ErrorCode TestErrorHandling_5()
+{
+  Core moab;
+  Interface& mb = moab;
+
+  // Load a file that is not recognized by MOAB
+  ErrorCode rval = mb.load_file("TestErrorHandling.cpp");CHK_ERR(rval);
+
+  return MB_SUCCESS;
+}
 #endif
 
-int main()
+int main(int argc, char** argv)
 {
+#ifdef USE_MPI
+  MPI_Init(&argc, &argv);
+#endif
+
   MBErrorHandler_Init();
 
   cout << "--------------------Running test case 1 start--------------------" << endl;
@@ -146,7 +181,22 @@ int main()
   TestErrorHandling_3();
   cout << "--------------------Running test case 3 end----------------------\n" << endl;
 
+#ifdef USE_ERROR_INFO_CLASS
+#else
+  cout << "--------------------Running test case 4 start--------------------" << endl;
+  TestErrorHandling_4();
+  cout << "--------------------Running test case 4 end----------------------\n" << endl;
+
+  cout << "--------------------Running test case 5 start--------------------" << endl;
+  TestErrorHandling_5();
+  cout << "--------------------Running test case 5 end----------------------\n" << endl;
+#endif
+
   MBErrorHandler_Finalize();
 
+#ifdef USE_MPI
+  MPI_Finalize();
+#endif
+
   return 0;
 }

diff --git a/src/Core.cpp b/src/Core.cpp
index 0bd882f..8012355 100644
--- a/src/Core.cpp
+++ b/src/Core.cpp
@@ -490,10 +490,12 @@ ErrorCode Core::load_file( const char* file_name,
 #endif
   }
   else {
-    if (set_tag_name && num_set_tag_vals)
-      rval = serial_load_file( file_name, file_set, opts, &sl );
-    else
-      rval = serial_load_file( file_name, file_set, opts );
+    if (set_tag_name && num_set_tag_vals) {
+      rval = serial_load_file( file_name, file_set, opts, &sl );CHK_ERR(rval);
+    }
+    else {
+      rval = serial_load_file( file_name, file_set, opts );CHK_ERR(rval);
+    }
   }
 
   if (MB_SUCCESS == rval && !opts.all_seen()) {
@@ -545,7 +547,8 @@ ErrorCode Core::serial_load_file( const char* file_name,
 #endif
   if (status) {
     mError->set_last_error( "%s: %s", file_name, strerror(errno) );
-    return MB_FILE_DOES_NOT_EXIST;
+    //return MB_FILE_DOES_NOT_EXIST;
+    SET_ERR_STR(MB_FILE_DOES_NOT_EXIST, file_name << ": " << strerror(errno));
   }
 #if defined(WIN32) || defined(WIN64) || defined(MSC_VER)
   else if (_S_IFDIR(stat_data.st_mode)) {
@@ -608,6 +611,7 @@ ErrorCode Core::serial_load_file( const char* file_name,
 
   if (MB_SUCCESS != rval) {
     clean_up_failed_read( initial_ents, initial_tags );
+    SET_ERR(rval, "Failed to load file after trying all possible readers");
   }
   else if (file_set) {
     Range new_ents;

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