diff options
author | Ruben Bridgewater <ruben.bridgewater@maibornwolff.de> | 2020-03-17 19:24:25 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2020-04-02 19:41:57 +0300 |
commit | 6baddcfddf8dbaeac02c12efac07189919fbd8dd (patch) | |
tree | 01839cb815968faed6f3b3042460f5f8af644597 /lib/internal/util | |
parent | e20b4f918af8c0e3760252b79a0abe362b42f97c (diff) |
util: only inspect error properties that are not visible otherwise
Inspecting errors results in duplicated information in case an error
is created with enumerable `name`, `message` or `stack` properties.
In that case, check if the output already contains that information
and prevent listing that property.
This reduces the noise as receiver.
PR-URL: https://github.com/nodejs/node/pull/32327
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Diffstat (limited to 'lib/internal/util')
-rw-r--r-- | lib/internal/util/inspect.js | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index a39779838a4..e25b599e8cc 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -890,7 +890,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) { return ctx.stylize(base, 'date'); } } else if (isError(value)) { - base = formatError(value, constructor, tag, ctx); + base = formatError(value, constructor, tag, ctx, keys); if (keys.length === 0 && protoProps === undefined) return base; } else if (isAnyArrayBuffer(value)) { @@ -1083,11 +1083,23 @@ function getFunctionBase(value, constructor, tag) { return base; } -function formatError(err, constructor, tag, ctx) { +function formatError(err, constructor, tag, ctx, keys) { const name = err.name != null ? String(err.name) : 'Error'; let len = name.length; let stack = err.stack ? String(err.stack) : ErrorPrototypeToString(err); + // Do not "duplicate" error properties that are already included in the output + // otherwise. + if (!ctx.showHidden && keys.length !== 0) { + for (const name of ['name', 'message', 'stack']) { + const index = keys.indexOf(name); + // Only hide the property in case it's part of the original stack + if (index !== -1 && stack.includes(err[name])) { + keys.splice(index, 1); + } + } + } + // A stack trace may contain arbitrary data. Only manipulate the output // for "regular errors" (errors that "look normal") for now. if (constructor === null || |