Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,12 @@ function getStackString(ctx, error) {
if (typeof error.stack === 'string') {
return error.stack;
}
return formatValue(ctx, error.stack);
ctx.seen.push(error);
ctx.indentationLvl += 4;
const result = formatValue(ctx, error.stack);
ctx.indentationLvl -= 4;
ctx.seen.pop();
return `${ErrorPrototypeToString(error)}\n ${result}`;
}
return ErrorPrototypeToString(error);
}
Expand Down
34 changes: 31 additions & 3 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,14 +770,14 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
// Note: Symbols are not supported by `Error#toString()` which is called by
// accessing the `stack` property.
[
[404, '404 [RangeError]: foo', '[404]'],
[404, '404 [RangeError]: foo', '[RangeError: foo\n 404]'],
[0, '0 [RangeError]: foo', '[RangeError: foo]'],
[0n, '0 [RangeError]: foo', '[RangeError: foo]'],
[null, 'null: foo', '[RangeError: foo]'],
[undefined, 'RangeError: foo', '[RangeError: foo]'],
[false, 'false [RangeError]: foo', '[RangeError: foo]'],
['', 'foo', '[RangeError: foo]'],
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[[\n 1,\n 2,\n 3\n]]'],
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[RangeError: foo\n [\n 1,\n 2,\n 3\n ]]'],
].forEach(([value, outputStart, stack]) => {
let err = new RangeError('foo');
err.name = value;
Expand Down Expand Up @@ -3457,6 +3457,34 @@ ${error.stack.split('\n').slice(1).join('\n')}`,

assert.strictEqual(
inspect(error),
'[[\n Symbol(foo)\n]]'
'[Error\n [\n Symbol(foo)\n ]]'
);
}

{
const prepareStackTrace = Error.prepareStackTrace;

Error.prepareStackTrace = (error) => error;

const error = new Error('foo');

assert.strictEqual(inspect(error), '[Error: foo\n [Circular *1]]');

Error.prepareStackTrace = prepareStackTrace;
}

{
const error = new Error('foo');
error.stack = error;

assert.strictEqual(inspect(error), '[Error: foo\n [Circular *1]]');
}

{
const error = new Error('foo');
const error2 = new Error('bar');
error.stack = error2;
error2.stack = error;

assert.strictEqual(inspect(error), '[Error: foo\n [Error: bar\n [Circular *1]]]');
}
Loading