GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: inspect: do not crash on an Error stack that contains a Symbol · nodejs/node@a9d332a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/util
    • inspect.js
  • test/parallel
    • test-util-inspect.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,8 +1287,14 @@ function identicalSequenceRange(a, b) {
12871287
return { len: 0, offset: 0 };
12881288
}
12891289

1290-
function getStackString(error) {
1291-
return error.stack ? String(error.stack) : ErrorPrototypeToString(error);
1290+
function getStackString(ctx, error) {
1291+
if (error.stack) {
1292+
if (typeof error.stack === 'string') {
1293+
return error.stack;
1294+
}
1295+
return formatValue(ctx, error.stack);
1296+
}
1297+
return ErrorPrototypeToString(error);
12921298
}
12931299

12941300
function getStackFrames(ctx, err, stack) {
@@ -1303,7 +1309,7 @@ function getStackFrames(ctx, err, stack) {
13031309

13041310
// Remove stack frames identical to frames in cause.
13051311
if (cause != null && isError(cause)) {
1306-
const causeStack = getStackString(cause);
1312+
const causeStack = getStackString(ctx, cause);
13071313
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
13081314
if (causeStackStart !== -1) {
13091315
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
@@ -1426,7 +1432,7 @@ function safeGetCWD() {
14261432

14271433
function formatError(err, constructor, tag, ctx, keys) {
14281434
const name = err.name != null ? err.name : 'Error';
1429-
let stack = getStackString(err);
1435+
let stack = getStackString(ctx, err);
14301436

14311437
removeDuplicateErrorKeys(ctx, keys, err, stack);
14321438

Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,18 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
777777
[undefined, 'RangeError: foo', '[RangeError: foo]'],
778778
[false, 'false [RangeError]: foo', '[RangeError: foo]'],
779779
['', 'foo', '[RangeError: foo]'],
780-
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[1,2,3]'],
780+
[[1, 2, 3], '1,2,3 [RangeError]: foo', '[[\n 1,\n 2,\n 3\n]]'],
781781
].forEach(([value, outputStart, stack]) => {
782782
let err = new RangeError('foo');
783783
err.name = value;
784+
const result = util.inspect(err);
784785
assert(
785-
util.inspect(err).startsWith(outputStart),
786+
result.startsWith(outputStart),
786787
util.format(
787-
'The name set to %o did not result in the expected output "%s"',
788+
'The name set to %o did not result in the expected output "%s", got "%s"',
788789
value,
789-
outputStart
790+
outputStart,
791+
result.split('\n')[0]
790792
)
791793
);
792794

@@ -3416,3 +3418,13 @@ assert.strictEqual(
34163418
${error.stack.split('\n').slice(1).join('\n')}`,
34173419
);
34183420
}
3421+
3422+
{
3423+
const error = new Error();
3424+
error.stack = [Symbol('foo')];
3425+
3426+
assert.strictEqual(
3427+
inspect(error),
3428+
'[[\n Symbol(foo)\n]]'
3429+
);
3430+
}

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.