Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw an exception from inside javascript

I have a project that uses Activiti engine. It support running scripts using Nashorn. I have no problem running this code in either script tasks or in task listeners. But when using execution listeners I have issues.

In my script I want to throw an error that should be caught by the java code. For example:

    throw new Error("this is an error");

But I get an error:

    problem evaluating script: Error: this is an error in scripts/error.js at line number 8 at column number 1

I have also tried this initially:

    var BpmnError = Java.type(org.activiti.engine.delegate.BpmnError');
    throw new BpmnError("BusinessExeptionOccured","a Message");

In this case no error is caught as if the throw never happened.

In the Activiti documentation it states:

  As of Activiti 5.9, it is possible to throw BPMN Errors from user code inside Service Tasks or Script Tasks. In order to do this, a special ActivitiException called BpmnError can be thrown in JavaDelegates or scripts

I have not been able find any example of how this can be done.

I have also not seen any examples of JavaScript code that can throw a jdk.nashorn.internal.runtime.ECMAException A comment in the opendJDK ECMAException states:

Exception used to implement ECMAScript "throw" from scripts. 

Any help with this will be appreciated.

like image 376
Maudrid Avatar asked Aug 04 '26 05:08

Maudrid


1 Answers

You can catch the ScriptException and then access the thrown ECMAScript object from there.

Sample code:

import javax.script.*;
import jdk.nashorn.api.scripting.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ScriptEngineManager m = new ScriptEngineManager();
        ScriptEngine e = m.getEngineByName("nashorn");
        try {
            e.eval("throw new Error('this is an error');");
        } catch (ScriptException se) {
            // get the original cause
            Throwable cause = se.getCause();
            // in this case, the cause is a nashorn exception
            if (cause instanceof NashornException) {
                NashornException ne = (NashornException)cause;
                // Access the underlying ECMAScript error object thrown
                Object obj = ne.getEcmaError();
                // print ECMA object 'as is'
                System.out.println(obj);

                // In this example, the thrown ECMAScript object is
                // an instanceof Error. Script objects are accessible
                // as JSObject in java code.
                if (obj instanceof JSObject) {
                    JSObject jsObj = (JSObject)obj;
                    System.out.println(jsObj.getMember("message"));
                    System.out.println(jsObj.getMember("name"));
                    // access nashorn specific 'stack' property
                    System.out.println("stack trace: " + jsObj.getMember("stack"));
                }
            }
        }
    }
}
like image 89
A. Sundararajan Avatar answered Aug 05 '26 18:08

A. Sundararajan