Skip to content

Commit

Permalink
Bug 1879018 - Use replacer function to avoid calling JSON.stringify o…
Browse files Browse the repository at this point in the history
…n XPCOM objects in Assert.sys.mjs. r=jmaher

This is a better way to fix the problem than my previous attempt.

Differential Revision: https://phabricator.services.mozilla.com/D200908
  • Loading branch information
darktrojan committed Feb 7, 2024
1 parent 533f00b commit 59e708a
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions testing/modules/Assert.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function replacer(key, value) {
if (typeof value === "function" || instanceOf(value, "RegExp")) {
return value.toString();
}
if (
typeof value === "object" &&
value !== null &&
"QueryInterface" in value
) {
return value.toString();
}
return value;
}

Expand All @@ -74,25 +81,11 @@ function getMessage(error, prefix = "") {
// Wrap calls to JSON.stringify in try...catch blocks, as they may throw. If
// so, fall back to toString().
try {
if (
typeof error.actual == "object" &&
error.actual !== null &&
"QueryInterface" in error.actual
) {
throw new Error("don't attempt to stringify this XPCOM object");
}
actual = JSON.stringify(error.actual, replacer);
} catch (ex) {
actual = Object.prototype.toString.call(error.actual);
}
try {
if (
typeof error.expected == "object" &&
error.expected !== null &&
"QueryInterface" in error.expected
) {
throw new Error("don't attempt to stringify this XPCOM object");
}
expected = JSON.stringify(error.expected, replacer);
} catch (ex) {
expected = Object.prototype.toString.call(error.expected);
Expand Down

0 comments on commit 59e708a

Please sign in to comment.