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.internal.runtime;
27
28 import java.text.MessageFormat;
29 import java.util.Locale;
30 import java.util.ResourceBundle;
31 import jdk.nashorn.api.scripting.NashornException;
32 import jdk.nashorn.internal.scripts.JS;
33
34 /**
35 * Helper class to throw various standard "ECMA error" exceptions such as Error, ReferenceError, TypeError etc.
36 */
37 public final class ECMAErrors {
38 private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages";
39
40 private static final ResourceBundle MESSAGES_BUNDLE;
41 static {
42 MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
43 }
44
45 /** We assume that compiler generates script classes into the known package. */
46 private static final String scriptPackage;
47 static {
48 String name = JS.class.getName();
49 scriptPackage = name.substring(0, name.lastIndexOf('.'));
50 }
51
52 private ECMAErrors() {
53 }
54
55 private static ECMAException error(final Object thrown, final Throwable cause) {
56 return new ECMAException(thrown, cause);
57 }
58
59 /**
60 * Error dispatch mechanism.
61 * Create a {@link ParserException} as the correct JavaScript error
62 *
63 * @param e {@code ParserException} for error dispatcher
64 *
65 * @return the resulting {@link ECMAException}
66 */
67 public static ECMAException asEcmaException(final ParserException e) {
68 return asEcmaException(Context.getGlobalTrusted(), e);
69 }
70
71 /**
72 * Error dispatch mechanism.
73 * Create a {@link ParserException} as the correct JavaScript error
74 *
75 * @param global global scope object
76 * @param e {@code ParserException} for error dispatcher
77 *
78 * @return the resulting {@link ECMAException}
79 */
80 public static ECMAException asEcmaException(final ScriptObject global, final ParserException e) {
81 final JSErrorType errorType = e.getErrorType();
82 assert errorType != null : "error type for " + e + " was null";
83
84 final GlobalObject globalObj = (GlobalObject)global;
85 final String msg = e.getMessage();
86
87 // translate to ECMAScript Error object using error type
88 switch (errorType) {
89 case ERROR:
90 return error(globalObj.newError(msg), e);
91 case EVAL_ERROR:
92 return error(globalObj.newEvalError(msg), e);
93 case RANGE_ERROR:
94 return error(globalObj.newRangeError(msg), e);
95 case REFERENCE_ERROR:
96 return error(globalObj.newReferenceError(msg), e);
97 case SYNTAX_ERROR:
98 return error(globalObj.newSyntaxError(msg), e);
99 case TYPE_ERROR:
100 return error(globalObj.newTypeError(msg), e);
101 case URI_ERROR:
102 return error(globalObj.newURIError(msg), e);
103 default:
104 // should not happen - perhaps unknown error type?
105 throw new AssertionError(e.getMessage());
106 }
107 }
108
109 /**
110 * Create a syntax error (ECMA 15.11.6.4)
111 *
112 * @param msgId resource tag for error message
113 * @param args arguments to resource
114 *
115 * @return the resulting {@link ECMAException}
116 */
117 public static ECMAException syntaxError(final String msgId, final String... args) {
118 return syntaxError(Context.getGlobalTrusted(), msgId, args);
119 }
120
121 /**
122 * Create a syntax error (ECMA 15.11.6.4)
123 *
124 * @param global global scope object
125 * @param msgId resource tag for error message
126 * @param args arguments to resource
127 *
128 * @return the resulting {@link ECMAException}
129 */
130 public static ECMAException syntaxError(final ScriptObject global, final String msgId, final String... args) {
131 return syntaxError(global, null, msgId, args);
132 }
133
134 /**
135 * Create a syntax error (ECMA 15.11.6.4)
136 *
137 * @param cause native Java {@code Throwable} that is the cause of error
138 * @param msgId resource tag for error message
139 * @param args arguments to resource
140 *
141 * @return the resulting {@link ECMAException}
142 */
143 public static ECMAException syntaxError(final Throwable cause, final String msgId, final String... args) {
144 return syntaxError(Context.getGlobalTrusted(), cause, msgId, args);
145 }
146
147 /**
148 * Create a syntax error (ECMA 15.11.6.4)
149 *
150 * @param global global scope object
151 * @param cause native Java {@code Throwable} that is the cause of error
152 * @param msgId resource tag for error message
153 * @param args arguments to resource
154 *
155 * @return the resulting {@link ECMAException}
156 */
157 public static ECMAException syntaxError(final ScriptObject global, final Throwable cause, final String msgId, final String... args) {
158 final String msg = getMessage("syntax.error." + msgId, args);
159 return error(((GlobalObject)global).newSyntaxError(msg), cause);
160 }
161
162 /**
163 * Create a type error (ECMA 15.11.6.5)
164 *
165 * @param msgId resource tag for error message
166 * @param args arguments to resource
167 *
168 * @return the resulting {@link ECMAException}
169 */
170 public static ECMAException typeError(final String msgId, final String... args) {
171 return typeError(Context.getGlobalTrusted(), msgId, args);
172 }
173
174 /**
175 * Create a type error (ECMA 15.11.6.5)
176 *
177 * @param global global scope object
178 * @param msgId resource tag for error message
179 * @param args arguments to resource
180 *
181 * @return the resulting {@link ECMAException}
182 */
183 public static ECMAException typeError(final ScriptObject global, final String msgId, final String... args) {
184 return typeError(global, null, msgId, args);
185 }
186
187 /**
188 * Create a type error (ECMA 15.11.6.5)
189 *
190 * @param cause native Java {@code Throwable} that is the cause of error
191 * @param msgId resource tag for error message
192 * @param args arguments to resource
193 *
194 * @return the resulting {@link ECMAException}
195 */
196 public static ECMAException typeError(final Throwable cause, final String msgId, final String... args) {
197 return typeError(Context.getGlobalTrusted(), cause, msgId, args);
198 }
199
200 /**
201 * Create a type error (ECMA 15.11.6.5)
202 *
203 * @param global global scope object
204 * @param cause native Java {@code Throwable} that is the cause of error
205 * @param msgId resource tag for error message
206 * @param args arguments to resource
207 *
208 * @return the resulting {@link ECMAException}
209 */
210 public static ECMAException typeError(final ScriptObject global, final Throwable cause, final String msgId, final String... args) {
211 final String msg = getMessage("type.error." + msgId, args);
212 return error(((GlobalObject)global).newTypeError(msg), cause);
213 }
214
215 /**
216 * Create a range error (ECMA 15.11.6.2)
217 *
218 * @param msgId resource tag for error message
219 * @param args arguments to resource
220 *
221 * @return the resulting {@link ECMAException}
222 */
223 public static ECMAException rangeError(final String msgId, final String... args) {
224 return rangeError(Context.getGlobalTrusted(), msgId, args);
225 }
226
227 /**
228 * Create a range error (ECMA 15.11.6.2)
229 *
230 * @param global global scope object
231 * @param msgId resource tag for error message
232 * @param args arguments to resource
233 *
234 * @return the resulting {@link ECMAException}
235 */
236 public static ECMAException rangeError(final ScriptObject global, final String msgId, final String... args) {
237 return rangeError(global, null, msgId, args);
238 }
239
240 /**
241 * Create a range error (ECMA 15.11.6.2)
242 *
243 * @param cause native Java {@code Throwable} that is the cause of error
244 * @param msgId resource tag for error message
245 * @param args arguments to resource
246 *
247 * @return the resulting {@link ECMAException}
248 */
249 public static ECMAException rangeError(final Throwable cause, final String msgId, final String... args) {
250 return rangeError(Context.getGlobalTrusted(), cause, msgId, args);
251 }
252
253 /**
254 * Create a range error (ECMA 15.11.6.2)
255 *
256 * @param global global scope object
257 * @param cause native Java {@code Throwable} that is the cause of error
258 * @param msgId resource tag for error message
259 * @param args arguments to resource
260 *
261 * @return the resulting {@link ECMAException}
262 */
263 public static ECMAException rangeError(final ScriptObject global, final Throwable cause, final String msgId, final String... args) {
264 final String msg = getMessage("range.error." + msgId, args);
265 return error(((GlobalObject)global).newRangeError(msg), cause);
266 }
267
268 /**
269 * Create a reference error (ECMA 15.11.6.3)
270 *
271 * @param msgId resource tag for error message
272 * @param args arguments to resource
273 *
274 * @return the resulting {@link ECMAException}
275 */
276 public static ECMAException referenceError(final String msgId, final String... args) {
277 return referenceError(Context.getGlobalTrusted(), msgId, args);
278 }
279
280 /**
281 * Create a reference error (ECMA 15.11.6.3)
282 *
283 * @param global global scope object
284 * @param msgId resource tag for error message
285 * @param args arguments to resource
286 *
287 * @return the resulting {@link ECMAException}
288 */
289 public static ECMAException referenceError(final ScriptObject global, final String msgId, final String... args) {
290 return referenceError(global, null, msgId, args);
291 }
292
293 /**
294 * Create a reference error (ECMA 15.11.6.3)
295 *
296 * @param cause native Java {@code Throwable} that is the cause of error
297 * @param msgId resource tag for error message
298 * @param args arguments to resource
299 *
300 * @return the resulting {@link ECMAException}
301 */
302 public static ECMAException referenceError(final Throwable cause, final String msgId, final String... args) {
303 return referenceError(Context.getGlobalTrusted(), cause, msgId, args);
304 }
305
306 /**
307 * Create a reference error (ECMA 15.11.6.3)
308 *
309 * @param global global scope object
310 * @param cause native Java {@code Throwable} that is the cause of error
311 * @param msgId resource tag for error message
312 * @param args arguments to resource
313 *
314 * @return the resulting {@link ECMAException}
315 */
316 public static ECMAException referenceError(final ScriptObject global, final Throwable cause, final String msgId, final String... args) {
317 final String msg = getMessage("reference.error." + msgId, args);
318 return error(((GlobalObject)global).newReferenceError(msg), cause);
319 }
320
321 /**
322 * Create a URI error (ECMA 15.11.6.6)
323 *
324 * @param msgId resource tag for error message
325 * @param args arguments to resource
326 *
327 * @return the resulting {@link ECMAException}
328 */
329 public static ECMAException uriError(final String msgId, final String... args) {
330 return uriError(Context.getGlobalTrusted(), msgId, args);
331 }
332
333 /**
334 * Create a URI error (ECMA 15.11.6.6)
335 *
336 * @param global global scope object
337 * @param msgId resource tag for error message
338 * @param args arguments to resource
339 *
340 * @return the resulting {@link ECMAException}
341 */
342 public static ECMAException uriError(final ScriptObject global, final String msgId, final String... args) {
343 return uriError(global, null, msgId, args);
344 }
345
346 /**
347 * Create a URI error (ECMA 15.11.6.6)
348 *
349 * @param cause native Java {@code Throwable} that is the cause of error
350 * @param msgId resource tag for error message
351 * @param args arguments to resource
352 *
353 * @return the resulting {@link ECMAException}
354 */
355 public static ECMAException uriError(final Throwable cause, final String msgId, final String... args) {
356 return uriError(Context.getGlobalTrusted(), cause, msgId, args);
357 }
358
359 /**
360 * Create a URI error (ECMA 15.11.6.6)
361 *
362 * @param global global scope object
363 * @param cause native Java {@code Throwable} that is the cause of error
364 * @param msgId resource tag for error message
365 * @param args arguments to resource
366 *
367 * @return the resulting {@link ECMAException}
368 */
369 public static ECMAException uriError(final ScriptObject global, final Throwable cause, final String msgId, final String... args) {
370 final String msg = getMessage("uri.error." + msgId, args);
371 return error(((GlobalObject)global).newURIError(msg), cause);
372 }
373
374 /**
375 * Get the exception message by placing the args in the resource defined
376 * by the resource tag. This is visible to, e.g. the {@link jdk.nashorn.internal.parser.Parser}
377 * can use it to generate compile time messages with the correct locale
378 *
379 * @param msgId the resource tag (message id)
380 * @param args arguments to error string
381 *
382 * @return the filled out error string
383 */
384 public static String getMessage(final String msgId, final String... args) {
385 try {
386 return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
387 } catch (final java.util.MissingResourceException e) {
388 throw new RuntimeException("no message resource found for message id: "+ msgId);
389 }
390 }
391
392
393 /**
394 * Check if a stack trace element is in JavaScript
395 *
396 * @param frame frame
397 *
398 * @return true if frame is in the script
399 */
400 public static boolean isScriptFrame(final StackTraceElement frame) {
401 final String className = frame.getClassName();
402
403 // Look for script package in class name (into which compiler puts generated code)
404 if (className.startsWith(scriptPackage)) {
405 final String source = frame.getFileName();
406 /*
407 * Make sure that it is not some Java code that Nashorn has in that package!
408 * also, we don't want to report JavaScript code that lives in script engine implementation
409 * We want to report only user's own scripts and not any of our own scripts like "engine.js"
410 */
411 return source != null && !source.endsWith(".java") && !source.contains(NashornException.ENGINE_SCRIPT_SOURCE_NAME);
412 }
413 return false;
414 }
415 }
|
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.internal.runtime;
27
28 import java.text.MessageFormat;
29 import java.util.Locale;
30 import java.util.ResourceBundle;
31 import jdk.nashorn.api.scripting.NashornException;
32 import jdk.nashorn.internal.scripts.JS;
33 import jdk.nashorn.internal.codegen.CompilerConstants;
34 import jdk.nashorn.internal.objects.Global;
35
36 /**
37 * Helper class to throw various standard "ECMA error" exceptions such as Error, ReferenceError, TypeError etc.
38 */
39 public final class ECMAErrors {
40 private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages";
41
42 private static final ResourceBundle MESSAGES_BUNDLE;
43 static {
44 MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
45 }
46
47 /** We assume that compiler generates script classes into the known package. */
48 private static final String scriptPackage;
49 static {
50 String name = JS.class.getName();
51 scriptPackage = name.substring(0, name.lastIndexOf('.'));
52 }
53
54 private ECMAErrors() {
55 }
56
57 private static ECMAException error(final Object thrown, final Throwable cause) {
58 return new ECMAException(thrown, cause);
59 }
60
61 /**
62 * Error dispatch mechanism.
63 * Create a {@link ParserException} as the correct JavaScript error
64 *
65 * @param e {@code ParserException} for error dispatcher
66 *
67 * @return the resulting {@link ECMAException}
68 */
69 public static ECMAException asEcmaException(final ParserException e) {
70 return asEcmaException(Context.getGlobal(), e);
71 }
72
73 /**
74 * Error dispatch mechanism.
75 * Create a {@link ParserException} as the correct JavaScript error
76 *
77 * @param global global scope object
78 * @param e {@code ParserException} for error dispatcher
79 *
80 * @return the resulting {@link ECMAException}
81 */
82 public static ECMAException asEcmaException(final Global global, final ParserException e) {
83 final JSErrorType errorType = e.getErrorType();
84 assert errorType != null : "error type for " + e + " was null";
85
86 final Global globalObj = global;
87 final String msg = e.getMessage();
88
89 // translate to ECMAScript Error object using error type
90 switch (errorType) {
91 case ERROR:
92 return error(globalObj.newError(msg), e);
93 case EVAL_ERROR:
94 return error(globalObj.newEvalError(msg), e);
95 case RANGE_ERROR:
96 return error(globalObj.newRangeError(msg), e);
97 case REFERENCE_ERROR:
98 return error(globalObj.newReferenceError(msg), e);
99 case SYNTAX_ERROR:
100 return error(globalObj.newSyntaxError(msg), e);
101 case TYPE_ERROR:
102 return error(globalObj.newTypeError(msg), e);
103 case URI_ERROR:
104 return error(globalObj.newURIError(msg), e);
105 default:
106 // should not happen - perhaps unknown error type?
107 throw new AssertionError(e.getMessage());
108 }
109 }
110
111 /**
112 * Create a syntax error (ECMA 15.11.6.4)
113 *
114 * @param msgId resource tag for error message
115 * @param args arguments to resource
116 *
117 * @return the resulting {@link ECMAException}
118 */
119 public static ECMAException syntaxError(final String msgId, final String... args) {
120 return syntaxError(Context.getGlobal(), msgId, args);
121 }
122
123 /**
124 * Create a syntax error (ECMA 15.11.6.4)
125 *
126 * @param global global scope object
127 * @param msgId resource tag for error message
128 * @param args arguments to resource
129 *
130 * @return the resulting {@link ECMAException}
131 */
132 public static ECMAException syntaxError(final Global global, final String msgId, final String... args) {
133 return syntaxError(global, null, msgId, args);
134 }
135
136 /**
137 * Create a syntax error (ECMA 15.11.6.4)
138 *
139 * @param cause native Java {@code Throwable} that is the cause of error
140 * @param msgId resource tag for error message
141 * @param args arguments to resource
142 *
143 * @return the resulting {@link ECMAException}
144 */
145 public static ECMAException syntaxError(final Throwable cause, final String msgId, final String... args) {
146 return syntaxError(Context.getGlobal(), cause, msgId, args);
147 }
148
149 /**
150 * Create a syntax error (ECMA 15.11.6.4)
151 *
152 * @param global global scope object
153 * @param cause native Java {@code Throwable} that is the cause of error
154 * @param msgId resource tag for error message
155 * @param args arguments to resource
156 *
157 * @return the resulting {@link ECMAException}
158 */
159 public static ECMAException syntaxError(final Global global, final Throwable cause, final String msgId, final String... args) {
160 final String msg = getMessage("syntax.error." + msgId, args);
161 return error(global.newSyntaxError(msg), cause);
162 }
163
164 /**
165 * Create a type error (ECMA 15.11.6.5)
166 *
167 * @param msgId resource tag for error message
168 * @param args arguments to resource
169 *
170 * @return the resulting {@link ECMAException}
171 */
172 public static ECMAException typeError(final String msgId, final String... args) {
173 return typeError(Context.getGlobal(), msgId, args);
174 }
175
176 /**
177 * Create a type error (ECMA 15.11.6.5)
178 *
179 * @param global global scope object
180 * @param msgId resource tag for error message
181 * @param args arguments to resource
182 *
183 * @return the resulting {@link ECMAException}
184 */
185 public static ECMAException typeError(final Global global, final String msgId, final String... args) {
186 return typeError(global, null, msgId, args);
187 }
188
189 /**
190 * Create a type error (ECMA 15.11.6.5)
191 *
192 * @param cause native Java {@code Throwable} that is the cause of error
193 * @param msgId resource tag for error message
194 * @param args arguments to resource
195 *
196 * @return the resulting {@link ECMAException}
197 */
198 public static ECMAException typeError(final Throwable cause, final String msgId, final String... args) {
199 return typeError(Context.getGlobal(), cause, msgId, args);
200 }
201
202 /**
203 * Create a type error (ECMA 15.11.6.5)
204 *
205 * @param global global scope object
206 * @param cause native Java {@code Throwable} that is the cause of error
207 * @param msgId resource tag for error message
208 * @param args arguments to resource
209 *
210 * @return the resulting {@link ECMAException}
211 */
212 public static ECMAException typeError(final Global global, final Throwable cause, final String msgId, final String... args) {
213 final String msg = getMessage("type.error." + msgId, args);
214 return error(global.newTypeError(msg), cause);
215 }
216
217 /**
218 * Create a range error (ECMA 15.11.6.2)
219 *
220 * @param msgId resource tag for error message
221 * @param args arguments to resource
222 *
223 * @return the resulting {@link ECMAException}
224 */
225 public static ECMAException rangeError(final String msgId, final String... args) {
226 return rangeError(Context.getGlobal(), msgId, args);
227 }
228
229 /**
230 * Create a range error (ECMA 15.11.6.2)
231 *
232 * @param global global scope object
233 * @param msgId resource tag for error message
234 * @param args arguments to resource
235 *
236 * @return the resulting {@link ECMAException}
237 */
238 public static ECMAException rangeError(final Global global, final String msgId, final String... args) {
239 return rangeError(global, null, msgId, args);
240 }
241
242 /**
243 * Create a range error (ECMA 15.11.6.2)
244 *
245 * @param cause native Java {@code Throwable} that is the cause of error
246 * @param msgId resource tag for error message
247 * @param args arguments to resource
248 *
249 * @return the resulting {@link ECMAException}
250 */
251 public static ECMAException rangeError(final Throwable cause, final String msgId, final String... args) {
252 return rangeError(Context.getGlobal(), cause, msgId, args);
253 }
254
255 /**
256 * Create a range error (ECMA 15.11.6.2)
257 *
258 * @param global global scope object
259 * @param cause native Java {@code Throwable} that is the cause of error
260 * @param msgId resource tag for error message
261 * @param args arguments to resource
262 *
263 * @return the resulting {@link ECMAException}
264 */
265 public static ECMAException rangeError(final Global global, final Throwable cause, final String msgId, final String... args) {
266 final String msg = getMessage("range.error." + msgId, args);
267 return error(global.newRangeError(msg), cause);
268 }
269
270 /**
271 * Create a reference error (ECMA 15.11.6.3)
272 *
273 * @param msgId resource tag for error message
274 * @param args arguments to resource
275 *
276 * @return the resulting {@link ECMAException}
277 */
278 public static ECMAException referenceError(final String msgId, final String... args) {
279 return referenceError(Context.getGlobal(), msgId, args);
280 }
281
282 /**
283 * Create a reference error (ECMA 15.11.6.3)
284 *
285 * @param global global scope object
286 * @param msgId resource tag for error message
287 * @param args arguments to resource
288 *
289 * @return the resulting {@link ECMAException}
290 */
291 public static ECMAException referenceError(final Global global, final String msgId, final String... args) {
292 return referenceError(global, null, msgId, args);
293 }
294
295 /**
296 * Create a reference error (ECMA 15.11.6.3)
297 *
298 * @param cause native Java {@code Throwable} that is the cause of error
299 * @param msgId resource tag for error message
300 * @param args arguments to resource
301 *
302 * @return the resulting {@link ECMAException}
303 */
304 public static ECMAException referenceError(final Throwable cause, final String msgId, final String... args) {
305 return referenceError(Context.getGlobal(), cause, msgId, args);
306 }
307
308 /**
309 * Create a reference error (ECMA 15.11.6.3)
310 *
311 * @param global global scope object
312 * @param cause native Java {@code Throwable} that is the cause of error
313 * @param msgId resource tag for error message
314 * @param args arguments to resource
315 *
316 * @return the resulting {@link ECMAException}
317 */
318 public static ECMAException referenceError(final Global global, final Throwable cause, final String msgId, final String... args) {
319 final String msg = getMessage("reference.error." + msgId, args);
320 return error(global.newReferenceError(msg), cause);
321 }
322
323 /**
324 * Create a URI error (ECMA 15.11.6.6)
325 *
326 * @param msgId resource tag for error message
327 * @param args arguments to resource
328 *
329 * @return the resulting {@link ECMAException}
330 */
331 public static ECMAException uriError(final String msgId, final String... args) {
332 return uriError(Context.getGlobal(), msgId, args);
333 }
334
335 /**
336 * Create a URI error (ECMA 15.11.6.6)
337 *
338 * @param global global scope object
339 * @param msgId resource tag for error message
340 * @param args arguments to resource
341 *
342 * @return the resulting {@link ECMAException}
343 */
344 public static ECMAException uriError(final Global global, final String msgId, final String... args) {
345 return uriError(global, null, msgId, args);
346 }
347
348 /**
349 * Create a URI error (ECMA 15.11.6.6)
350 *
351 * @param cause native Java {@code Throwable} that is the cause of error
352 * @param msgId resource tag for error message
353 * @param args arguments to resource
354 *
355 * @return the resulting {@link ECMAException}
356 */
357 public static ECMAException uriError(final Throwable cause, final String msgId, final String... args) {
358 return uriError(Context.getGlobal(), cause, msgId, args);
359 }
360
361 /**
362 * Create a URI error (ECMA 15.11.6.6)
363 *
364 * @param global global scope object
365 * @param cause native Java {@code Throwable} that is the cause of error
366 * @param msgId resource tag for error message
367 * @param args arguments to resource
368 *
369 * @return the resulting {@link ECMAException}
370 */
371 public static ECMAException uriError(final Global global, final Throwable cause, final String msgId, final String... args) {
372 final String msg = getMessage("uri.error." + msgId, args);
373 return error(global.newURIError(msg), cause);
374 }
375
376 /**
377 * Get the exception message by placing the args in the resource defined
378 * by the resource tag. This is visible to, e.g. the {@link jdk.nashorn.internal.parser.Parser}
379 * can use it to generate compile time messages with the correct locale
380 *
381 * @param msgId the resource tag (message id)
382 * @param args arguments to error string
383 *
384 * @return the filled out error string
385 */
386 public static String getMessage(final String msgId, final String... args) {
387 try {
388 return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
389 } catch (final java.util.MissingResourceException e) {
390 throw new RuntimeException("no message resource found for message id: "+ msgId);
391 }
392 }
393
394
395 /**
396 * Check if a stack trace element is in JavaScript
397 *
398 * @param frame frame
399 *
400 * @return true if frame is in the script
401 */
402 public static boolean isScriptFrame(final StackTraceElement frame) {
403 final String className = frame.getClassName();
404
405 // Look for script package in class name (into which compiler puts generated code)
406 if (className.startsWith(scriptPackage) && !frame.getMethodName().startsWith(CompilerConstants.INTERNAL_METHOD_PREFIX)) {
407 final String source = frame.getFileName();
408 /*
409 * Make sure that it is not some Java code that Nashorn has in that package!
410 * also, we don't want to report JavaScript code that lives in script engine implementation
411 * We want to report only user's own scripts and not any of our own scripts like "engine.js"
412 */
413 return source != null && !source.endsWith(".java") && !source.contains(NashornException.ENGINE_SCRIPT_SOURCE_NAME);
414 }
415 return false;
416 }
417 }
|