trafodion-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sure...@apache.org
Subject [1/5] incubator-trafodion git commit: [TRAFODION-1988] Better java exception handling in the java/JNI layer
Date Fri, 23 Sep 2016 15:31:14 GMT
Repository: incubator-trafodion
Updated Branches:
  refs/heads/master 951c15d01 -> 3ad30f51e


[TRAFODION-1988] Better java exception handling in the java/JNI layer

The stack trace of the exception displayed from JNI side now contains the
cause of the exception displayed in a nested way.


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/commit/0037eab5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/tree/0037eab5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafodion/diff/0037eab5

Branch: refs/heads/master
Commit: 0037eab52931483e27bcd747f362361a360a4d5a
Parents: 16ebe09
Author: selvaganesang <selva.govindarajan@esgyn.com>
Authored: Tue Sep 20 05:49:50 2016 +0000
Committer: selvaganesang <selva.govindarajan@esgyn.com>
Committed: Tue Sep 20 05:49:50 2016 +0000

----------------------------------------------------------------------
 .../tm/hbasetmlib2/javaobjectinterfacetm.cpp    | 33 ++++++++++++++------
 .../tm/hbasetmlib2/javaobjectinterfacetm.h      |  2 ++
 core/sql/executor/JavaObjectInterface.cpp       | 21 +++++++++++--
 core/sql/executor/JavaObjectInterface.h         |  2 ++
 4 files changed, 46 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0037eab5/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.cpp
----------------------------------------------------------------------
diff --git a/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.cpp b/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.cpp
index df7db02..7f7cd08 100755
--- a/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.cpp
+++ b/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.cpp
@@ -54,6 +54,7 @@ jclass JavaObjectInterfaceTM::gStackTraceClass = NULL;
 jmethodID JavaObjectInterfaceTM::gGetStackTraceMethodID = NULL;
 jmethodID JavaObjectInterfaceTM::gThrowableToStringMethodID = NULL;
 jmethodID JavaObjectInterfaceTM::gStackFrameToStringMethodID = NULL;
+jmethodID JavaObjectInterfaceTM::gGetCauseMethodID = NULL;
 
 //////////////////////////////////////////////////////////////////////////////
 // 
@@ -389,7 +390,7 @@ void JavaObjectInterfaceTM::logError(const char* cat, const char* file,
int line
 
 bool  JavaObjectInterfaceTM::getExceptionDetails(JNIEnv *jenv)
 {
-   std::string *error_msg;
+   std::string *error_msg = NULL;
 
    if (_tlp_error_msg != NULL)
    {
@@ -421,21 +422,32 @@ bool  JavaObjectInterfaceTM::getExceptionDetails(JNIEnv *jenv)
        _tlp_error_msg = error_msg;
        return false;
    }
+
+   appendExceptionMessages(jenv, a_exception, error_msg);
+   *error_msg += "\n";
+   _tlp_error_msg = error_msg;
+   return true;
+}
+
+void JavaObjectInterfaceTM::appendExceptionMessages(JNIEnv *jenv, jthrowable a_exception,
std::string *error_msg)
+{
     jstring msg_obj =
        (jstring) jenv->CallObjectMethod(a_exception,
                                          gThrowableToStringMethodID);
     const char *msg_str;
-    error_msg = new std::string("");
-    if (msg_obj != NULL) {
+    if (msg_obj != NULL)
+    {
        msg_str = jenv->GetStringUTFChars(msg_obj, 0);
+       // Start the error message in a new line
+       *error_msg = "\n";
        *error_msg += msg_str;
        jenv->ReleaseStringUTFChars(msg_obj, msg_str);
        jenv->DeleteLocalRef(msg_obj);
     }
-    else {
+    else
        msg_str = "Exception is thrown, but tostring is null";
-       *error_msg += msg_str;
-    }
+
+
     // Get the stack trace
     jobjectArray frames =
         (jobjectArray) jenv->CallObjectMethod(
@@ -444,7 +456,7 @@ bool  JavaObjectInterfaceTM::getExceptionDetails(JNIEnv *jenv)
     if (frames == NULL)
     {
        _tlp_error_msg = error_msg;
-       return true;
+       return;
     }
     jsize frames_length = jenv->GetArrayLength(frames);
 
@@ -465,7 +477,10 @@ bool  JavaObjectInterfaceTM::getExceptionDetails(JNIEnv *jenv)
        }
     }
     *error_msg += "\n";
-    _tlp_error_msg = error_msg;
-    return true;
+    jthrowable j_cause = (jthrowable)jenv->CallObjectMethod(a_exception, gGetCauseMethodID);
+    if (j_cause != NULL) {
+       *error_msg += "Caused by ";
+       appendExceptionMessages(jenv, j_cause, error_msg);
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0037eab5/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.h
----------------------------------------------------------------------
diff --git a/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.h b/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.h
index 3f8d41b..f108c4d 100755
--- a/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.h
+++ b/core/sqf/src/seatrans/tm/hbasetmlib2/javaobjectinterfacetm.h
@@ -142,6 +142,7 @@ public:
     return isInitialized_;
   }
   bool getExceptionDetails(JNIEnv *jenv);
+  void appendExceptionMessages(JNIEnv *jenv, jthrowable a_exception, std::string *error_msg);
   
 protected:
   static JavaVM*   jvm_;
@@ -156,6 +157,7 @@ protected:
   static jmethodID gGetStackTraceMethodID;
   static jmethodID gThrowableToStringMethodID;
   static jmethodID gStackFrameToStringMethodID;
+  static jmethodID gGetCauseMethodID;
 };
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0037eab5/core/sql/executor/JavaObjectInterface.cpp
----------------------------------------------------------------------
diff --git a/core/sql/executor/JavaObjectInterface.cpp b/core/sql/executor/JavaObjectInterface.cpp
index e056ca8..beab4ed 100644
--- a/core/sql/executor/JavaObjectInterface.cpp
+++ b/core/sql/executor/JavaObjectInterface.cpp
@@ -43,6 +43,7 @@ jclass JavaObjectInterface::gStackTraceClass = NULL;
 jmethodID JavaObjectInterface::gGetStackTraceMethodID = NULL;
 jmethodID JavaObjectInterface::gThrowableToStringMethodID = NULL;
 jmethodID JavaObjectInterface::gStackFrameToStringMethodID = NULL;
+jmethodID JavaObjectInterface::gGetCauseMethodID = NULL;
 
   
 static const char* const joiErrorEnumStr[] = 
@@ -296,6 +297,9 @@ JOI_RetCode JavaObjectInterface::initJVM()
         gThrowableToStringMethodID = jenv_->GetMethodID(gThrowableClass,
                       "toString",
                       "()Ljava/lang/String;");
+        gGetCauseMethodID = jenv_->GetMethodID(gThrowableClass,
+                      "getCause",
+                      "()Ljava/lang/Throwable;");
      }
   }
   if (gStackTraceClass == NULL)
