-
Notifications
You must be signed in to change notification settings - Fork 900
Description
Currently, Rhino wraps native Java exceptions thrown by host functions with the WrappedException class. If a script catches a WrappedException, Rhino exposes an error object to the catch block with a message like "JavaException: java.lang.RuntimeException: exception message". For my use case, users running scripts do not know that their JavaScript code is being executed in Java, so it's confusing that some exception messages are prefixed by "JavaException: (Java class name):".
This code is implemented in the static function ScriptRuntime.newCatchScope
(https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/ScriptRuntime.java#L3962). As far as I can tell, it currently doesn't seem possible to override this behavior other than by updating every jsFunction_
function to catch all exceptions and rethrow EvaluatorException.
To allow some Rhino users to avoid exposing implementation details of Java exceptions, while maintaining the current behavior for users who prefer to allow scripts to see details of Java exceptions, one approach could be to add a context feature flag and update ScriptRuntime.newCatchScope
like this:
else if (t instanceof WrappedException) {
WrappedException we = (WrappedException) t;
re = we;
javaException = we.getWrappedException();
if (cx.hasFeature(Context.FEATURE_HIDE_WRAPPED_EXCEPTIONS)) {
type = TopLevel.NativeErrors.InternalError;
errorMsg = javaException.getMessage();
} else {
type = TopLevel.NativeErrors.JavaException;
errorMsg = javaException.getClass().getName() + ": " + javaException.getMessage();
}
}
Are there any simpler ways to do this?
I could submit a PR for this if adding a Context feature flag seems like a good approach.