Using the JDK 6 ScriptEngine
mechanism, anything that goes wrong during an "eval" or "invokeMethod" or whatever results in a ScriptException
being thrown back to the invoking Java environment. As far as I've been able to tell by experimentation and source code reading, the best I can do to get information back from Javascript when my Javascript code wants to throw an exception is to throw a string. That string shows up in the "getMessage" return value from the ScriptException
object. Not pretty.
It seems like it would be nice to be able to:
if (somethingWrong) {
throw { error: Errors.INVALID_SOMETHING, context: whatever() };
}
from Javascript, and then the Java code could somehow get at that object. I fear however that given the current implementation of the Rhino ScriptEngine
wrapper, that's just not possible. If anybody knows a trick that does work, however, I'd love to see it.