Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6474943

JSR 223 exceptions lack critical diagnostic info from Rhino

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 6
    • core-libs
    • b03
    • x86
    • linux
    • Verified

      Download the Dojo AJAX framework and see if you can load it in a plain Rhino environment without any browser bindings. You can't, of course, but the interesting question is why not - what bindings are missing? Let's try it:

      ---%<---
      $ /space/jdk1.6/bin/jrunscript -e 'load("dojo.js")'
      script error: sun.org.mozilla.javascript.internal.WrappedException: Wrapped javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166 (<system-init>#294) in <system-init> at line number 294
      ---%<---

      OK... not sure what that means, but let's take a look at dojo.js line 166:

      ---%<---
      dojo.raise = function(/*String*/ message, /*Error?*/ exception){
      // summary: Throw an error message, appending text of 'exception' if provided.
      // note: Also prints a message to the user using 'dojo.hostenv.println'.
      if(exception){
      message = message + ": "+dojo.errorToString(exception);
      }

      // print the message to the user if hostenv.println is defined
      try { dojo.hostenv.println("FATAL: "+message); } catch (e) {}

      throw Error(message);
      }
      ---%<---

      So, it's an error. That I knew already! The question is, why was it thrown? dojo.raise is called from about 20 different places. The 'message' parameter given to the function doesn't seem to appear at all.

      If you use a try...catch block you can try to extract some more information:

      ---%<---
      $ /space/jdk1.6/bin/jrunscript -e 'function dump(x) {for (p in x) {println(p + "=" + x[p])}} try {load("dojo.js")} catch (e) {dump(e)}'
      fileName=<system-init>
      rhinoException=sun.org.mozilla.javascript.internal.WrappedException: Wrapped javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166 (<system-init>#294)
      message=javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166
      javaException=javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166
      name=JavaException
      lineNumber=294
      ---%<---

      Hmm, looks like e.javaException is the root cause:

      ---%<---
      $ /space/jdk1.6/bin/jrunscript -e 'function dump(x) {for (p in x) {println(p + "=" + x[p])}} try {load("dojo.js")} catch (e) {dump(e.javaException)}'
      [....lots of output...]
      cause=null
      message=sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166
      class=class javax.script.ScriptException
      ---%<---

      No help here; message is just a string, and there is no meaningful stack trace:

      ---%<---
      $ /space/jdk1.6/bin/jrunscript -e 'function dump(x) {for (p in x) {println(p + "=" + x[p])}} try {load("dojo.js")} catch (e) {e.javaException.printStackTrace()}'
      javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (dojo.js#166) in dojo.js at line number 166
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at sun.org.mozilla.javascript.internal.MemberBox.invoke(MemberBox.java:131)
      at sun.org.mozilla.javascript.internal.NativeJavaMethod.call(NativeJavaMethod.java:190)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.callSpecial(ScriptRuntime.java:2082)
      at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:2973)
      at sun.org.mozilla.javascript.internal.Interpreter.interpret(Interpreter.java:2239)
      at sun.org.mozilla.javascript.internal.InterpretedFunction.call(InterpretedFunction.java:138)
      at sun.org.mozilla.javascript.internal.ContextFactory.doTopCall(ContextFactory.java:323)
      at sun.org.mozilla.javascript.internal.ScriptRuntime.doTopCall(ScriptRuntime.java:2747)
      at sun.org.mozilla.javascript.internal.InterpretedFunction.exec(InterpretedFunction.java:149)
      at sun.org.mozilla.javascript.internal.Context.evaluateReader(Context.java:1152)
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:106)
      at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:124)
      at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)
      at com.sun.tools.script.shell.Main.evaluateString(Main.java:280)
      at com.sun.tools.script.shell.Main.evaluateString(Main.java:301)
      at com.sun.tools.script.shell.Main.access$300(Main.java:19)
      at com.sun.tools.script.shell.Main$3.run(Main.java:199)
      at com.sun.tools.script.shell.Main.main(Main.java:30)
      ---%<---

      So, we're stuck. Why does the program fail?

      If you then apply the patch I suggest to the JRE, you immediately get something more useful:

      ---%<---
      $ /space/jdk1.6/bin/jrunscript -J-Xbootclasspath/p:....patch.... -e 'load("dojo.js")'
      script error: sun.org.mozilla.javascript.internal.WrappedException: Wrapped javax.script.ScriptException: Error: no window object in dojo.js at line number 166 (<system-init>#294) in <system-init> at line number 294
      ---%<---

      Still no useful stack trace (probably interpreted JavaScript can't give one), but at least we have "no window object". A simple search in dojo.js yields

      ---%<---
      if(typeof window == 'undefined'){
      dojo.raise("no window object");
      }
      ---%<---

      which tells us that we forgot to define the 'window' top-level object.

            sundar Sundararajan Athijegannathan
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: