Skip to content

Commit bafa0c7

Browse files
committed
more details for errors
1 parent fbca0ae commit bafa0c7

File tree

2 files changed

+381
-4
lines changed

2 files changed

+381
-4
lines changed

src/test/java/org/htmlunit/WebDriverTestCase.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,22 @@ public abstract class WebDriverTestCase extends WebTestCase {
147147

148148
private static final String LOG_EX_FUNCTION =
149149
" function logEx(e) {\n"
150-
+ " let rx = /\\[object (.*)\\]/;\n"
151-
+ " let toStr = Object.prototype.toString.call(e);\n"
152-
+ " let match = rx.exec(toStr);\n"
153-
+ " if (match != null) { toStr = match[1]; }\n"
150+
+ " let toStr = null;\n"
151+
+ " if (toStr === null && e instanceof EvalError) { toStr = 'EvalError'; }\n"
152+
+ " if (toStr === null && e instanceof RangeError) { toStr = 'RangeError'; }\n"
153+
+ " if (toStr === null && e instanceof ReferenceError) { toStr = 'ReferenceError'; }\n"
154+
+ " if (toStr === null && e instanceof SyntaxError) { toStr = 'SyntaxError'; }\n"
155+
+ " if (toStr === null && e instanceof TypeError) { toStr = 'TypeError'; }\n"
156+
+ " if (toStr === null && e instanceof URIError) { toStr = 'URIError'; }\n"
157+
+ " if (toStr === null && e instanceof AggregateError) { toStr = 'AggregateError'; }\n"
158+
+ " if (toStr === null && typeof InternalError == 'function' "
159+
+ "&& e instanceof InternalError) { toStr = 'InternalError'; }\n"
160+
+ " if (toStr === null) {\n"
161+
+ " let rx = /\\[object (.*)\\]/;\n"
162+
+ " toStr = Object.prototype.toString.call(e);\n"
163+
+ " let match = rx.exec(toStr);\n"
164+
+ " if (match != null) { toStr = match[1]; }\n"
165+
+ " }"
154166
+ " log(e.name + '/' + toStr);\n"
155167
+ "}\n";
156168

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
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.BrowserRunner;
19+
import org.htmlunit.junit.annotation.Alerts;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
23+
/**
24+
* Test for error handling.
25+
*
26+
* @author Ronald Brill
27+
*/
28+
@RunWith(BrowserRunner.class)
29+
public class ErrorTest extends WebDriverTestCase {
30+
31+
/**
32+
* @throws Exception if the test fails
33+
*/
34+
@Test
35+
@Alerts(DEFAULT = {"Error", "Whoops!", "undefined", "undefined", "undefined",
36+
"undefined", "true", "Error/Error"},
37+
FF = {"Error", "Whoops!", "undefined", "11", "undefined", "25", "true", "Error/Error"},
38+
FF_ESR = {"Error", "Whoops!", "undefined", "11", "undefined", "25", "true", "Error/Error"})
39+
public void error() throws Exception {
40+
final String html
41+
= "<html>\n"
42+
+ "<head>\n"
43+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
44+
+ "</head>\n"
45+
+ "<body>\n"
46+
+ "<script>"
47+
+ " try {\n"
48+
+ " throw new Error('Whoops!');\n"
49+
+ " } catch (e) {"
50+
+ " log(e.name);\n"
51+
+ " log(e.message);\n"
52+
+ " log(e.cause);\n"
53+
+ " log(e.columnNumber);\n"
54+
+ " log(e.filename);\n"
55+
+ " log(e.lineNumber);\n"
56+
+ " log(e instanceof Error);\n"
57+
+ " logEx(e);\n"
58+
+ " }\n"
59+
+ "</script>\n"
60+
+ "</body></html>";
61+
62+
loadPageVerifyTitle2(html);
63+
}
64+
65+
/**
66+
* @throws Exception if the test fails
67+
*/
68+
@Test
69+
@Alerts(DEFAULT = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
70+
"undefined", "true", "true", "EvalError/EvalError"},
71+
FF = {"EvalError", "Whoops!", "undefined", "11", "undefined", "25",
72+
"true", "true", "EvalError/EvalError"},
73+
FF_ESR = {"EvalError", "Whoops!", "undefined", "11", "undefined", "25",
74+
"true", "true", "EvalError/EvalError"})
75+
public void evalError() throws Exception {
76+
final String html
77+
= "<html>\n"
78+
+ "<head>\n"
79+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
80+
+ "</head>\n"
81+
+ "<body>\n"
82+
+ "<script>"
83+
+ " try {\n"
84+
+ " throw new EvalError('Whoops!');\n"
85+
+ " } catch (e) {"
86+
+ " log(e.name);\n"
87+
+ " log(e.message);\n"
88+
+ " log(e.cause);\n"
89+
+ " log(e.columnNumber);\n"
90+
+ " log(e.filename);\n"
91+
+ " log(e.lineNumber);\n"
92+
+ " log(e instanceof Error);\n"
93+
+ " log(e instanceof EvalError);\n"
94+
+ " logEx(e);\n"
95+
+ " }\n"
96+
+ "</script>\n"
97+
+ "</body></html>";
98+
99+
loadPageVerifyTitle2(html);
100+
}
101+
102+
/**
103+
* @throws Exception if the test fails
104+
*/
105+
@Test
106+
@Alerts(DEFAULT = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
107+
"undefined", "true", "true", "RangeError/RangeError"},
108+
FF = {"RangeError", "Whoops!", "undefined", "11", "undefined", "25",
109+
"true", "true", "RangeError/RangeError"},
110+
FF_ESR = {"RangeError", "Whoops!", "undefined", "11", "undefined", "25",
111+
"true", "true", "RangeError/RangeError"})
112+
public void rangeError() throws Exception {
113+
final String html
114+
= "<html>\n"
115+
+ "<head>\n"
116+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
117+
+ "</head>\n"
118+
+ "<body>\n"
119+
+ "<script>"
120+
+ " try {\n"
121+
+ " throw new RangeError('Whoops!');\n"
122+
+ " } catch (e) {"
123+
+ " log(e.name);\n"
124+
+ " log(e.message);\n"
125+
+ " log(e.cause);\n"
126+
+ " log(e.columnNumber);\n"
127+
+ " log(e.filename);\n"
128+
+ " log(e.lineNumber);\n"
129+
+ " log(e instanceof Error);\n"
130+
+ " log(e instanceof RangeError);\n"
131+
+ " logEx(e);\n"
132+
+ " }\n"
133+
+ "</script>\n"
134+
+ "</body></html>";
135+
136+
loadPageVerifyTitle2(html);
137+
}
138+
139+
/**
140+
* @throws Exception if the test fails
141+
*/
142+
@Test
143+
@Alerts(DEFAULT = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
144+
"undefined", "true", "true", "ReferenceError/ReferenceError"},
145+
FF = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "25",
146+
"true", "true", "ReferenceError/ReferenceError"},
147+
FF_ESR = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "25",
148+
"true", "true", "ReferenceError/ReferenceError"})
149+
public void referenceError() throws Exception {
150+
final String html
151+
= "<html>\n"
152+
+ "<head>\n"
153+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
154+
+ "</head>\n"
155+
+ "<body>\n"
156+
+ "<script>"
157+
+ " try {\n"
158+
+ " throw new ReferenceError('Whoops!');\n"
159+
+ " } catch (e) {"
160+
+ " log(e.name);\n"
161+
+ " log(e.message);\n"
162+
+ " log(e.cause);\n"
163+
+ " log(e.columnNumber);\n"
164+
+ " log(e.filename);\n"
165+
+ " log(e.lineNumber);\n"
166+
+ " log(e instanceof Error);\n"
167+
+ " log(e instanceof ReferenceError);\n"
168+
+ " logEx(e);\n"
169+
+ " }\n"
170+
+ "</script>\n"
171+
+ "</body></html>";
172+
173+
loadPageVerifyTitle2(html);
174+
}
175+
176+
/**
177+
* @throws Exception if the test fails
178+
*/
179+
@Test
180+
@Alerts(DEFAULT = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
181+
"undefined", "true", "true", "SyntaxError/SyntaxError"},
182+
FF = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "25",
183+
"true", "true", "SyntaxError/SyntaxError"},
184+
FF_ESR = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "25",
185+
"true", "true", "SyntaxError/SyntaxError"})
186+
public void syntaxError() throws Exception {
187+
final String html
188+
= "<html>\n"
189+
+ "<head>\n"
190+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
191+
+ "</head>\n"
192+
+ "<body>\n"
193+
+ "<script>"
194+
+ " try {\n"
195+
+ " throw new SyntaxError('Whoops!');\n"
196+
+ " } catch (e) {"
197+
+ " log(e.name);\n"
198+
+ " log(e.message);\n"
199+
+ " log(e.cause);\n"
200+
+ " log(e.columnNumber);\n"
201+
+ " log(e.filename);\n"
202+
+ " log(e.lineNumber);\n"
203+
+ " log(e instanceof Error);\n"
204+
+ " log(e instanceof SyntaxError);\n"
205+
+ " logEx(e);\n"
206+
+ " }\n"
207+
+ "</script>\n"
208+
+ "</body></html>";
209+
210+
loadPageVerifyTitle2(html);
211+
}
212+
213+
/**
214+
* @throws Exception if the test fails
215+
*/
216+
@Test
217+
@Alerts(DEFAULT = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
218+
"undefined", "true", "true", "TypeError/TypeError"},
219+
FF = {"TypeError", "Whoops!", "undefined", "11", "undefined", "25",
220+
"true", "true", "TypeError/TypeError"},
221+
FF_ESR = {"TypeError", "Whoops!", "undefined", "11", "undefined", "25",
222+
"true", "true", "TypeError/TypeError"})
223+
public void typeError() throws Exception {
224+
final String html
225+
= "<html>\n"
226+
+ "<head>\n"
227+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
228+
+ "</head>\n"
229+
+ "<body>\n"
230+
+ "<script>"
231+
+ " try {\n"
232+
+ " throw new TypeError('Whoops!');\n"
233+
+ " } catch (e) {"
234+
+ " log(e.name);\n"
235+
+ " log(e.message);\n"
236+
+ " log(e.cause);\n"
237+
+ " log(e.columnNumber);\n"
238+
+ " log(e.filename);\n"
239+
+ " log(e.lineNumber);\n"
240+
+ " log(e instanceof Error);\n"
241+
+ " log(e instanceof TypeError);\n"
242+
+ " logEx(e);\n"
243+
+ " }\n"
244+
+ "</script>\n"
245+
+ "</body></html>";
246+
247+
loadPageVerifyTitle2(html);
248+
}
249+
250+
/**
251+
* @throws Exception if the test fails
252+
*/
253+
@Test
254+
@Alerts(DEFAULT = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
255+
"undefined", "true", "true", "URIError/URIError"},
256+
FF = {"URIError", "Whoops!", "undefined", "11", "undefined", "25",
257+
"true", "true", "URIError/URIError"},
258+
FF_ESR = {"URIError", "Whoops!", "undefined", "11", "undefined", "25",
259+
"true", "true", "URIError/URIError"})
260+
public void uriError() throws Exception {
261+
final String html
262+
= "<html>\n"
263+
+ "<head>\n"
264+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
265+
+ "</head>\n"
266+
+ "<body>\n"
267+
+ "<script>"
268+
+ " try {\n"
269+
+ " throw new URIError('Whoops!');\n"
270+
+ " } catch (e) {"
271+
+ " log(e.name);\n"
272+
+ " log(e.message);\n"
273+
+ " log(e.cause);\n"
274+
+ " log(e.columnNumber);\n"
275+
+ " log(e.filename);\n"
276+
+ " log(e.lineNumber);\n"
277+
+ " log(e instanceof Error);\n"
278+
+ " log(e instanceof URIError);\n"
279+
+ " logEx(e);\n"
280+
+ " }\n"
281+
+ "</script>\n"
282+
+ "</body></html>";
283+
284+
loadPageVerifyTitle2(html);
285+
}
286+
287+
/**
288+
* @throws Exception if the test fails
289+
*/
290+
@Test
291+
@Alerts(DEFAULT = {"AggregateError", "Whoops!", "undefined", "Error: some error",
292+
"undefined", "undefined",
293+
"undefined", "true", "true", "AggregateError/AggregateError"},
294+
FF = {"AggregateError", "Whoops!", "undefined", "Error: some error",
295+
"11", "undefined", "25",
296+
"true", "true", "AggregateError/AggregateError"},
297+
FF_ESR = {"AggregateError", "Whoops!", "undefined", "Error: some error",
298+
"11", "undefined", "25",
299+
"true", "true", "AggregateError/AggregateError"})
300+
public void aggregateError() throws Exception {
301+
final String html
302+
= "<html>\n"
303+
+ "<head>\n"
304+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
305+
+ "</head>\n"
306+
+ "<body>\n"
307+
+ "<script>"
308+
+ " try {\n"
309+
+ " throw new AggregateError([new Error(\"some error\")], 'Whoops!');\n"
310+
+ " } catch (e) {"
311+
+ " log(e.name);\n"
312+
+ " log(e.message);\n"
313+
+ " log(e.cause);\n"
314+
+ " log(e.errors);\n"
315+
+ " log(e.columnNumber);\n"
316+
+ " log(e.filename);\n"
317+
+ " log(e.lineNumber);\n"
318+
+ " log(e instanceof Error);\n"
319+
+ " log(e instanceof AggregateError);\n"
320+
+ " logEx(e);\n"
321+
+ " }\n"
322+
+ "</script>\n"
323+
+ "</body></html>";
324+
325+
loadPageVerifyTitle2(html);
326+
}
327+
328+
/**
329+
* @throws Exception if the test fails
330+
*/
331+
@Test
332+
@Alerts(DEFAULT = {"ReferenceError", "InternalError is not defined",
333+
"undefined", "undefined", "undefined",
334+
"undefined", "true", "false", "ReferenceError/ReferenceError"},
335+
FF = {"InternalError", "Whoops!", "undefined", "11", "undefined", "25",
336+
"true", "true", "InternalError/InternalError"},
337+
FF_ESR = {"InternalError", "Whoops!", "undefined", "11", "undefined", "25",
338+
"true", "true", "InternalError/InternalError"})
339+
public void internalError() throws Exception {
340+
final String html
341+
= "<html>\n"
342+
+ "<head>\n"
343+
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
344+
+ "</head>\n"
345+
+ "<body>\n"
346+
+ "<script>"
347+
+ " try {\n"
348+
+ " throw new InternalError('Whoops!');\n"
349+
+ " } catch (e) {"
350+
+ " log(e.name);\n"
351+
+ " log(e.message);\n"
352+
+ " log(e.cause);\n"
353+
+ " log(e.columnNumber);\n"
354+
+ " log(e.filename);\n"
355+
+ " log(e.lineNumber);\n"
356+
+ " log(e instanceof Error);\n"
357+
+ " log(typeof InternalError == 'function' && e instanceof InternalError);\n"
358+
+ " logEx(e);\n"
359+
+ " }\n"
360+
+ "</script>\n"
361+
+ "</body></html>";
362+
363+
loadPageVerifyTitle2(html);
364+
}
365+
}

0 commit comments

Comments
 (0)