Skip to content

Commit

Permalink
more details for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 3, 2025
1 parent fbca0ae commit bafa0c7
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/test/java/org/htmlunit/WebDriverTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,22 @@ public abstract class WebDriverTestCase extends WebTestCase {

private static final String LOG_EX_FUNCTION =
" function logEx(e) {\n"
+ " let rx = /\\[object (.*)\\]/;\n"
+ " let toStr = Object.prototype.toString.call(e);\n"
+ " let match = rx.exec(toStr);\n"
+ " if (match != null) { toStr = match[1]; }\n"
+ " let toStr = null;\n"
+ " if (toStr === null && e instanceof EvalError) { toStr = 'EvalError'; }\n"
+ " if (toStr === null && e instanceof RangeError) { toStr = 'RangeError'; }\n"
+ " if (toStr === null && e instanceof ReferenceError) { toStr = 'ReferenceError'; }\n"
+ " if (toStr === null && e instanceof SyntaxError) { toStr = 'SyntaxError'; }\n"
+ " if (toStr === null && e instanceof TypeError) { toStr = 'TypeError'; }\n"
+ " if (toStr === null && e instanceof URIError) { toStr = 'URIError'; }\n"
+ " if (toStr === null && e instanceof AggregateError) { toStr = 'AggregateError'; }\n"
+ " if (toStr === null && typeof InternalError == 'function' "
+ "&& e instanceof InternalError) { toStr = 'InternalError'; }\n"
+ " if (toStr === null) {\n"
+ " let rx = /\\[object (.*)\\]/;\n"
+ " toStr = Object.prototype.toString.call(e);\n"
+ " let match = rx.exec(toStr);\n"
+ " if (match != null) { toStr = match[1]; }\n"
+ " }"
+ " log(e.name + '/' + toStr);\n"
+ "}\n";

Expand Down
365 changes: 365 additions & 0 deletions src/test/java/org/htmlunit/javascript/ErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
/*
* Copyright (c) 2002-2025 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.javascript;

import org.htmlunit.WebDriverTestCase;
import org.htmlunit.junit.BrowserRunner;
import org.htmlunit.junit.annotation.Alerts;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Test for error handling.
*
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class ErrorTest extends WebDriverTestCase {

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"Error", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "Error/Error"},
FF = {"Error", "Whoops!", "undefined", "11", "undefined", "25", "true", "Error/Error"},
FF_ESR = {"Error", "Whoops!", "undefined", "11", "undefined", "25", "true", "Error/Error"})
public void error() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new Error('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"EvalError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "EvalError/EvalError"},
FF = {"EvalError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "EvalError/EvalError"},
FF_ESR = {"EvalError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "EvalError/EvalError"})
public void evalError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new EvalError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof EvalError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"RangeError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "RangeError/RangeError"},
FF = {"RangeError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "RangeError/RangeError"},
FF_ESR = {"RangeError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "RangeError/RangeError"})
public void rangeError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new RangeError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof RangeError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"ReferenceError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "ReferenceError/ReferenceError"},
FF = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "ReferenceError/ReferenceError"},
FF_ESR = {"ReferenceError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "ReferenceError/ReferenceError"})
public void referenceError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new ReferenceError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof ReferenceError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"SyntaxError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "SyntaxError/SyntaxError"},
FF = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "SyntaxError/SyntaxError"},
FF_ESR = {"SyntaxError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "SyntaxError/SyntaxError"})
public void syntaxError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new SyntaxError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof SyntaxError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"TypeError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "TypeError/TypeError"},
FF = {"TypeError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "TypeError/TypeError"},
FF_ESR = {"TypeError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "TypeError/TypeError"})
public void typeError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new TypeError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof TypeError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"URIError", "Whoops!", "undefined", "undefined", "undefined",
"undefined", "true", "true", "URIError/URIError"},
FF = {"URIError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "URIError/URIError"},
FF_ESR = {"URIError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "URIError/URIError"})
public void uriError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new URIError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof URIError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"AggregateError", "Whoops!", "undefined", "Error: some error",
"undefined", "undefined",
"undefined", "true", "true", "AggregateError/AggregateError"},
FF = {"AggregateError", "Whoops!", "undefined", "Error: some error",
"11", "undefined", "25",
"true", "true", "AggregateError/AggregateError"},
FF_ESR = {"AggregateError", "Whoops!", "undefined", "Error: some error",
"11", "undefined", "25",
"true", "true", "AggregateError/AggregateError"})
public void aggregateError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new AggregateError([new Error(\"some error\")], 'Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.errors);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(e instanceof AggregateError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}

/**
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = {"ReferenceError", "InternalError is not defined",
"undefined", "undefined", "undefined",
"undefined", "true", "false", "ReferenceError/ReferenceError"},
FF = {"InternalError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "InternalError/InternalError"},
FF_ESR = {"InternalError", "Whoops!", "undefined", "11", "undefined", "25",
"true", "true", "InternalError/InternalError"})
public void internalError() throws Exception {
final String html
= "<html>\n"
+ "<head>\n"
+ "<script>" + LOG_TITLE_FUNCTION + "</script>\n"
+ "</head>\n"
+ "<body>\n"
+ "<script>"
+ " try {\n"
+ " throw new InternalError('Whoops!');\n"
+ " } catch (e) {"
+ " log(e.name);\n"
+ " log(e.message);\n"
+ " log(e.cause);\n"
+ " log(e.columnNumber);\n"
+ " log(e.filename);\n"
+ " log(e.lineNumber);\n"
+ " log(e instanceof Error);\n"
+ " log(typeof InternalError == 'function' && e instanceof InternalError);\n"
+ " logEx(e);\n"
+ " }\n"
+ "</script>\n"
+ "</body></html>";

loadPageVerifyTitle2(html);
}
}

0 comments on commit bafa0c7

Please sign in to comment.