GeezerVison is showing github.com — util: only inspect error properties that are not visible otherwise · nodejs/node@12fe985
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 +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -874,7 +874,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { | 874 | 874 | return ctx.stylize(base, 'date'); | 875 | 875 | } | 876 | 876 | } else if (isError(value)) { | 877 | | - base = formatError(value, constructor, tag, ctx); | | 877 | + base = formatError(value, constructor, tag, ctx, keys); | 878 | 878 | if (keys.length === 0 && protoProps === undefined) | 879 | 879 | return base; | 880 | 880 | } else if (isAnyArrayBuffer(value)) { | @@ -1064,11 +1064,23 @@ function getFunctionBase(value, constructor, tag) { | 1064 | 1064 | return base; | 1065 | 1065 | } | 1066 | 1066 |
| 1067 | | -function formatError(err, constructor, tag, ctx) { | | 1067 | +function formatError(err, constructor, tag, ctx, keys) { | 1068 | 1068 | const name = err.name != null ? String(err.name) : 'Error'; | 1069 | 1069 | let len = name.length; | 1070 | 1070 | let stack = err.stack ? String(err.stack) : ErrorPrototypeToString(err); | 1071 | 1071 |
| | 1072 | + // Do not "duplicate" error properties that are already included in the output | | 1073 | + // otherwise. | | 1074 | + if (!ctx.showHidden && keys.length !== 0) { | | 1075 | + for (const name of ['name', 'message', 'stack']) { | | 1076 | + const index = keys.indexOf(name); | | 1077 | + // Only hide the property in case it's part of the original stack | | 1078 | + if (index !== -1 && stack.includes(err[name])) { | | 1079 | + keys.splice(index, 1); | | 1080 | + } | | 1081 | + } | | 1082 | + } | | 1083 | + | 1072 | 1084 | // A stack trace may contain arbitrary data. Only manipulate the output | 1073 | 1085 | // for "regular errors" (errors that "look normal") for now. | 1074 | 1086 | if (constructor === null || | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -696,6 +696,28 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); | 696 | 696 | Error.stackTraceLimit = tmp; | 697 | 697 | } | 698 | 698 |
| | 699 | +// Prevent enumerable error properties from being printed. | | 700 | +{ | | 701 | + let err = new Error(); | | 702 | + err.message = 'foobar'; | | 703 | + let out = util.inspect(err).split('\n'); | | 704 | + assert.strictEqual(out[0], 'Error: foobar'); | | 705 | + assert(out[out.length - 1].startsWith(' at ')); | | 706 | + // Reset the error, the stack is otherwise not recreated. | | 707 | + err = new Error(); | | 708 | + err.message = 'foobar'; | | 709 | + err.name = 'Unique'; | | 710 | + Object.defineProperty(err, 'stack', { value: err.stack, enumerable: true }); | | 711 | + out = util.inspect(err).split('\n'); | | 712 | + assert.strictEqual(out[0], 'Unique: foobar'); | | 713 | + assert(out[out.length - 1].startsWith(' at ')); | | 714 | + err.name = 'Baz'; | | 715 | + out = util.inspect(err).split('\n'); | | 716 | + assert.strictEqual(out[0], 'Unique: foobar'); | | 717 | + assert.strictEqual(out[out.length - 2], " name: 'Baz'"); | | 718 | + assert.strictEqual(out[out.length - 1], '}'); | | 719 | +} | | 720 | + | 699 | 721 | // Doesn't capture stack trace. | 700 | 722 | { | 701 | 723 | function BadCustomError(msg) { | |
|