Truly independent web browser
LibWeb: Report where a non-Error exception was thrown
Shannon Boothc665272d1c97
diff --git a/Libraries/LibWeb/HTML/ErrorInformation.cpp b/Libraries/LibWeb/HTML/ErrorInformation.cppindex d59e12b3c39..97976679fbf 100644--- a/Libraries/LibWeb/HTML/ErrorInformation.cpp+++ b/Libraries/LibWeb/HTML/ErrorInformation.cpp@@ -55,6 +55,9 @@ ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)// FIXME: This offset is relative to the javascript source. Other browsers appear to do it relative// to the entire source document! Calculate that somehow.+ // NB: Take the throw site even when an Error's traceback gives the location, so the VM stops keeping it alive.+ auto throw_site = vm.take_throw_site_of(exception);+// NB: If we got an Error object, then try and extract the information from the location the object was made.if (auto object = exception.as_if<JS::Object>(); object && object->error_data()) {for (auto const& frame : object->error_data()->traceback()) {@@ -67,6 +70,12 @@ ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)}}}+ // NB: Otherwise, if a throw statement threw it, take the location from there.+ else if (throw_site.has_value()) {+ attributes.filename = throw_site->filename();+ attributes.lineno = throw_site->start.line;+ attributes.colno = throw_site->start.column;+ }// NB: Otherwise, we fall back to try and find the location of the invocation of the function itself.else {for (auto const& frame : vm.stack_trace()) {diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex c807aa8707e..39c9120482d 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -1198,6 +1198,8 @@ Text/input/wpt-import/workers/SharedWorker-extendedLifetime-data.htmlText/input/wpt-import/workers/SharedWorker-extendedLifetime-named-module.htmlText/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.htmlText/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html+Text/input/wpt-import/workers/semantics/reporting-errors/001.html+Text/input/wpt-import/workers/semantics/reporting-errors/002.htmlText/input/wpt-import/workers/modules/dedicated-worker-import-failure.htmlText/input/wpt-import/workers/modules/shared-worker-import-failure.htmldiff --git a/Tests/LibWeb/Text/expected/HTML/error-location-after-swallowed-throw.txt b/Tests/LibWeb/Text/expected/HTML/error-location-after-swallowed-throw.txtnew file mode 100644index 00000000000..6df64863355--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/error-location-after-swallowed-throw.txt@@ -0,0 +1 @@+Uncaught exception: [object Object] at line 20, column 58diff --git a/Tests/LibWeb/Text/expected/HTML/error-location-of-thrown-primitive.txt b/Tests/LibWeb/Text/expected/HTML/error-location-of-thrown-primitive.txtnew file mode 100644index 00000000000..4b2e95d9dbf--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/error-location-of-thrown-primitive.txt@@ -0,0 +1,3 @@+Uncaught exception: 42 at line 14, column 13+Uncaught exception: 42 at line 17, column 20+Uncaught exception: 42 at line 22, column 20diff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/001.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/001.txtnew file mode 100644index 00000000000..cc01225df3d--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/001.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass shared worker, not handled\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/002.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/002.txtnew file mode 100644index 00000000000..0ddc3ec6934--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/semantics/reporting-errors/002.txt@@ -0,0 +1,6 @@+Harness status: OK++Found 1 tests++1 Pass+Pass shared worker, addEventListener\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/error-location-after-swallowed-throw.html b/Tests/LibWeb/Text/input/HTML/error-location-after-swallowed-throw.htmlnew file mode 100644index 00000000000..050091b9496--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/error-location-after-swallowed-throw.html@@ -0,0 +1,22 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(done => {+ removeTestErrorHandler();++ window.onerror = (message, filename, lineno, colno) => {+ println(`${message} at line ${lineno}, column ${colno}`);+ done();+ return true;+ };++ const reason = {};+ new Promise(() => {+ throw reason;+ }).catch(() => {});++ const controller = new AbortController();+ controller.abort(reason);+ setTimeout(() => controller.signal.throwIfAborted());+ });+</script>diff --git a/Tests/LibWeb/Text/input/HTML/error-location-of-thrown-primitive.html b/Tests/LibWeb/Text/input/HTML/error-location-of-thrown-primitive.htmlnew file mode 100644index 00000000000..1fc8025a97d--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/error-location-of-thrown-primitive.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ test(() => {+ removeTestErrorHandler();++ window.onerror = (message, filename, lineno, colno) => {+ println(`${message} at line ${lineno}, column ${colno}`);+ return true;+ };++ const target = new EventTarget();+ target.addEventListener("throw", () => {+ throw 42;+ });+ target.dispatchEvent(new Event("throw"));+ reportError(42);++ try {+ throw 42;+ } catch {}+ reportError(42);+ });+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.html b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.htmlnew file mode 100644index 00000000000..348e3e27f68--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.html@@ -0,0 +1,17 @@+<!doctype html>+<title>shared worker, not handled</title>+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<div id=log></div>+<script>+setup({allow_uncaught_exception:true});+async_test(function() {+ window.onerror = this.step_func(function(a) {+ assert_unreached('window.onerror invoked: ' + a);+ });+ var worker = new SharedWorker('001.js', '');+ worker.port.onmessage = this.step_func_done(function(e) {+ assert_equals(e.data, '');+ });+});+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.js b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.jsnew file mode 100644index 00000000000..5736666cc3b--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/001.js@@ -0,0 +1,28 @@+var port;+var timeout;+onerror = function(a,b,c,d,e) {+ // will return undefined, thus the error is "not handled"+ // so error should be reported to the user, but this test doesn't check+ // that.+ // just make sure that this method is invoked with five arguments+ clearTimeout(timeout);+ var log = '';+ if (arguments.length != 5)+ log += 'got ' + arguments.length + ' arguments, expected 5. ';+ if (typeof a != 'string')+ log += 'first argument wasn\'t a string. ';+ if (b != location.href)+ log += 'second argument was ' + b + ', expected ' + location.href + '. ';+ if (typeof c != 'number')+ log += 'third argument wasn\'t a number. ';+ if (typeof d != 'number')+ log += 'fourth argument wasn\'t a number. ';+ if (e != 42)+ log += 'fifth argument wasn\'t the thrown exception. ';+ port.postMessage(log);+}+onconnect = function (e) {+ port = e.ports[0];+ timeout = setTimeout(function() { port.postMessage('self.onerror was not invoked'); }, 250);+ throw 42; // will "report the error"+}diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.html b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.htmlnew file mode 100644index 00000000000..edb0708bd79--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.html@@ -0,0 +1,17 @@+<!doctype html>+<title>shared worker, addEventListener</title>+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<div id=log></div>+<script>+setup({allow_uncaught_exception:true});+async_test(function() {+ window.onerror = this.step_func(function(a) {+ assert_unreached('window.onerror invoked: ' + a);+ });+ var worker = new SharedWorker('002.js', '');+ worker.port.onmessage = this.step_func_done(function(e) {+ assert_equals(e.data, '');+ });+});+</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.js b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.jsnew file mode 100644index 00000000000..ff96922634f--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/semantics/reporting-errors/002.js@@ -0,0 +1,34 @@+var port;+var timeout;+addEventListener('error', function(e) {+ // event is not canceled, thus the error is "not handled"+ // so error should be reported to the user, but this test doesn't check+ // that.+ // just make sure that this event has the right properties+ clearTimeout(timeout);+ var log = '';+ if (!self.ErrorEvent || Object.getPrototypeOf(e) != ErrorEvent.prototype)+ log += 'event should be an ErrorEvent. ';+ if (e.bubbles)+ log += 'event should not bubble. ';+ if (!e.cancelable)+ log += 'event should be cancelable. ';+ if (!e.isTrusted)+ log += 'event should be trusted. ';+ if (typeof e.message != 'string')+ log += 'message wasn\'t a string. ';+ if (e.filename != location.href)+ log += 'filename was ' + e.filename + ', expected ' + location.href + '. ';+ if (typeof e.lineno != 'number')+ log += 'lineno wasn\'t a number. ';+ if (typeof e.colno != 'number')+ log += 'colno argument wasn\'t a number. ';+ if (e.error != 42)+ log += 'fifth argument wasn\'t the thrown exception. ';+ port.postMessage(log);+}, false);+onconnect = function (e) {+ port = e.ports[0];+ timeout = setTimeout(function() { port.postMessage('No error event fired'); }, 250);+ throw 42; // will "report the error"+}