Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle exception cause with null message
Browse files Browse the repository at this point in the history
UweKubosch committed Apr 23, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 7335e21 commit 4009259
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/org/jruby/embed/jsr223/JRubyEngine.java
Original file line number Diff line number Diff line change
@@ -214,7 +214,8 @@ public Object invokeMethod(Object receiver, String method, Object... args)
}
return container.callMethod(receiver, method, args, Object.class);
} catch (Exception e) {
if (e.getCause().getMessage().contains("undefined method")) {
if (e.getCause() != null && e.getCause().getMessage() != null
&& e.getCause().getMessage().contains("undefined method")) {
throw wrapMethodException(e);
}
throw wrapException(e);
@@ -239,7 +240,8 @@ public Object invokeFunction(String method, Object... args)
}
return container.callMethod(container.getProvider().getRuntime().getTopSelf(), method, args, Object.class);
} catch (Exception e) {
if (e.getCause().getMessage().contains("undefined method")) {
if (e.getCause() != null && e.getCause().getMessage() != null
&& e.getCause().getMessage().contains("undefined method")) {
throw wrapMethodException(e);
}
throw wrapException(e);
30 changes: 30 additions & 0 deletions core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java
Original file line number Diff line number Diff line change
@@ -617,6 +617,21 @@ public void testInvokeMethod() throws Exception {
instance.getBindings(ScriptContext.ENGINE_SCOPE).clear();
}

/**
* Test of invokeMethod method throwing an exception with a null message.
*/
@Test
public void testInvokeMethodWithJavaException() throws Exception {
ScriptEngine instance = newScriptEngine();
instance.eval("def trigger_npe\nraise java.lang.NullPointerException.new\nend");
try {
Object result = ((Invocable) instance).invokeMethod(Object.class,"trigger_npe", null);
fail("Expected javax.script.ScriptException");
} catch (javax.script.ScriptException sex) {
// javax.script.ScriptException is expected
}
}

/**
* Test of invokeFunction method, of class Jsr223JRubyEngine.
*/
@@ -643,6 +658,21 @@ public void testInvokeFunction() throws Exception {
instance.getBindings(ScriptContext.ENGINE_SCOPE).clear();
}

/**
* Test of invokeFunction method throwing an exception with a null message.
*/
@Test
public void testInvokeFunctionWithJavaException() throws Exception {
ScriptEngine instance = newScriptEngine();
instance.eval("def trigger_npe\nraise java.lang.NullPointerException.new\nend");
try {
Object result = ((Invocable) instance).invokeFunction("trigger_npe", null);
fail("Expected javax.script.ScriptException");
} catch (javax.script.ScriptException sex) {
// javax.script.ScriptException is expected
}
}

/**
* Test of getInterface method, of class Jsr223JRubyEngine.
*/

0 comments on commit 4009259

Please sign in to comment.