Skip to content

Commit

Permalink
Handle exception cause with null message
Browse files Browse the repository at this point in the history
  • Loading branch information
UweKubosch committed Apr 23, 2018
1 parent 7335e21 commit 4009259
Show file tree
Hide file tree
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
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down

0 comments on commit 4009259

Please sign in to comment.