简体   繁体   English

你如何从Nashorn获得JavaScript堆栈跟踪?

[英]How do you get the JavaScript stack trace from Nashorn?

I have a script that works just fine in Chrome but crashes on Nashorn with: 我有一个在Chrome中工作得很好但在Nashorn崩溃的脚本:

(Error: Namespace "com.cognitect.transit.util" already declared. in at line number 19664 at column number 6 (错误:名称空间“com.cognitect.transit.util”已在第6栏第19664行声明。

That error is not very useful, as that line contains: 该错误不是很有用,因为该行包含:

  throw Error('Namespace "' + name + '" already declared.');

I need to get a full stack trace from Nashorn, I found NashornException.getScriptStackString but the error Nashorn is generating is of type javax.script.ScriptException which gives me an empty string when I call NashornException.getScriptStackString . 我需要从Nashorn获得一个完整的堆栈跟踪,我找到了NashornException.getScriptStackString但是Nashorn生成的错误是javax.script.ScriptException类型,当我调用NashornException.getScriptStackString时,它给了我一个空字符串。

How do I get an JavaScript stack trace from Nashorn? 如何从Nashorn获取JavaScript堆栈跟踪?

I don't want to do it from JavaScript, I want to do it the same way the browser does it, no matter what JS code you are running. 我不想从JavaScript中做到这一点,无论你运行什么JS代码,我都希望以与浏览器相同的方式实现它。 A lot of my JS code is third party, it's generated, I cannot modify the thousand of functions I have to print exceptions just in case. 很多我的JS代码都是第三方,它是生成的,我无法修改我必须打印异常的数千个函数以防万一。

The NashornException is available as cause of the ScriptException : NashornException可作为原因ScriptException

import jdk.nashorn.api.scripting.NashornException;

...

catch (ScriptException e) {
     if (e.getCause() instance of NashornException) 
         String jsStackTrace = NashornException.getScriptStackString(e.getCause());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM