我在grails应用程序中使用ScriptEngineManager来执行javascript代码。
当我执行此javascript时:
var x = new java.util.ArrayList([1, 2, 3])
log(x[55])
使用此代码:
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript")
return engine.eval(javascript)
} catch (ScriptException jsEx) {
// Exception is added in outer function to the log
throw new JavascriptException(jsEx.getMessage(), jsEx)
} catch (Exception e) {
throw new JavascriptException("UNEXPECTED Exception in invokeJavaScript " + e.getMessage(), e)
}
我在执行e中得到java.lang.IndexOutOufBoundsException
。可以,但是我想知道javascript中哪一行发生了错误。是否有可能从javascript代码获得最后成功的一行?
我找到了解决我问题的方法。我解析了异常的堆栈跟踪,所以我得到了行号:
//this method gives me the stacktrace as string
String trace = helperService.stacktraceToString(e)
String line = ""
trace.eachLine {
if (it.contains("<eval>")) {
line = " at line " + it.substring(it.lastIndexOf("<eval>:"), it.length() -1).replace("<eval>:", "")
}
}