Skip to content

Commit 939b7d0

Browse files
BridgeARaduh95
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 <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b038f8b commit 939b7d0

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
@@ -945,7 +945,14 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
945945
protoProps = undefined;
946946
}
947947

948-
let tag = value[SymbolToStringTag];
948+
let tag = '';
949+
950+
try {
951+
tag = value[SymbolToStringTag];
952+
} catch {
953+
// Ignore error.
954+
}
955+
949956
// Only list the tag in case it's non-enumerable / not an own property.
950957
// Otherwise we'd print this twice.
951958
if (typeof tag !== 'string' ||

test/parallel/test-util-inspect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,7 @@ util.inspect(process);
16671667
}
16681668
}
16691669

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

16721672
const y = {
16731673
get [Symbol.toStringTag]() {
@@ -1676,7 +1676,11 @@ util.inspect(process);
16761676
};
16771677
const x = { y };
16781678
y.x = x;
1679-
assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/);
1679+
1680+
assert.strictEqual(
1681+
util.inspect(x),
1682+
'<ref *1> {\n y: { x: [Circular *1], Symbol(Symbol.toStringTag): [Getter] }\n}'
1683+
);
16801684

16811685
class NotStringClass {
16821686
get [Symbol.toStringTag]() {

0 commit comments

Comments
 (0)