@@ -455,6 +459,14 @@ NABoolean  JavaObjectInterface::getExceptionDetails(JNIEnv *jenv)
        cli_globals->setJniErrorStr(error_msg);
        return FALSE;
    }
+   appendExceptionMessages(jenv, a_exception, error_msg);
+   error_msg += "\n";
+   cli_globals->setJniErrorStr(error_msg);
+   return TRUE;
+}
+
+void JavaObjectInterface::appendExceptionMessages(JNIEnv *jenv, jthrowable a_exception, NAString
&error_msg)
+{
     jstring msg_obj =
        (jstring) jenv->CallObjectMethod(a_exception,
                                          gThrowableToStringMethodID);
@@ -480,7 +492,7 @@ NABoolean  JavaObjectInterface::getExceptionDetails(JNIEnv *jenv)
     if (frames == NULL)
     {
        cli_globals->setJniErrorStr(error_msg);
-       return TRUE;
+       return;
     }
     jsize frames_length = jenv->GetArrayLength(frames);
 
@@ -501,8 +513,11 @@ NABoolean  JavaObjectInterface::getExceptionDetails(JNIEnv *jenv)
        }
     }
     error_msg += "\n";
-    cli_globals->setJniErrorStr(error_msg);
-    return TRUE;
+    jthrowable j_cause = (jthrowable)jenv->CallObjectMethod(a_exception, gGetCauseMethodID);
+    if (j_cause != NULL) {
+       error_msg += "Caused by ";
+       appendExceptionMessages(jenv, j_cause, error_msg);
+    }
 } 
 
 NAString JavaObjectInterface::getLastError()

http://git-wip-us.apache.org/repos/asf/incubator-trafodion/blob/0037eab5/core/sql/executor/JavaObjectInterface.h
----------------------------------------------------------------------
diff --git a/core/sql/executor/JavaObjectInterface.h b/core/sql/executor/JavaObjectInterface.h
index 65549f0..660faa0 100644
--- a/core/sql/executor/JavaObjectInterface.h
+++ b/core/sql/executor/JavaObjectInterface.h
@@ -160,6 +160,7 @@ public:
   // Pass in jenv if the thread where the object is created is different than
   // the thread where exception occurred
   NABoolean getExceptionDetails(JNIEnv *jenv = NULL);  
+  void appendExceptionMessages(JNIEnv *jenv, jthrowable a_exception, NAString &error_msg);
   
   NAHeap *getHeap() { return heap_; }
 protected:
@@ -169,6 +170,7 @@ protected:
   static jmethodID gGetStackTraceMethodID;
   static jmethodID gThrowableToStringMethodID;
   static jmethodID gStackFrameToStringMethodID;
+  static jmethodID gGetCauseMethodID;
   static jint jniHandleCapacity_;
 
   jobject   javaObj_;


Mime
View raw message