Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Hiding Java exception implementation details from catch block #911

Closed
youngj opened this issue May 25, 2021 · 0 comments · Fixed by #964
Closed

Hiding Java exception implementation details from catch block #911

youngj opened this issue May 25, 2021 · 0 comments · Fixed by #964
Labels
embedding Rhino Issues related to enbedding Rhino enhancement

Comments

@youngj
Copy link
Contributor

youngj commented May 25, 2021

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.

@p-bakker p-bakker added this to the Release 1.7.14 milestone Oct 13, 2021
@p-bakker p-bakker added enhancement Java Interop Issues related to the interaction between Java and JavaScript embedding Rhino Issues related to enbedding Rhino and removed Java Interop Issues related to the interaction between Java and JavaScript labels Oct 13, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
embedding Rhino Issues related to enbedding Rhino enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants