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
108 this.fileName = ste.getFileName();
109 this.line = ste.getLineNumber();
110 return;
111 }
112 }
113
114 this.fileName = null;
115 this.line = 0;
116 }
117
118 /**
119 * Get the source file name for this {@code NashornException}
120 *
121 * @return the file name
122 */
123 public final String getFileName() {
124 return fileName;
125 }
126
127 /**
128 * Get the line number for this {@code NashornException}
129 *
130 * @return the line number
131 */
132 public final int getLineNumber() {
133 return line;
134 }
135
136 /**
137 * Get the column for this {@code NashornException}
138 *
139 * @return the column
140 */
141 public final int getColumnNumber() {
142 return column;
143 }
144
145 /**
146 * Returns array javascript stack frames from the given exception object.
147 *
148 * @param exception exception from which stack frames are retrieved and filtered
149 * @return array of javascript stack frames
150 */
151 public static StackTraceElement[] getScriptFrames(final Throwable exception) {
152 final StackTraceElement[] frames = exception.getStackTrace();
153 final List<StackTraceElement> filtered = new ArrayList<>();
154 for (final StackTraceElement st : frames) {
155 if (ECMAErrors.isScriptFrame(st)) {
156 final String className = "<" + st.getFileName() + ">";
157 String methodName = st.getMethodName();
158 if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
159 methodName = "<program>";
160 }
161
162 if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
163 methodName = "<anonymous>";
164 }
165
191 final int len = buf.length();
192 // remove trailing '\n'
193 if (len > 0) {
194 assert buf.charAt(len - 1) == '\n';
195 buf.deleteCharAt(len - 1);
196 }
197 return buf.toString();
198 }
199
200 protected Object getThrown() {
201 return null;
202 }
203
204 protected NashornException initEcmaError(final ScriptObject global) {
205 if (ecmaError != null) {
206 return this; // initialized already!
207 }
208
209 final Object thrown = getThrown();
210 if (thrown instanceof ScriptObject) {
211 ecmaError = ScriptObjectMirror.wrap(thrown, global);
212 } else {
213 ecmaError = thrown;
214 }
215
216 return this;
217 }
218
219 /**
220 * Return the underlying ECMA error object, if available.
221 *
222 * @return underlying ECMA Error object's mirror or whatever was thrown
223 * from script such as a String, Number or a Boolean.
224 */
225 public Object getEcmaError() {
226 return ecmaError;
227 }
228 }
|
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 String fileName;
49 // script line number
50 private int line;
51 // script column number
52 private 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
108 this.fileName = ste.getFileName();
109 this.line = ste.getLineNumber();
110 return;
111 }
112 }
113
114 this.fileName = null;
115 this.line = 0;
116 }
117
118 /**
119 * Get the source file name for this {@code NashornException}
120 *
121 * @return the file name
122 */
123 public final String getFileName() {
124 return fileName;
125 }
126
127 /**
128 * Set the source file name for this {@code NashornException}
129 *
130 * @param fileName the file name
131 */
132 public final void setFileName(final String fileName) {
133 this.fileName = fileName;
134 }
135
136 /**
137 * Get the line number for this {@code NashornException}
138 *
139 * @return the line number
140 */
141 public final int getLineNumber() {
142 return line;
143 }
144
145 /**
146 * Set the line number for this {@code NashornException}
147 *
148 * @param line the line number
149 */
150 public final void setLineNumber(final int line) {
151 this.line = line;
152 }
153
154 /**
155 * Get the column for this {@code NashornException}
156 *
157 * @return the column number
158 */
159 public final int getColumnNumber() {
160 return column;
161 }
162
163 /**
164 * Set the column for this {@code NashornException}
165 *
166 * @param column the column number
167 */
168 public final void setColumnNumber(final int column) {
169 this.column = column;
170 }
171
172 /**
173 * Returns array javascript stack frames from the given exception object.
174 *
175 * @param exception exception from which stack frames are retrieved and filtered
176 * @return array of javascript stack frames
177 */
178 public static StackTraceElement[] getScriptFrames(final Throwable exception) {
179 final StackTraceElement[] frames = exception.getStackTrace();
180 final List<StackTraceElement> filtered = new ArrayList<>();
181 for (final StackTraceElement st : frames) {
182 if (ECMAErrors.isScriptFrame(st)) {
183 final String className = "<" + st.getFileName() + ">";
184 String methodName = st.getMethodName();
185 if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
186 methodName = "<program>";
187 }
188
189 if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
190 methodName = "<anonymous>";
191 }
192
218 final int len = buf.length();
219 // remove trailing '\n'
220 if (len > 0) {
221 assert buf.charAt(len - 1) == '\n';
222 buf.deleteCharAt(len - 1);
223 }
224 return buf.toString();
225 }
226
227 protected Object getThrown() {
228 return null;
229 }
230
231 protected NashornException initEcmaError(final ScriptObject global) {
232 if (ecmaError != null) {
233 return this; // initialized already!
234 }
235
236 final Object thrown = getThrown();
237 if (thrown instanceof ScriptObject) {
238 setEcmaError(ScriptObjectMirror.wrap(thrown, global));
239 } else {
240 setEcmaError(thrown);
241 }
242
243 return this;
244 }
245
246 /**
247 * Return the underlying ECMA error object, if available.
248 *
249 * @return underlying ECMA Error object's mirror or whatever was thrown
250 * from script such as a String, Number or a Boolean.
251 */
252 public Object getEcmaError() {
253 return ecmaError;
254 }
255
256 /**
257 * Return the underlying ECMA error object, if available.
258 *
259 * @param ecmaError underlying ECMA Error object's mirror or whatever was thrown
260 * from script such as a String, Number or a Boolean.
261 */
262 public void setEcmaError(final Object ecmaError) {
263 this.ecmaError = ecmaError;
264 }
265 }
|