diff options
author | Rich Trott <rtrott@gmail.com> | 2017-09-30 03:06:28 +0300 |
---|---|---|
committer | Evan Lucas <evanlucas@me.com> | 2017-10-23 17:21:39 +0300 |
commit | 0bd2fd562777ceb56574f5b2fb13524edda2149b (patch) | |
tree | 98c99bd8aac8e3df8a21edb8b8a3e7922c3e1dce /lib | |
parent | 0b048698600d20701ae0151441849b2a6993127b (diff) |
errors: make properties mutable
Userland code can break if it depends on a mutable `code` property for
errors. Allow users to change the `code` property but do not propagate
changes to the error `name`.
Additionally, make `message` and `name` consistent with `Error` object
(non-enumerable). Test that `console.log()` and `.toString()` calls on
internal `Error` objects with mutated properties have analogous results
with the standard ECMAScript `Error` objects.
Backport-PR-URL: https://github.com/nodejs/node/pull/16078
PR-URL: https://github.com/nodejs/node/pull/15694
Fixes: https://github.com/nodejs/node/issues/15658
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/internal/errors.js | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 180df6e9c20..cb1f87862da 100755 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -18,15 +18,13 @@ function makeNodeError(Base) { return class NodeError extends Base { constructor(key, ...args) { super(message(key, args)); - this[kCode] = key; - } - - get name() { - return `${super.name} [${this[kCode]}]`; - } - - get code() { - return this[kCode]; + this[kCode] = this.code = key; + Object.defineProperty(this, 'name', { + configurable: true, + enumerable: false, + value: `${super.name} [${this[kCode]}]`, + writable: true + }); } }; } |