Skip to content

Commit 59855f2

Browse files
avivkellercjihrig
andcommitted
util: don't throw on circular toStringTag error
Co-Authored-By: Colin Ihrig <[email protected]>
1 parent 53b1050 commit 59855f2

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/internal/util/inspect.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
10721072
ArrayPrototypePushApply(output, protoProps);
10731073
}
10741074
} catch (err) {
1075+
if (!isStackOverflowError(err)) throw err;
10751076
const constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1);
10761077
return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl);
10771078
}
@@ -1557,17 +1558,13 @@ function groupArrayElements(ctx, output, value) {
15571558
}
15581559

15591560
function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
1560-
if (isStackOverflowError(err)) {
1561-
ctx.seen.pop();
1562-
ctx.indentationLvl = indentationLvl;
1563-
return ctx.stylize(
1564-
`[${constructorName}: Inspection interrupted ` +
1565-
'prematurely. Maximum call stack size exceeded.]',
1566-
'special',
1567-
);
1568-
}
1569-
/* c8 ignore next */
1570-
assert.fail(err.stack);
1561+
ctx.seen.pop();
1562+
ctx.indentationLvl = indentationLvl;
1563+
return ctx.stylize(
1564+
`[${constructorName}: Inspection interrupted ` +
1565+
'prematurely. Maximum call stack size exceeded.]',
1566+
'special',
1567+
);
15711568
}
15721569

15731570
function addNumericSeparator(integerString) {

test/parallel/test-util-inspect.js

+9
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,15 @@ util.inspect(process);
16441644

16451645
assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/);
16461646

1647+
const y = {
1648+
get [Symbol.toStringTag]() {
1649+
return JSON.stringify(this);
1650+
}
1651+
};
1652+
const x = { y };
1653+
y.x = x;
1654+
assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/);
1655+
16471656
class NotStringClass {
16481657
get [Symbol.toStringTag]() {
16491658
return null;

0 commit comments

Comments
 (0)