3

I have a script that works just fine in Chrome but crashes on Nashorn with:

(Error: Namespace "com.cognitect.transit.util" already declared. in at line number 19664 at column number 6

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.

How do I get an JavaScript stack trace from Nashorn?

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. 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.

2
  • JavaScript errors generate their stack trace when they're created. You can simply surround your code with a try/catch clause and then read the stack no matter where you catch it. Sep 11, 2015 at 18:56
  • @BenjaminGruenbaum Are you referring to try/catch in JavaScript? Sep 11, 2015 at 19:35

1 Answer 1

7

The NashornException is available as cause of the ScriptException:

import jdk.nashorn.api.scripting.NashornException;

...

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.