Skip to content

Commit b21e1cb

Browse files
committed
- only call getCause for InvocationTargetException exceptions
1 parent 788df96 commit b21e1cb

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/methodCallBaton.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,17 @@ v8::Handle<v8::Value> MethodCallBaton::resultsToV8(JNIEnv *env) {
8383
v8::HandleScope scope;
8484

8585
if(m_error) {
86-
jclass throwableClazz = env->FindClass("java/lang/Throwable");
87-
jmethodID throwable_getCause = env->GetMethodID(throwableClazz, "getCause", "()Ljava/lang/Throwable;");
88-
jthrowable cause = (jthrowable)env->CallObjectMethod(m_error, throwable_getCause);
86+
jthrowable cause = m_error;
87+
88+
// if we've caught an InvocationTargetException exception,
89+
// let's grab the cause. users don't necessarily know that
90+
// we're invoking the methods through reflection
91+
jclass invocationExceptionClazz = env->FindClass("java/lang/reflect/InvocationTargetException");
92+
if (env->IsInstanceOf(m_error, invocationExceptionClazz)) {
93+
jclass throwableClazz = env->FindClass("java/lang/Throwable");
94+
jmethodID throwable_getCause = env->GetMethodID(throwableClazz, "getCause", "()Ljava/lang/Throwable;");
95+
cause = (jthrowable)env->CallObjectMethod(m_error, throwable_getCause);
96+
}
8997

9098
v8::Handle<v8::Value> err = javaExceptionToV8(m_java, env, cause, m_errorString);
9199
return scope.Close(err);

0 commit comments

Comments
 (0)