diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 93301ca8ddfd45..0cd2bcdfab0028 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1186,7 +1186,14 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { protoProps = undefined; } - let tag = value[SymbolToStringTag]; + let tag = ''; + + try { + tag = value[SymbolToStringTag]; + } catch { + // Ignore error. + } + // Only list the tag in case it's non-enumerable / not an own property. // Otherwise we'd print this twice. if (typeof tag !== 'string' || diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index b26309bb66abd8..0faa2a0058e79d 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1646,7 +1646,7 @@ util.inspect(process); } } - assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/); + assert.strictEqual(util.inspect(new ThrowingClass()), 'ThrowingClass {}'); const y = { get [Symbol.toStringTag]() { @@ -1655,7 +1655,11 @@ util.inspect(process); }; const x = { y }; y.x = x; - assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/); + + assert.strictEqual( + util.inspect(x), + ' {\n y: { x: [Circular *1], Symbol(Symbol.toStringTag): [Getter] }\n}' + ); class NotStringClass { get [Symbol.toStringTag]() {