From 6f090565aa960794bbdcddfe422c643a7ab55f89 Mon Sep 17 00:00:00 2001 From: Sigmund Cherem Date: Fri, 6 Oct 2023 00:58:54 +0000 Subject: [PATCH] [test_runner] include error stack in window.onerror if available. According to the [Window.onerror MDN docs][1] an error object may be provided as a 5th argument. This error object can be used to extract a stack trace with more context about the error message. [1]: https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event With this change, we can get a more precise error on uncuaght exceptions. This is an example of a before and after on a unit test failing in ddc-chrome-linux. Before: ``` Runtime window.onerror: window.onerror called: http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:5082: Uncaught Error ``` After: ``` Runtime window.onerror: window.onerror called: http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:5082: Uncaught Error Error: Expected a value of type 'FutureOr?', but got one of type 'LegacyJavaScriptObject' at Object.throw_ [as throw] (http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:5082:11) at Object.castError (http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:5041:15) at Object.cast [as as] (http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:5385:19) at dart.NullableType.new.as (http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:7243:60) at _AsyncCompleter.new.complete (http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:31216:28) at http://127.0.0.1:46235/root_build/gen/utils/ddc/stable/sdk/amd/dart_sdk.js:99073:19 ``` Change-Id: If89c253d8e6edee9b7102269466b40b605951a6c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328804 Reviewed-by: Nicholas Shahan Commit-Queue: Sigmund Cherem --- pkg/test_runner/lib/src/browser_controller.dart | 13 ++++++++----- pkg/test_runner/lib/src/test_controller.js | 8 ++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/pkg/test_runner/lib/src/browser_controller.dart b/pkg/test_runner/lib/src/browser_controller.dart index 4c3ab8bd29c..b0e400cdf29 100644 --- a/pkg/test_runner/lib/src/browser_controller.dart +++ b/pkg/test_runner/lib/src/browser_controller.dart @@ -1388,12 +1388,15 @@ body div { test_completed = false; } - window.onerror = function (message, url, lineNumber) { - if (url) { - reportError(url + ':' + lineNumber + ':' + message); - } else { - reportError(message); + window.onerror = function (message, url, lineNumber, columnNumber, err) { + // Ensure the stack is included in the reported error, if available. + if (err && err.stack) { + message = message + '\\n' + err.stack; } + if (url) { + message = url + ':' + lineNumber + ':' + columnNumber + ':' + message; + } + reportError(message); } function reportError(msg) { diff --git a/pkg/test_runner/lib/src/test_controller.js b/pkg/test_runner/lib/src/test_controller.js index e7523427904..caaf894b3b4 100644 --- a/pkg/test_runner/lib/src/test_controller.js +++ b/pkg/test_runner/lib/src/test_controller.js @@ -93,10 +93,14 @@ var hadTopLevelError = false; // Set window onerror to make sure that we catch test harness errors across all // browsers. -window.onerror = function (message, url, lineNumber) { +window.onerror = function (message, url, lineNumber, columnNumber, err) { + // Ensure the stack is included in the reported error, if available. + if (err && err.stack) { + message = message + '\n' + err.stack; + } if (url) { message = ('window.onerror called: \n\n' + - url + ':' + lineNumber + ':\n' + message + '\n\n'); + url + ':' + lineNumber + ':' + columnNumber + ':\n' + message + '\n\n'); } if (testExpectsGlobalError) { testSuppressedGlobalErrors.push({