Skip to content

Truly independent web browser

LibWeb: Use a DOMException's name and message in error reports

Shannon Boothbd8daf804fd5
diff --git a/Libraries/LibWeb/HTML/ErrorInformation.cpp b/Libraries/LibWeb/HTML/ErrorInformation.cpp
index 0cfc9ecbda4..d59e12b3c39 100644
--- a/Libraries/LibWeb/HTML/ErrorInformation.cpp
+++ b/Libraries/LibWeb/HTML/ErrorInformation.cpp
@@ -8,6 +8,7 @@
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/HTML/ErrorInformation.h>
+#include <LibWeb/WebIDL/DOMException.h>
namespace Web::HTML {
@@ -22,21 +23,27 @@ ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)
// 3. Set attributes[message], attributes[filename], attributes[lineno], and attributes[colno] to
// implementation-defined values derived from exception.
+ // NB: An Error's or a DOMException's message is prefixed with its name, as Error.prototype.toString() does.
+ auto prefix_with_name = [](Utf16String name, Utf16String message) {
+ if (message.is_empty())
+ return name;
+ if (name.is_empty())
+ return message;
+ return Utf16String::formatted("{}: {}", name, message);
+ };
+
attributes.message = [&] {
if (auto object = exception.as_if<JS::Object>()) {
+ // NB: A DOMException's name and message are accessors on its prototype.
+ if (auto const* dom_exception = Bindings::dom_exception_from_object(*object))
+ return prefix_with_name(dom_exception->name().to_utf16_string(), dom_exception->message().to_utf16_string());
+
if (MUST(object->has_own_property(vm.names.message))) {
auto message = object->get_without_side_effects(vm.names.message).to_utf16_string_without_side_effects();
-
- // NB: The message of an Error object is prefixed with its name, as Error.prototype.toString() does.
if (is<JS::Error>(*object)) {
auto name = object->get_without_side_effects(vm.names.name);
- if (name.is_string()) {
- auto name_string = name.to_utf16_string_without_side_effects();
- if (message.is_empty())
- return name_string;
- if (!name_string.is_empty())
- return Utf16String::formatted("{}: {}", name_string, message);
- }
+ if (name.is_string())
+ return prefix_with_name(name.to_utf16_string_without_side_effects(), move(message));
}
return message;
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 4dda98d902c..f4d0a5437d5 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -1196,6 +1196,8 @@ Text/input/wpt-import/workers/SharedWorker-extendedLifetime.html
Text/input/wpt-import/workers/SharedWorker-extendedLifetime-blob.html
Text/input/wpt-import/workers/SharedWorker-extendedLifetime-data.html
Text/input/wpt-import/workers/SharedWorker-extendedLifetime-named-module.html
+Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.html
+Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html
[CapFrameRate]
; Ends a smooth scroll once 20 frames see no change, which frames faster than the compositor's ticks do
diff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.txt
new file mode 100644
index 00000000000..ebc964a1163
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 10 tests
+
+10 Pass
+Pass Throw DOMException-TypeError in toplevel: classic: listener
+Pass Throw DOMException-TypeError in toplevel: classic: handler
+Pass Throw DOMException-TypeError in setTimeout-function: classic: listener
+Pass Throw DOMException-TypeError in setTimeout-function: classic: handler
+Pass Throw DOMException-TypeError in setTimeout-string: classic: listener
+Pass Throw DOMException-TypeError in setTimeout-string: classic: handler
+Pass Throw DOMException-TypeError in onmessage: classic: listener
+Pass Throw DOMException-TypeError in onmessage: classic: handler
+Pass Throw DOMException-TypeError in onerror: classic: listener
+Pass Throw DOMException-TypeError in onerror: classic: handler
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.txt
new file mode 100644
index 00000000000..d0626ddbe54
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.txt
@@ -0,0 +1,15 @@
+Harness status: OK
+
+Found 10 tests
+
+10 Pass
+Pass Throw DOMException-TypeError in toplevel: module: listener
+Pass Throw DOMException-TypeError in toplevel: module: handler
+Pass Throw DOMException-TypeError in setTimeout-function: module: listener
+Pass Throw DOMException-TypeError in setTimeout-function: module: handler
+Pass Throw DOMException-TypeError in setTimeout-string: module: listener
+Pass Throw DOMException-TypeError in setTimeout-string: module: handler
+Pass Throw DOMException-TypeError in onmessage: module: listener
+Pass Throw DOMException-TypeError in onmessage: module: handler
+Pass Throw DOMException-TypeError in onerror: module: listener
+Pass Throw DOMException-TypeError in onerror: module: handler
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.html b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.html
new file mode 100644
index 00000000000..19fbda62ec7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.html
@@ -0,0 +1,7 @@
+<!doctype html>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="./message-helper.js"></script>
+<script>
+runTest('classic', 'DOMException-TypeError');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-helper.js b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-helper.js
new file mode 100644
index 00000000000..6271329b031
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-helper.js
@@ -0,0 +1,61 @@
+// The error's `message` values in Worker error event handlers are tested.
+// While not explicitly specified in the HTML spec, we expect some information
+// about thrown errors (e.g. original message, the string "TypeError", etc.)
+// to appear in the `message`.
+
+function prepareHandler(t, error, expectedCount) {
+ let count = 0;
+ return t.step_func(e => {
+ e.preventDefault();
+
+ assert_regexp_match(
+ e.message,
+ /Throw in/,
+ 'e.message should contain the message of the thrown error');
+
+ if (error === 'DOMException-TypeError') {
+ assert_regexp_match(e.message, /TypeError/);
+ }
+
+ ++count;
+ if (count >= expectedCount) {
+ t.done();
+ }
+ });
+}
+
+function expectErrors(worker, error, expectedCount, title) {
+ async_test(t => {
+ worker.addEventListener('error',
+ prepareHandler(t, error, expectedCount));
+ }, title+ ': listener');
+ async_test(t => {
+ worker.onerror = prepareHandler(t, error, expectedCount);
+ }, title + ': handler');
+}
+
+function runTest(type, error) {
+ for (const location of ['toplevel',
+ 'setTimeout-function',
+ 'setTimeout-string',
+ 'onmessage',
+ 'onerror']) {
+ const worker = new Worker(
+ 'throw.js?throw-in-' + location + '&error=' + error,
+ {type});
+ let expectedCount = 1;
+ if (location === 'onmessage') {
+ // This makes the worker's message handler to throw an error.
+ worker.postMessage('foo');
+ }
+ if (location === 'onerror') {
+ // This makes the worker's message handler to throw an error,
+ // AND worker's error handler to throw another error.
+ // Therefore we expect two errors here.
+ worker.postMessage('foo');
+ expectedCount = 2;
+ }
+ expectErrors(worker, error, expectedCount,
+ 'Throw ' + error + ' in ' + location + ': ' + type);
+ }
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html
new file mode 100644
index 00000000000..cee37ed7533
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html
@@ -0,0 +1,7 @@
+<!doctype html>
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="./message-helper.js"></script>
+<script>
+runTest('module', 'DOMException-TypeError');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/throw.js b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/throw.js
new file mode 100644
index 00000000000..704098a6d86
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/throw.js
@@ -0,0 +1,34 @@
+const params = new URL(self.location.href).searchParams;
+
+self.createError = (message) => {
+ if (params.get('error') === 'DOMException-TypeError') {
+ return new DOMException(message, 'TypeError');
+ } else {
+ return new Error(message);
+ }
+};
+
+onerror = function() {
+ if (params.has('throw-in-onerror')) {
+ throw createError('Throw in error handler');
+ }
+ return false;
+};
+onmessage = function() {
+ throw createError('Throw in message handler');
+ return false;
+};
+
+if (params.has('throw-in-toplevel')) {
+ throw createError('Throw in toplevel');
+}
+
+// We don't use step_timeout() here, because we have to test the behavior of
+// setTimeout() without wrappers.
+if (params.has('throw-in-setTimeout-function')) {
+ setTimeout(() => { throw createError('Throw in setTimeout function') }, 0);
+}
+
+if (params.has('throw-in-setTimeout-string')) {
+ setTimeout("throw createError('Throw in setTimeout string')", 0);
+}