I'm starting to run into the dirty little secrets of what is an otherwise very useful JSR223 scripting environment.
I'm using the builtin version of Rhino shipped with Java 6 SE, accessing it through JSR223's ScriptingEngine
et al.
When I get an exception caused by a Java object I've exported into the Javascript environment, it is a ScriptingException
that wraps a sun.org.mozilla.javascript.internal.WrappedException
that wraps my real exception (e.g. UnsupportedOperationException
or whatever)
The ScriptingException
returns null for getFileName() and -1 for getLineNumber().
But when I look at the message and at the debugger, the WrappedException
has the correct filename and line number, it's just not publishing it via the ScriptingException's getter methods.
Great. Now what do I do? I don't know how I'm going to use sun.org.mozilla.javascript.internal.wrappedException which isn't a public class anyway.
Argh. Java 6's Rhino does the same thing (doesn't publish the file name / line number / etc via ScriptingException's methods) with
sun.org.mozilla.javascript.internal.EvaluatorException
and who knows how many other exceptions.The only reasonable way I can think of to handle this is to use reflection. Here's my solution.