Clean JavaScript stack trace

A. Sundararajan sundararajan.athijegannathan at oracle.com
Fri Jun 20 16:22:06 UTC 2014


You can evaluate:

function getScriptTrace() {
   try {
      throw new Error();
   } catch (e) {
      return e.stack
   }
}

in your script and call the same from java code using 
javax.script.Invocable.invokeFunction. ("stack" property and other 
related extensions are documented here: 
https://wiki.openjdk.java.net/display/Nashorn/Nashorn+extensions).

Or if you're okay with nashorn specific extension, you can use this API 
from java code.

public static StackTraceElement[] 
jdk.nashorn.api.scripting.NashornException.getScriptFrames(Throwable th)

Hope this helps,
-Sundar

On Friday 20 June 2014 09:35 PM, Greg Brail wrote:
> Is there a way in Nashorn or javax.script to get a clean JavaScript-only
> stack trace (not a Java stack trace, but only containing JavaScript code)
> out of an exception thrown by a script? I don't see anything in the Nashorn
> Wiki or javax.script -- apologies if I'm missing something there.
>
> For instance, I can get a full Java stack trace when my script fails, but
> I'd like to ignore all the Java stuff and give users the stack trace that
> they would get if they were running the "plain" JavaScript natively rather
> than inside Java.
>



More information about the nashorn-dev mailing list