From 59e708aa2978dc910f20043a16f32b1b1a33a9cf Mon Sep 17 00:00:00 2001 From: Geoff Lankow Date: Wed, 7 Feb 2024 21:46:20 +0000 Subject: [PATCH] Bug 1879018 - Use replacer function to avoid calling JSON.stringify on 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 --- testing/modules/Assert.sys.mjs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/testing/modules/Assert.sys.mjs b/testing/modules/Assert.sys.mjs index 75a5d7e64cf0..47e2d3700d39 100644 --- a/testing/modules/Assert.sys.mjs +++ b/testing/modules/Assert.sys.mjs @@ -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; } @@ -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);