View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Test for error handling.
24   *
25   * @author Ronald Brill
26   */
27  public class ErrorTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts(DEFAULT = {"Error", "Whoops!", "undefined", "undefined", "undefined",
34                         "undefined", "true", "Error/Error"},
35              FF = {"Error", "Whoops!", "undefined", "11", "undefined", "26", "true", "Error/Error"},
36              FF_ESR = {"Error", "Whoops!", "undefined", "11", "undefined", "26", "true", "Error/Error"})
37      @HtmlUnitNYI(CHROME = {"Error", "Whoops!", "undefined", "undefined", "undefined",
38                             "26", "true", "Error/Error"},
39              EDGE = {"Error", "Whoops!", "undefined", "undefined", "undefined",
40                      "26", "true", "Error/Error"},
41              FF = {"Error", "Whoops!", "undefined", "undefined", "undefined",
42                    "26", "true", "Error/Error"},
43              FF_ESR = {"Error", "Whoops!", "undefined", "undefined", "undefined",
44                        "26", "true", "Error/Error"})
45      public void error() throws Exception {
46          final String html = DOCTYPE_HTML
47              + "<html>\n"
48              + "<head>\n"
49              + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
50              + "</head>\n"
51              + "<body>\n"
52              + "<script>"
53              + "  try {\n"
54              + "    throw new Error('Whoops!');\n"
55              + "  } catch(e) {"
56              + "    log(e.name);\n"
57              + "    log(e.message);\n"
58              + "    log(e.cause);\n"
59              + "    log(e.columnNumber);\n"
60              + "    log(e.filename);\n"
61              + "    log(e.lineNumber);\n"
62              + "    log(e instanceof Error);\n"
63              + "    logEx(e);\n"
64              + "  }\n"
65              + "</script>\n"
66              + "</body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      @Alerts(DEFAULT = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
76                         "undefined", "true", "true", "EvalError"},
77              FF = {"EvalError", "Whoops!", "undefined", "11", "undefined", "26",
78                    "true", "true", "EvalError"},
79              FF_ESR = {"EvalError", "Whoops!", "undefined", "11", "undefined", "26",
80                        "true", "true", "EvalError"})
81      @HtmlUnitNYI(CHROME = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
82                             "26", "true", "true", "EvalError"},
83              EDGE = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
84                      "26", "true", "true", "EvalError"},
85              FF = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
86                    "26", "true", "true", "EvalError"},
87              FF_ESR = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
88                        "26", "true", "true", "EvalError"})
89      public void evalError() throws Exception {
90          final String html = DOCTYPE_HTML
91              + "<html>\n"
92              + "<head>\n"
93              + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
94              + "</head>\n"
95              + "<body>\n"
96              + "<script>"
97              + "  try {\n"
98              + "    throw new EvalError('Whoops!');\n"
99              + "  } catch(e) {"
100             + "    log(e.name);\n"
101             + "    log(e.message);\n"
102             + "    log(e.cause);\n"
103             + "    log(e.columnNumber);\n"
104             + "    log(e.filename);\n"
105             + "    log(e.lineNumber);\n"
106             + "    log(e instanceof Error);\n"
107             + "    log(e instanceof EvalError);\n"
108             + "    logEx(e);\n"
109             + "  }\n"
110             + "</script>\n"
111             + "</body></html>";
112 
113         loadPageVerifyTitle2(html);
114     }
115 
116     /**
117      * @throws Exception if the test fails
118      */
119     @Test
120     @Alerts(DEFAULT = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
121                        "undefined", "true", "true", "RangeError"},
122             FF = {"RangeError", "Whoops!", "undefined", "11", "undefined", "26",
123                   "true", "true", "RangeError"},
124             FF_ESR = {"RangeError", "Whoops!", "undefined", "11", "undefined", "26",
125                       "true", "true", "RangeError"})
126     @HtmlUnitNYI(CHROME = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
127                            "26", "true", "true", "RangeError"},
128             EDGE = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
129                     "26", "true", "true", "RangeError"},
130             FF = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
131                   "26", "true", "true", "RangeError"},
132             FF_ESR = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
133                       "26", "true", "true", "RangeError"})
134     public void rangeError() throws Exception {
135         final String html = DOCTYPE_HTML
136             + "<html>\n"
137             + "<head>\n"
138             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
139             + "</head>\n"
140             + "<body>\n"
141             + "<script>"
142             + "  try {\n"
143             + "    throw new RangeError('Whoops!');\n"
144             + "  } catch(e) {"
145             + "    log(e.name);\n"
146             + "    log(e.message);\n"
147             + "    log(e.cause);\n"
148             + "    log(e.columnNumber);\n"
149             + "    log(e.filename);\n"
150             + "    log(e.lineNumber);\n"
151             + "    log(e instanceof Error);\n"
152             + "    log(e instanceof RangeError);\n"
153             + "    logEx(e);\n"
154             + "  }\n"
155             + "</script>\n"
156             + "</body></html>";
157 
158         loadPageVerifyTitle2(html);
159     }
160 
161     /**
162      * @throws Exception if the test fails
163      */
164     @Test
165     @Alerts(DEFAULT = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
166                        "undefined", "true", "true", "ReferenceError"},
167             FF = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "26",
168                   "true", "true", "ReferenceError"},
169             FF_ESR = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "26",
170                       "true", "true", "ReferenceError"})
171     @HtmlUnitNYI(CHROME = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
172                            "26", "true", "true", "ReferenceError"},
173             EDGE = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
174                     "26", "true", "true", "ReferenceError"},
175             FF = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
176                   "26", "true", "true", "ReferenceError"},
177             FF_ESR = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
178                       "26", "true", "true", "ReferenceError"})
179     public void referenceError() throws Exception {
180         final String html = DOCTYPE_HTML
181             + "<html>\n"
182             + "<head>\n"
183             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
184             + "</head>\n"
185             + "<body>\n"
186             + "<script>"
187             + "  try {\n"
188             + "    throw new ReferenceError('Whoops!');\n"
189             + "  } catch(e) {"
190             + "    log(e.name);\n"
191             + "    log(e.message);\n"
192             + "    log(e.cause);\n"
193             + "    log(e.columnNumber);\n"
194             + "    log(e.filename);\n"
195             + "    log(e.lineNumber);\n"
196             + "    log(e instanceof Error);\n"
197             + "    log(e instanceof ReferenceError);\n"
198             + "    logEx(e);\n"
199             + "  }\n"
200             + "</script>\n"
201             + "</body></html>";
202 
203         loadPageVerifyTitle2(html);
204     }
205 
206     /**
207      * @throws Exception if the test fails
208      */
209     @Test
210     @Alerts(DEFAULT = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
211                        "undefined", "true", "true", "SyntaxError"},
212             FF = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "26",
213                   "true", "true", "SyntaxError"},
214             FF_ESR = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "26",
215                       "true", "true", "SyntaxError"})
216     @HtmlUnitNYI(CHROME = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
217                            "26", "true", "true", "SyntaxError"},
218             EDGE = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
219                     "26", "true", "true", "SyntaxError"},
220             FF = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
221                   "26", "true", "true", "SyntaxError"},
222             FF_ESR = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
223                       "26", "true", "true", "SyntaxError"})
224     public void syntaxError() throws Exception {
225         final String html = DOCTYPE_HTML
226             + "<html>\n"
227             + "<head>\n"
228             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
229             + "</head>\n"
230             + "<body>\n"
231             + "<script>"
232             + "  try {\n"
233             + "    throw new SyntaxError('Whoops!');\n"
234             + "  } catch(e) {"
235             + "    log(e.name);\n"
236             + "    log(e.message);\n"
237             + "    log(e.cause);\n"
238             + "    log(e.columnNumber);\n"
239             + "    log(e.filename);\n"
240             + "    log(e.lineNumber);\n"
241             + "    log(e instanceof Error);\n"
242             + "    log(e instanceof SyntaxError);\n"
243             + "    logEx(e);\n"
244             + "  }\n"
245             + "</script>\n"
246             + "</body></html>";
247 
248         loadPageVerifyTitle2(html);
249     }
250 
251     /**
252      * @throws Exception if the test fails
253      */
254     @Test
255     @Alerts(DEFAULT = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
256                        "undefined", "true", "true", "TypeError"},
257             FF = {"TypeError", "Whoops!", "undefined", "11", "undefined", "26",
258                   "true", "true", "TypeError"},
259             FF_ESR = {"TypeError", "Whoops!", "undefined", "11", "undefined", "26",
260                       "true", "true", "TypeError"})
261     @HtmlUnitNYI(CHROME = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
262                            "26", "true", "true", "TypeError"},
263             EDGE = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
264                     "26", "true", "true", "TypeError"},
265             FF = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
266                   "26", "true", "true", "TypeError"},
267             FF_ESR = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
268                       "26", "true", "true", "TypeError"})
269     public void typeError() throws Exception {
270         final String html = DOCTYPE_HTML
271             + "<html>\n"
272             + "<head>\n"
273             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
274             + "</head>\n"
275             + "<body>\n"
276             + "<script>"
277             + "  try {\n"
278             + "    throw new TypeError('Whoops!');\n"
279             + "  } catch(e) {"
280             + "    log(e.name);\n"
281             + "    log(e.message);\n"
282             + "    log(e.cause);\n"
283             + "    log(e.columnNumber);\n"
284             + "    log(e.filename);\n"
285             + "    log(e.lineNumber);\n"
286             + "    log(e instanceof Error);\n"
287             + "    log(e instanceof TypeError);\n"
288             + "    logEx(e);\n"
289             + "  }\n"
290             + "</script>\n"
291             + "</body></html>";
292 
293         loadPageVerifyTitle2(html);
294     }
295 
296     /**
297      * @throws Exception if the test fails
298      */
299     @Test
300     @Alerts(DEFAULT = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
301                        "undefined", "true", "true", "URIError"},
302             FF = {"URIError", "Whoops!", "undefined", "11", "undefined", "26",
303                   "true", "true", "URIError"},
304             FF_ESR = {"URIError", "Whoops!", "undefined", "11", "undefined", "26",
305                       "true", "true", "URIError"})
306     @HtmlUnitNYI(CHROME = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
307                            "26", "true", "true", "URIError"},
308             EDGE = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
309                     "26", "true", "true", "URIError"},
310             FF = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
311                   "26", "true", "true", "URIError"},
312             FF_ESR = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
313                       "26", "true", "true", "URIError"})
314     public void uriError() throws Exception {
315         final String html = DOCTYPE_HTML
316             + "<html>\n"
317             + "<head>\n"
318             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
319             + "</head>\n"
320             + "<body>\n"
321             + "<script>"
322             + "  try {\n"
323             + "    throw new URIError('Whoops!');\n"
324             + "  } catch(e) {"
325             + "    log(e.name);\n"
326             + "    log(e.message);\n"
327             + "    log(e.cause);\n"
328             + "    log(e.columnNumber);\n"
329             + "    log(e.filename);\n"
330             + "    log(e.lineNumber);\n"
331             + "    log(e instanceof Error);\n"
332             + "    log(e instanceof URIError);\n"
333             + "    logEx(e);\n"
334             + "  }\n"
335             + "</script>\n"
336             + "</body></html>";
337 
338         loadPageVerifyTitle2(html);
339     }
340 
341     /**
342      * @throws Exception if the test fails
343      */
344     @Test
345     @Alerts(DEFAULT = {"AggregateError", "Whoops!", "undefined", "Error: some error",
346                        "undefined", "undefined",
347                        "undefined", "true", "true", "AggregateError/AggregateError"},
348             FF = {"AggregateError", "Whoops!", "undefined", "Error: some error",
349                   "11", "undefined", "26",
350                   "true", "true", "AggregateError/AggregateError"},
351             FF_ESR = {"AggregateError", "Whoops!", "undefined", "Error: some error",
352                       "11", "undefined", "26",
353                       "true", "true", "AggregateError/AggregateError"})
354     @HtmlUnitNYI(CHROME = {"AggregateError", "Whoops!", "undefined", "Error: some error",
355                            "undefined", "undefined",
356                            "26", "true", "true", "AggregateError/AggregateError"},
357             EDGE = {"AggregateError", "Whoops!", "undefined", "Error: some error",
358                     "undefined", "undefined",
359                     "26", "true", "true", "AggregateError/AggregateError"},
360             FF = {"AggregateError", "Whoops!", "undefined", "Error: some error",
361                   "undefined", "undefined",
362                   "26", "true", "true", "AggregateError/AggregateError"},
363             FF_ESR = {"AggregateError", "Whoops!", "undefined", "Error: some error",
364                       "undefined", "undefined",
365                       "26", "true", "true", "AggregateError/AggregateError"})
366     public void aggregateError() throws Exception {
367         final String html = DOCTYPE_HTML
368             + "<html>\n"
369             + "<head>\n"
370             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
371             + "</head>\n"
372             + "<body>\n"
373             + "<script>"
374             + "  try {\n"
375             + "    throw new AggregateError([new Error(\"some error\")], 'Whoops!');\n"
376             + "  } catch(e) {"
377             + "    log(e.name);\n"
378             + "    log(e.message);\n"
379             + "    log(e.cause);\n"
380             + "    log(e.errors);\n"
381             + "    log(e.columnNumber);\n"
382             + "    log(e.filename);\n"
383             + "    log(e.lineNumber);\n"
384             + "    log(e instanceof Error);\n"
385             + "    log(e instanceof AggregateError);\n"
386             + "    logEx(e);\n"
387             + "  }\n"
388             + "</script>\n"
389             + "</body></html>";
390 
391         loadPageVerifyTitle2(html);
392     }
393 
394     /**
395      * @throws Exception if the test fails
396      */
397     @Test
398     @Alerts(DEFAULT = {"ReferenceError", "InternalError is not defined",
399                        "undefined", "undefined", "undefined",
400                        "undefined", "true", "false", "ReferenceError"},
401             FF = {"InternalError", "Whoops!", "undefined", "11", "undefined", "26",
402                   "true", "true", "InternalError/InternalError"},
403             FF_ESR = {"InternalError", "Whoops!", "undefined", "11", "undefined", "26",
404                       "true", "true", "InternalError/InternalError"})
405     @HtmlUnitNYI(CHROME = {"InternalError", "Whoops!", "undefined", "undefined", "undefined", "26",
406                            "true", "true", "InternalError/InternalError"},
407             EDGE = {"InternalError", "Whoops!", "undefined", "undefined", "undefined", "26",
408                     "true", "true", "InternalError/InternalError"},
409             FF = {"InternalError", "Whoops!", "undefined", "undefined", "undefined", "26",
410                   "true", "true", "InternalError/InternalError"},
411             FF_ESR = {"InternalError", "Whoops!", "undefined", "undefined", "undefined", "26",
412                       "true", "true", "InternalError/InternalError"})
413     public void internalError() throws Exception {
414         final String html = DOCTYPE_HTML
415             + "<html>\n"
416             + "<head>\n"
417             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
418             + "</head>\n"
419             + "<body>\n"
420             + "<script>"
421             + "  try {\n"
422             + "    throw new InternalError('Whoops!');\n"
423             + "  } catch(e) {"
424             + "    log(e.name);\n"
425             + "    log(e.message);\n"
426             + "    log(e.cause);\n"
427             + "    log(e.columnNumber);\n"
428             + "    log(e.filename);\n"
429             + "    log(e.lineNumber);\n"
430             + "    log(e instanceof Error);\n"
431             + "    log(typeof InternalError == 'function' && e instanceof InternalError);\n"
432             + "    logEx(e);\n"
433             + "  }\n"
434             + "</script>\n"
435             + "</body></html>";
436 
437         loadPageVerifyTitle2(html);
438     }
439 
440     /**
441      * @throws Exception if the test fails
442      */
443     @Test
444     @Alerts(DEFAULT = {"Error", "", "undefined", "undefined", "undefined", "undefined"},
445             FF = {"Error", "", "undefined", "11", "undefined", "26"},
446             FF_ESR = {"Error", "", "undefined", "11", "undefined", "26"})
447     @HtmlUnitNYI(CHROME = {"Error", "", "undefined", "undefined", "undefined", "26"},
448             EDGE = {"Error", "", "undefined", "undefined", "undefined", "26"},
449             FF = {"Error", "", "undefined", "undefined", "undefined", "26"},
450             FF_ESR = {"Error", "", "undefined", "undefined", "undefined", "26"})
451     public void ctorWithoutParams() throws Exception {
452         final String html = DOCTYPE_HTML
453             + "<html>\n"
454             + "<head>\n"
455             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
456             + "</head>\n"
457             + "<body>\n"
458             + "<script>"
459             + "  try {\n"
460             + "    throw new Error();\n"
461             + "  } catch(e) {"
462             + "    log(e.name);\n"
463             + "    log(e.message);\n"
464             + "    log(e.cause);\n"
465             + "    log(e.columnNumber);\n"
466             + "    log(e.filename);\n"
467             + "    log(e.lineNumber);\n"
468             + "  }\n"
469             + "</script>\n"
470             + "</body></html>";
471 
472         loadPageVerifyTitle2(html);
473     }
474 
475     /**
476      * @throws Exception if the test fails
477      */
478     @Test
479     @Alerts(DEFAULT = {"Error", "msg", "undefined", "undefined", "undefined", "undefined"},
480             FF = {"Error", "msg", "undefined", "11", "undefined", "26"},
481             FF_ESR = {"Error", "msg", "undefined", "11", "undefined", "26"})
482     @HtmlUnitNYI(CHROME = {"Error", "msg", "undefined", "undefined", "undefined", "26"},
483             EDGE = {"Error", "msg", "undefined", "undefined", "undefined", "26"},
484             FF = {"Error", "msg", "undefined", "undefined", "undefined", "26"},
485             FF_ESR = {"Error", "msg", "undefined", "undefined", "undefined", "26"})
486     public void ctorFilename() throws Exception {
487         final String html = DOCTYPE_HTML
488             + "<html>\n"
489             + "<head>\n"
490             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
491             + "</head>\n"
492             + "<body>\n"
493             + "<script>"
494             + "  try {\n"
495             + "    throw new Error('msg', 'file');\n"
496             + "  } catch(e) {"
497             + "    log(e.name);\n"
498             + "    log(e.message);\n"
499             + "    log(e.cause);\n"
500             + "    log(e.columnNumber);\n"
501             + "    log(e.filename);\n"
502             + "    log(e.lineNumber);\n"
503             + "  }\n"
504             + "</script>\n"
505             + "</body></html>";
506 
507         loadPageVerifyTitle2(html);
508     }
509 
510     /**
511      * @throws Exception if the test fails
512      */
513     @Test
514     @Alerts(DEFAULT = {"Error", "test", "undefined", "undefined", "undefined", "undefined"},
515             FF = {"Error", "test", "undefined", "11", "undefined", "26"},
516             FF_ESR = {"Error", "test", "undefined", "11", "undefined", "26"})
517     @HtmlUnitNYI(CHROME = {"Error", "test", "undefined", "undefined", "undefined", "26"},
518             EDGE = {"Error", "test", "undefined", "undefined", "undefined", "26"},
519             FF = {"Error", "test", "undefined", "undefined", "undefined", "26"},
520             FF_ESR = {"Error", "test", "undefined", "undefined", "undefined", "26"})
521     public void ctorAsFunction() throws Exception {
522         final String html = DOCTYPE_HTML
523             + "<html>\n"
524             + "<head>\n"
525             + "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
526             + "</head>\n"
527             + "<body>\n"
528             + "<script>"
529             + "  try {\n"
530             + "    throw Error('test');\n"
531             + "  } catch(e) {"
532             + "    log(e.name);\n"
533             + "    log(e.message);\n"
534             + "    log(e.cause);\n"
535             + "    log(e.columnNumber);\n"
536             + "    log(e.filename);\n"
537             + "    log(e.lineNumber);\n"
538             + "  }\n"
539             + "</script>\n"
540             + "</body></html>";
541 
542         loadPageVerifyTitle2(html);
543     }
544 }