Skip to content

Commit

Permalink
Handle null and undefined thrown as exceptions
Browse files Browse the repository at this point in the history
Closes #3183.
  • Loading branch information
mbest committed May 22, 2021
1 parent 04f6c13 commit 39b7972
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/jsdom/living/helpers/runtime-script-errors.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
17 changes: 17 additions & 0 deletions test/api/jsdom-errors.js
Expand Up @@ -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 = `<body onclick="throw null"></body>`;
const doc = (new JSDOM(html, { virtualConsole, runScripts: "dangerously" })).window.document;

doc.body.click();
}, {
async: true
});
});
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<title>window.onerror: throwing null in event listeners</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
"use strict";

setup({ allow_uncaught_exception: true });

test(() => {
let errorEvents = 0;
let errorEvent;
window.addEventListener("error", event => {
errorEvent = event;
++errorEvents;
});

const element = document.createElement("div");

element.addEventListener("click", () => {
throw null; // eslint-disable-line no-throw-literal
});

element.dispatchEvent(new Event("click"));

assert_equals(errorEvents, 1);
assert_equals(errorEvent.message, "uncaught exception: null");
assert_equals(errorEvent.error, null);
}, "Throwing null in event listener");
</script>

0 comments on commit 39b7972

Please sign in to comment.