Truly independent web browser
LibWeb: Report the throw site of an error that has no stack frames
Andreas Kling14a57cb0e280
diff --git a/Libraries/LibWeb/HTML/ErrorInformation.cpp b/Libraries/LibWeb/HTML/ErrorInformation.cppindex 9de55e432ac..1f1737b4137 100644--- a/Libraries/LibWeb/HTML/ErrorInformation.cpp+++ b/Libraries/LibWeb/HTML/ErrorInformation.cpp@@ -61,6 +61,7 @@ ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)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.+ bool has_location = false;if (auto object = exception.as_if<JS::Object>(); object && object->error_data()) {for (auto const& frame : object->error_data()->traceback()) {auto source_range = frame.source_range();@@ -68,12 +69,17 @@ ErrorInformation extract_error_information(JS::VM& vm, JS::Value exception)attributes.filename = source_range.filename();attributes.lineno = source_range.start.line;attributes.colno = source_range.start.column;+ has_location = true;break;}}}- // NB: Otherwise, if a throw statement threw it, take the location from there.- else if (throw_site.has_value()) {+ if (has_location)+ return attributes;++ // NB: Otherwise, if a throw statement threw it, take the location from there. That includes an Error whose stack+ // has no frames, as with Error.stackTraceLimit set to 0.+ if (throw_site.has_value()) {attributes.filename = throw_site->filename();attributes.lineno = throw_site->start.line;attributes.colno = throw_site->start.column;diff --git a/Tests/LibWeb/Text/expected/error-event-location-without-stack-frames.txt b/Tests/LibWeb/Text/expected/error-event-location-without-stack-frames.txtnew file mode 100644index 00000000000..362b301ff6e--- /dev/null+++ b/Tests/LibWeb/Text/expected/error-event-location-without-stack-frames.txt@@ -0,0 +1,6 @@+10: thrown {"message":"Error: limit 10","filename":"error-event-location-without-stack-frames.html","lineno":2,"colno":9}+10: made {"message":"Error: made","filename":"error-event-location-without-stack-frames.html","lineno":3,"colno":15}+0: thrown {"message":"Error: limit 0","filename":"error-event-location-without-stack-frames.html","lineno":2,"colno":3}+0: made {"message":"Error: made","filename":"error-event-location-without-stack-frames.html","lineno":3,"colno":34}+"not a number": thrown {"message":"Error: limit not a number","filename":"error-event-location-without-stack-frames.html","lineno":2,"colno":3}+"not a number": made {"message":"Error: made","filename":"error-event-location-without-stack-frames.html","lineno":3,"colno":34}diff --git a/Tests/LibWeb/Text/input/error-event-location-without-stack-frames.html b/Tests/LibWeb/Text/input/error-event-location-without-stack-frames.htmlnew file mode 100644index 00000000000..4d8177d870a--- /dev/null+++ b/Tests/LibWeb/Text/input/error-event-location-without-stack-frames.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ removeTestErrorHandler();++ function runUncaught(source) {+ let reported;+ window.onerror = (message, filename, lineno, colno) => {+ reported = { message, filename: filename.split("/").pop(), lineno, colno };+ return true;+ };+ const script = document.createElement("script");+ script.textContent = source;+ document.body.appendChild(script);+ window.onerror = null;+ script.remove();+ return reported;+ }++ test(() => {+ const savedStackTraceLimit = Error.stackTraceLimit;+ try {+ for (const limit of [10, 0, "not a number"]) {+ Error.stackTraceLimit = limit;+ const thrown = runUncaught(`\n throw new Error("limit " + Error.stackTraceLimit);`);+ const made = runUncaught(`\n\n var error = new Error("made"); throw error;`);+ println(`${JSON.stringify(limit)}: thrown ${JSON.stringify(thrown)}`);+ println(`${JSON.stringify(limit)}: made ${JSON.stringify(made)}`);+ }+ } finally {+ Error.stackTraceLimit = savedStackTraceLimit;+ }+ });+</script>