Skip to content

Commit 3c62b38

Browse files
BridgeARtargos
authored andcommitted
util: inspect objects with throwing Symbol.toStringTag
`util.inspect()` should handle all kinds of input, even if it is not well defined. Throwing is something that is meant to be worked around in all known cases. This fixes issues inspecting objects where accessing the Symbol.toStringTag would cause an error. The symbol is just ignored in that case. Refs: #55539 Refs: #55544 PR-URL: #59860 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6133a82 commit 3c62b38

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/internal/util/inspect.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,14 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
935935
protoProps = undefined;
936936
}
937937

938-
let tag = value[SymbolToStringTag];
938+
let tag = '';
939+
940+
try {
941+
tag = value[SymbolToStringTag];
942+
} catch {
943+
// Ignore error.
944+
}
945+
939946
// Only list the tag in case it's non-enumerable / not an own property.
940947
// Otherwise we'd print this twice.
941948
if (typeof tag !== 'string' ||

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ util.inspect(process);
16461646
}
16471647
}
16481648

1649-
assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/);
1649+
assert.strictEqual(util.inspect(new ThrowingClass()), 'ThrowingClass {}');
16501650

16511651
const y = {
16521652
get [Symbol.toStringTag]() {
@@ -1655,7 +1655,11 @@ util.inspect(process);
16551655
};
16561656
const x = { y };
16571657
y.x = x;
1658-
assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/);
1658+
1659+
assert.strictEqual(
1660+
util.inspect(x),
1661+
'<ref *1> {\n y: { x: [Circular *1], Symbol(Symbol.toStringTag): [Getter] }\n}'
1662+
);
16591663

16601664
class NotStringClass {
16611665
get [Symbol.toStringTag]() {

0 commit comments

Comments
 (0)