diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index c10db2954db662..13012c6fce273f 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1062,6 +1062,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { ArrayPrototypePushApply(output, protoProps); } } catch (err) { + if (!isStackOverflowError(err)) throw err; const constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1); return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl); } @@ -1547,17 +1548,13 @@ function groupArrayElements(ctx, output, value) { } function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) { - if (isStackOverflowError(err)) { - ctx.seen.pop(); - ctx.indentationLvl = indentationLvl; - return ctx.stylize( - `[${constructorName}: Inspection interrupted ` + - 'prematurely. Maximum call stack size exceeded.]', - 'special', - ); - } - /* c8 ignore next */ - assert.fail(err.stack); + ctx.seen.pop(); + ctx.indentationLvl = indentationLvl; + return ctx.stylize( + `[${constructorName}: Inspection interrupted ` + + 'prematurely. Maximum call stack size exceeded.]', + 'special', + ); } function addNumericSeparator(integerString) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index b95d1c3784728b..3da292fc663c35 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -1644,6 +1644,15 @@ util.inspect(process); assert.throws(() => util.inspect(new ThrowingClass()), /toStringTag error/); + const y = { + get [Symbol.toStringTag]() { + return JSON.stringify(this); + } + }; + const x = { y }; + y.x = x; + assert.throws(() => util.inspect(x), /TypeError: Converting circular structure to JSON/); + class NotStringClass { get [Symbol.toStringTag]() { return null;