Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exception cause with null message #5146

Merged
merged 1 commit into from Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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