aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/runtime/ECMAErrors.java
blob: affba497b183705a079442a7f482ace1f6372024 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.nashorn.internal.runtime;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import jdk.nashorn.api.scripting.NashornException;
import jdk.nashorn.internal.scripts.JS;
import jdk.nashorn.internal.codegen.CompilerConstants;
import jdk.nashorn.internal.objects.Global;

/**
 * Helper class to throw various standard "ECMA error" exceptions such as Error, ReferenceError, TypeError etc.
 */
public final class ECMAErrors {
    private static final String MESSAGES_RESOURCE = "jdk.nashorn.internal.runtime.resources.Messages";

    private static final ResourceBundle MESSAGES_BUNDLE;
    static {
        MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
    }

    /** We assume that compiler generates script classes into the known package. */
    private static final String scriptPackage;
    static {
        String name = JS.class.getName();
        scriptPackage = name.substring(0, name.lastIndexOf('.'));
    }

    private ECMAErrors() {
    }

    private static ECMAException error(final Object thrown, final Throwable cause) {
        return new ECMAException(thrown, cause);
    }

     /**
     * Error dispatch mechanism.
     * Create a {@link ParserException} as the correct JavaScript error
     *
     * @param e {@code ParserException} for error dispatcher
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException asEcmaException(final ParserException e) {
        return asEcmaException(Context.getGlobal(), e);
    }

    /**
     * Error dispatch mechanism.
     * Create a {@link ParserException} as the correct JavaScript error
     *
     * @param global global scope object
     * @param e {@code ParserException} for error dispatcher
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException asEcmaException(final Global global, final ParserException e) {
        final JSErrorType errorType = e.getErrorType();
        assert errorType != null : "error type for " + e + " was null";

        final Global globalObj    = global;
        final String       msg    = e.getMessage();

        // translate to ECMAScript Error object using error type
        switch (errorType) {
        case ERROR:
            return error(globalObj.newError(msg), e);
        case EVAL_ERROR:
            return error(globalObj.newEvalError(msg), e);
        case RANGE_ERROR:
            return error(globalObj.newRangeError(msg), e);
        case REFERENCE_ERROR:
            return error(globalObj.newReferenceError(msg), e);
        case SYNTAX_ERROR:
            return error(globalObj.newSyntaxError(msg), e);
        case TYPE_ERROR:
            return error(globalObj.newTypeError(msg), e);
        case URI_ERROR:
            return error(globalObj.newURIError(msg), e);
        default:
            // should not happen - perhaps unknown error type?
            throw new AssertionError(e.getMessage());
        }
    }

    /**
     * Create a syntax error (ECMA 15.11.6.4)
     *
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException syntaxError(final String msgId, final String... args) {
        return syntaxError(Context.getGlobal(), msgId, args);
    }

    /**
     * Create a syntax error (ECMA 15.11.6.4)
     *
     * @param global  global scope object
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException syntaxError(final Global global, final String msgId, final String... args) {
        return syntaxError(global, null, msgId, args);
    }

    /**
     * Create a syntax error (ECMA 15.11.6.4)
     *
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException syntaxError(final Throwable cause, final String msgId, final String... args) {
        return syntaxError(Context.getGlobal(), cause, msgId, args);
    }

    /**
     * Create a syntax error (ECMA 15.11.6.4)
     *
     * @param global  global scope object
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException syntaxError(final Global global, final Throwable cause, final String msgId, final String... args) {
        final String msg = getMessage("syntax.error." + msgId, args);
        return error(global.newSyntaxError(msg), cause);
    }

    /**
     * Create a type error (ECMA 15.11.6.5)
     *
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException typeError(final String msgId, final String... args) {
        return typeError(Context.getGlobal(), msgId, args);
    }

    /**
     * Create a type error (ECMA 15.11.6.5)
     *
     * @param global  global scope object
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException typeError(final Global global, final String msgId, final String... args) {
        return typeError(global, null, msgId, args);
    }

    /**
     * Create a type error (ECMA 15.11.6.5)
     *
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException typeError(final Throwable cause, final String msgId, final String... args) {
        return typeError(Context.getGlobal(), cause, msgId, args);
    }

    /**
     * Create a type error (ECMA 15.11.6.5)
     *
     * @param global  global scope object
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException typeError(final Global global, final Throwable cause, final String msgId, final String... args) {
        final String msg = getMessage("type.error." + msgId, args);
        return error(global.newTypeError(msg), cause);
    }

    /**
     * Create a range error (ECMA 15.11.6.2)
     *
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException rangeError(final String msgId, final String... args) {
        return rangeError(Context.getGlobal(), msgId, args);
    }

    /**
     * Create a range error (ECMA 15.11.6.2)
     *
     * @param global  global scope object
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException rangeError(final Global global, final String msgId, final String... args) {
        return rangeError(global, null, msgId, args);
    }

    /**
     * Create a range error (ECMA 15.11.6.2)
     *
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException rangeError(final Throwable cause, final String msgId, final String... args) {
        return rangeError(Context.getGlobal(), cause, msgId, args);
    }

    /**
     * Create a range error (ECMA 15.11.6.2)
     *
     * @param global  global scope object
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException rangeError(final Global global, final Throwable cause, final String msgId, final String... args) {
        final String msg = getMessage("range.error." + msgId, args);
        return error(global.newRangeError(msg), cause);
    }

    /**
     * Create a reference error (ECMA 15.11.6.3)
     *
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException referenceError(final String msgId, final String... args) {
        return referenceError(Context.getGlobal(), msgId, args);
    }

    /**
     * Create a reference error (ECMA 15.11.6.3)
     *
     * @param global  global scope object
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException referenceError(final Global global, final String msgId, final String... args) {
        return referenceError(global, null, msgId, args);
    }

    /**
     * Create a reference error (ECMA 15.11.6.3)
     *
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException referenceError(final Throwable cause, final String msgId, final String... args) {
        return referenceError(Context.getGlobal(), cause, msgId, args);
    }

    /**
     * Create a reference error (ECMA 15.11.6.3)
     *
     * @param global  global scope object
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException referenceError(final Global global, final Throwable cause, final String msgId, final String... args) {
        final String msg = getMessage("reference.error." + msgId, args);
        return error(global.newReferenceError(msg), cause);
    }

    /**
     * Create a URI error (ECMA 15.11.6.6)
     *
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException uriError(final String msgId, final String... args) {
        return uriError(Context.getGlobal(), msgId, args);
    }

    /**
     * Create a URI error (ECMA 15.11.6.6)
     *
     * @param global  global scope object
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException uriError(final Global global, final String msgId, final String... args) {
        return uriError(global, null, msgId, args);
    }

    /**
     * Create a URI error (ECMA 15.11.6.6)
     *
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException uriError(final Throwable cause, final String msgId, final String... args) {
        return uriError(Context.getGlobal(), cause, msgId, args);
    }

    /**
     * Create a URI error (ECMA 15.11.6.6)
     *
     * @param global  global scope object
     * @param cause   native Java {@code Throwable} that is the cause of error
     * @param msgId   resource tag for error message
     * @param args    arguments to resource
     *
     * @return the resulting {@link ECMAException}
     */
    public static ECMAException uriError(final Global global, final Throwable cause, final String msgId, final String... args) {
        final String msg = getMessage("uri.error." + msgId, args);
        return error(global.newURIError(msg), cause);
    }

