src/jdk/nashorn/api/scripting/NashornException.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.api.scripting;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import jdk.nashorn.internal.codegen.CompilerConstants;
  31 import jdk.nashorn.internal.runtime.ECMAErrors;

  32 
  33 /**
  34  * This is base exception for all Nashorn exceptions. These originate from
  35  * user's ECMAScript code. Example: script parse errors, exceptions thrown from
  36  * scripts. Note that ScriptEngine methods like "eval", "invokeMethod",
  37  * "invokeFunction" will wrap this as ScriptException and throw it. But, there
  38  * are cases where user may need to access this exception (or implementation
  39  * defined subtype of this). For example, if java interface is implemented by a
  40  * script object or Java access to script object properties via java.util.Map
  41  * interface. In these cases, user code will get an instance of this or
  42  * implementation defined subclass.
  43  */
  44 @SuppressWarnings("serial")
  45 public abstract class NashornException extends RuntimeException {
  46     // script file name
  47     private final String fileName;
  48     // script line number
  49     private final int line;
  50     // script column number
  51     private final int column;


  52 
  53     /** script source name used for "engine.js" */
  54     public static final String ENGINE_SCRIPT_SOURCE_NAME = "nashorn:engine/resources/engine.js";
  55 
  56     /**
  57      * Constructor
  58      *
  59      * @param msg       exception message
  60      * @param fileName  file name
  61      * @param line      line number
  62      * @param column    column number
  63      */
  64     protected NashornException(final String msg, final String fileName, final int line, final int column) {
  65         this(msg, null, fileName, line, column);
  66     }
  67 
  68     /**
  69      * Constructor
  70      *
  71      * @param msg       exception message


 170      */
 171     public static String getScriptStackString(final Throwable exception) {
 172         final StringBuilder buf = new StringBuilder();
 173         final StackTraceElement[] frames = getScriptFrames(exception);
 174         for (final StackTraceElement st : frames) {
 175             buf.append("\tat ");
 176             buf.append(st.getMethodName());
 177             buf.append(" (");
 178             buf.append(st.getFileName());
 179             buf.append(':');
 180             buf.append(st.getLineNumber());
 181             buf.append(")\n");
 182         }
 183         final int len = buf.length();
 184         // remove trailing '\n'
 185         if (len > 0) {
 186             assert buf.charAt(len - 1) == '\n';
 187             buf.deleteCharAt(len - 1);
 188         }
 189         return buf.toString();





























 190     }
 191 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.api.scripting;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import jdk.nashorn.internal.codegen.CompilerConstants;
  31 import jdk.nashorn.internal.runtime.ECMAErrors;
  32 import jdk.nashorn.internal.runtime.ScriptObject;
  33 
  34 /**
  35  * This is base exception for all Nashorn exceptions. These originate from
  36  * user's ECMAScript code. Example: script parse errors, exceptions thrown from
  37  * scripts. Note that ScriptEngine methods like "eval", "invokeMethod",
  38  * "invokeFunction" will wrap this as ScriptException and throw it. But, there
  39  * are cases where user may need to access this exception (or implementation
  40  * defined subtype of this). For example, if java interface is implemented by a
  41  * script object or Java access to script object properties via java.util.Map
  42  * interface. In these cases, user code will get an instance of this or
  43  * implementation defined subclass.
  44  */
  45 @SuppressWarnings("serial")
  46 public abstract class NashornException extends RuntimeException {
  47     // script file name
  48     private final String fileName;
  49     // script line number
  50     private final int line;
  51     // script column number
  52     private final int column;
  53     // underlying ECMA error object - lazily initialized
  54     private Object ecmaError;
  55 
  56     /** script source name used for "engine.js" */
  57     public static final String ENGINE_SCRIPT_SOURCE_NAME = "nashorn:engine/resources/engine.js";
  58 
  59     /**
  60      * Constructor
  61      *
  62      * @param msg       exception message
  63      * @param fileName  file name
  64      * @param line      line number
  65      * @param column    column number
  66      */
  67     protected NashornException(final String msg, final String fileName, final int line, final int column) {
  68         this(msg, null, fileName, line, column);
  69     }
  70 
  71     /**
  72      * Constructor
  73      *
  74      * @param msg       exception message


 173      */
 174     public static String getScriptStackString(final Throwable exception) {
 175         final StringBuilder buf = new StringBuilder();
 176         final StackTraceElement[] frames = getScriptFrames(exception);
 177         for (final StackTraceElement st : frames) {
 178             buf.append("\tat ");
 179             buf.append(st.getMethodName());
 180             buf.append(" (");
 181             buf.append(st.getFileName());
 182             buf.append(':');
 183             buf.append(st.getLineNumber());
 184             buf.append(")\n");
 185         }
 186         final int len = buf.length();
 187         // remove trailing '\n'
 188         if (len > 0) {
 189             assert buf.charAt(len - 1) == '\n';
 190             buf.deleteCharAt(len - 1);
 191         }
 192         return buf.toString();
 193     }
 194     
 195     protected Object getThrown() {
 196         return null;
 197     }
 198     
 199     protected NashornException initEcmaError(final ScriptObject global) {
 200         if (ecmaError != null) {
 201             return this; // initialized already!
 202         }
 203         
 204         final Object thrown = getThrown();
 205         if (thrown instanceof ScriptObject) {
 206             ecmaError = ScriptObjectMirror.wrap(thrown, global);
 207         } else {
 208             ecmaError = thrown;
 209         }
 210         
 211         return this;
 212     }
 213 
 214     /**
 215      * Return the underlying ECMA error object, if available.
 216      *
 217      * @return underlying ECMA Error object's mirror or whatever was thrown
 218      *         from script such as a String, Number or a Boolean.
 219      */    
 220     public Object getEcmaError() {
 221         return ecmaError;
 222     }
 223 }