GeezerVison is showing github.com — util: inspect: do not crash on an Error stack that contains a Symbol · nodejs/node@a9d332a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1287,8 +1287,14 @@ function identicalSequenceRange(a, b) { | 1287 | 1287 | return { len: 0, offset: 0 }; | 1288 | 1288 | } | 1289 | 1289 |
| 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); | 1292 | 1298 | } | 1293 | 1299 |
| 1294 | 1300 | function getStackFrames(ctx, err, stack) { | @@ -1303,7 +1309,7 @@ function getStackFrames(ctx, err, stack) { | 1303 | 1309 |
| 1304 | 1310 | // Remove stack frames identical to frames in cause. | 1305 | 1311 | if (cause != null && isError(cause)) { | 1306 | | - const causeStack = getStackString(cause); | | 1312 | + const causeStack = getStackString(ctx, cause); | 1307 | 1313 | const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at'); | 1308 | 1314 | if (causeStackStart !== -1) { | 1309 | 1315 | const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n'); | @@ -1426,7 +1432,7 @@ function safeGetCWD() { | 1426 | 1432 |
| 1427 | 1433 | function formatError(err, constructor, tag, ctx, keys) { | 1428 | 1434 | const name = err.name != null ? err.name : 'Error'; | 1429 | | - let stack = getStackString(err); | | 1435 | + let stack = getStackString(ctx, err); | 1430 | 1436 |
| 1431 | 1437 | removeDuplicateErrorKeys(ctx, keys, err, stack); | 1432 | 1438 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +16-4Lines changed: 16 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -777,16 +777,18 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); | 777 | 777 | [undefined, 'RangeError: foo', '[RangeError: foo]'], | 778 | 778 | [false, 'false [RangeError]: foo', '[RangeError: foo]'], | 779 | 779 | ['', '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]]'], | 781 | 781 | ].forEach(([value, outputStart, stack]) => { | 782 | 782 | let err = new RangeError('foo'); | 783 | 783 | err.name = value; | | 784 | + const result = util.inspect(err); | 784 | 785 | assert( | 785 | | - util.inspect(err).startsWith(outputStart), | | 786 | + result.startsWith(outputStart), | 786 | 787 | 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"', | 788 | 789 | value, | 789 | | - outputStart | | 790 | + outputStart, | | 791 | + result.split('\n')[0] | 790 | 792 | ) | 791 | 793 | ); | 792 | 794 |
| @@ -3416,3 +3418,13 @@ assert.strictEqual( | 3416 | 3418 | ${error.stack.split('\n').slice(1).join('\n')}`, | 3417 | 3419 | ); | 3418 | 3420 | } | | 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 | +} |
|