Skip to content

Commit 60c1fbc

Browse files
authored
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 0319ba5 commit 60c1fbc

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
@@ -1186,7 +1186,14 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
11861186
protoProps = undefined;
11871187
}
11881188

1189-
let tag = value[SymbolToStringTag];
1189+
let tag = '';
1190+
1191+
try {
1192+
tag = value[SymbolToStringTag];
1193+
} catch {
1194+
// Ignore error.
1195+
}
1196+
11901197
// Only list the tag in case it's non-enumerable / not an own property.
11911198
// Otherwise we'd print this twice.
11921199
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)