diff --git a/lib/jsdom/living/helpers/runtime-script-errors.js b/lib/jsdom/living/helpers/runtime-script-errors.js index 6deb1ee39a..41982b0de7 100644 --- a/lib/jsdom/living/helpers/runtime-script-errors.js +++ b/lib/jsdom/living/helpers/runtime-script-errors.js @@ -18,6 +18,10 @@ function reportAnError(line, col, target, errorObject, message, location) { target[errorReportingMode] = true; + if (typeof message !== "string") { + message = "uncaught exception: " + util.inspect(errorObject); + } + const event = createAnEvent("error", target._globalObject, ErrorEvent, { cancelable: true, message, @@ -55,7 +59,7 @@ module.exports = function reportException(window, error, filenameHint) { const windowImpl = idlUtils.implForWrapper(window); - const handled = reportAnError(lineNumber, columnNumber, windowImpl, error, error.message, fileName); + const handled = reportAnError(lineNumber, columnNumber, windowImpl, error, error && error.message, fileName); if (!handled) { const errorString = shouldBeDisplayedAsError(error) ? `[${error.name}: ${error.message}]` : util.inspect(error); @@ -68,5 +72,5 @@ module.exports = function reportException(window, error, filenameHint) { }; function shouldBeDisplayedAsError(x) { - return x.name && x.message !== undefined && x.stack; + return x && x.name && x.message !== undefined && x.stack; } diff --git a/test/api/jsdom-errors.js b/test/api/jsdom-errors.js index 1bdea47d6b..5f818d3e8c 100644 --- a/test/api/jsdom-errors.js +++ b/test/api/jsdom-errors.js @@ -33,4 +33,21 @@ describe("API: virtual console jsdomErrors", () => { assert.isEmpty(errors); }); + + it("should emit unhandled null value thrown in inline event handlers", t => { + const virtualConsole = new VirtualConsole(); + virtualConsole.on("jsdomError", error => { + assert.ok(error instanceof Error); + assert.equal(error.message, "Uncaught null"); + assert.isNull(error.detail); + t.done(); + }); + + const html = ``; + const doc = (new JSDOM(html, { virtualConsole, runScripts: "dangerously" })).window.document; + + doc.body.click(); + }, { + async: true + }); }); diff --git a/test/web-platform-tests/to-upstream/html/webappapis/scripting/processing-model-2/window-onerror-event-throw-null.html b/test/web-platform-tests/to-upstream/html/webappapis/scripting/processing-model-2/window-onerror-event-throw-null.html new file mode 100644 index 0000000000..6d7e0bcffa --- /dev/null +++ b/test/web-platform-tests/to-upstream/html/webappapis/scripting/processing-model-2/window-onerror-event-throw-null.html @@ -0,0 +1,32 @@ + + +window.onerror: throwing null in event listeners + + +
+