Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better exception handling more tests

  • Loading branch information...
commit aea8a07b1c07b46dc6a1100dda86403584f73c55 1 parent d2bac5a
Joe Ferner authored
27 src/utils.cpp
View
@@ -2,6 +2,7 @@
#include "utils.h"
#include <string.h>
#include <algorithm>
+#include <sstream>
#include "javaObject.h"
std::list<jobject> javaReflectionGetMethods(JNIEnv *env, jclass clazz) {
@@ -266,8 +267,30 @@ jarray v8ToJava(JNIEnv* env, const v8::Arguments& args, int start, int end, std:
v8::Handle<v8::Value> javaExceptionToV8(JNIEnv* env, jthrowable ex, const std::string& alternateMessage) {
v8::HandleScope scope;
- std::string msg = alternateMessage + "\n" + javaObjectToString(env, ex);
- return scope.Close(v8::Exception::TypeError(v8::String::New(msg.c_str())));
+ std::ostringstream msg;
+ msg << alternateMessage;
+
+ if(ex) {
+ jclass stringWriterClazz = env->FindClass("java/io/StringWriter");
+ jmethodID stringWriter_constructor = env->GetMethodID(stringWriterClazz, "<init>", "()V");
+ jmethodID stringWriter_toString = env->GetMethodID(stringWriterClazz, "toString", "()Ljava/lang/String;");
+ jobject stringWriter = env->NewObject(stringWriterClazz, stringWriter_constructor);
+
+ jclass printWriterClazz = env->FindClass("java/io/PrintWriter");
+ jmethodID printWriter_constructor = env->GetMethodID(printWriterClazz, "<init>", "(Ljava/io/Writer;)V");
+ jobject printWriter = env->NewObject(printWriterClazz, printWriter_constructor, stringWriter);
+
+ jclass throwableClazz = env->FindClass("java/lang/Throwable");
+ jmethodID throwable_printStackTrace = env->GetMethodID(throwableClazz, "printStackTrace", "(Ljava/io/PrintWriter;)V");
+ env->CallObjectMethod(ex, throwable_printStackTrace, printWriter);
+
+ jstring strObj = (jstring)env->CallObjectMethod(stringWriter, stringWriter_toString);
+ std::string stackTrace = javaToString(env, strObj);
+
+ msg << "\n" << stackTrace;
+ }
+
+ return scope.Close(v8::Exception::Error(v8::String::New(msg.str().c_str())));
}
v8::Handle<v8::Value> javaExceptionToV8(JNIEnv* env, const std::string& alternateMessage) {
2  test/Test.java
View
@@ -6,4 +6,4 @@ public Test() {}
public Test(int i) { this.i = i; }
public int getInt() { return i; }
-}
+}
BIN  test/TestExceptions.class
View
Binary file not shown
4 test/TestExceptions.java
View
@@ -0,0 +1,4 @@
+
+public class TestExceptions {
+ public TestExceptions(Exception ex) throws Exception { throw ex; }
+}
36 test/java-test.js
View
@@ -64,4 +64,40 @@ exports['Java'] = nodeunit.testCase({
});
test.done();
},
+
+ "newInstance bad number of args": function(test) {
+ java.newInstance("Test", 42, "z", function(err, result) {
+ test.ok(err);
+ test.ok(!result);
+ test.done();
+ });
+ },
+
+ "newInstanceSync bad number of args": function(test) {
+ test.throws(function() {
+ java.newInstanceSync("Test", 42, "z");
+ });
+ test.done();
+ },
+
+ "newInstance exception thrown from constructor": function(test) {
+ var ex = java.newInstanceSync("java.lang.Exception", "my exception");
+ java.newInstance("TestExceptions", ex, function(err, result) {
+ test.ok(err);
+ test.ok(err.toString().match(/my exception/));
+ test.ok(!result);
+ test.done();
+ });
+ },
+
+ "newInstanceSync exception thrown from constructor": function(test) {
+ var ex = java.newInstanceSync("java.lang.Exception", "my exception");
+ try {
+ java.newInstanceSync("TestExceptions", ex);
+ test.fail("should throw");
+ } catch(err) {
+ test.ok(err.toString().match(/my exception/));
+ }
+ test.done();
+ },
});
Please sign in to comment.
Something went wrong with that request. Please try again.