![](/img/trans.png)
[英]javascript evaluation of ruby object inside haml throw undefine method exception
[英]Throw an exception from inside javascript
我有一個使用Activiti引擎的項目。 它支持使用Nashorn運行腳本。 在腳本任務或任務偵聽器中運行此代碼沒有問題。 但是當使用執行監聽器時,我遇到了問題。
在我的腳本中,我想拋出一個應該被java代碼捕獲的錯誤。 例如:
throw new Error("this is an error");
但是我收到一個錯誤:
problem evaluating script: Error: this is an error in scripts/error.js at line number 8 at column number 1
我最初也試過這個:
var BpmnError = Java.type(org.activiti.engine.delegate.BpmnError');
throw new BpmnError("BusinessExeptionOccured","a Message");
在這種情況下,沒有捕獲錯誤,就好像拋出從未發生過一樣。
在Activiti文檔中,它聲明:
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
我還沒有找到任何如何做到這一點的例子。
我還沒有看到任何可以拋出jdk.nashorn.internal.runtime.ECMAException的JavaScript代碼示例。在opendJDK ECMAException狀態中的注釋:
Exception used to implement ECMAScript "throw" from scripts.
任何幫助都將不勝感激。
您可以捕獲ScriptException,然后從那里訪問拋出的ECMAScript對象。
示例代碼:
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"));
}
}
}
}
}
在Activiti腳本任務中,您可以使用以下內容:
throw ("Message")
我希望這對你有幫助
拋出“這是一個錯誤”;
請參閱此處獲取解釋: JavaScript錯誤處理 。 由於Nashorn實現了ECMAScript 5.1(正如在這里聲稱的OpenJDK Wiki,Nashorn擴展 ),正常的JavaScript錯誤處理應該可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.