    /**
     * Get the exception message by placing the args in the resource defined
     * by the resource tag. This is visible to, e.g. the {@link jdk.nashorn.internal.parser.Parser}
     * can use it to generate compile time messages with the correct locale
     *
     * @param msgId the resource tag (message id)
     * @param args  arguments to error string
     *
     * @return the filled out error string
     */
    public static String getMessage(final String msgId, final String... args) {
        try {
            return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
        } catch (final java.util.MissingResourceException e) {
            throw new RuntimeException("no message resource found for message id: "+ msgId);
        }
    }


    /**
     * Check if a stack trace element is in JavaScript
     *
     * @param frame frame
     *
     * @return true if frame is in the script
     */
    public static boolean isScriptFrame(final StackTraceElement frame) {
        final String className = frame.getClassName();

        // Look for script package in class name (into which compiler puts generated code)
        if (className.startsWith(scriptPackage) && !frame.getMethodName().startsWith(CompilerConstants.INTERNAL_METHOD_PREFIX)) {
            final String source = frame.getFileName();
            /*
             * Make sure that it is not some Java code that Nashorn has in that package!
             * also, we don't want to report JavaScript code that lives in script engine implementation
             * We want to report only user's own scripts and not any of our own scripts like "engine.js"
             */
            return source != null && !source.endsWith(".java") && !source.contains(NashornException.ENGINE_SCRIPT_SOURCE_NAME);
        }
        return false;
    }
}