From ccd9855c75409323b6d5551187517843fba8e86f Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Fri, 10 Jun 2022 19:24:04 -0700 Subject: [PATCH] Evade override mistake See https://github.com/Agoric/agoric-sdk/blob/master/patches/bl%2B%2Breadable-stream%2B3.6.0.patch The original code used assignment to override the `name` and `toString` properties inherited from `Error.prototype`. However, if `Error.prototype` is frozen, as it is under Hardened JS (aka SES) or under the Node frozen intrinsics flag, then this assignment fails due to the JavaScript "override mistake". `enumerable: true` would accurately preserve the behavior of the original assignment, but I'm guessing that was not intentional. For an actual error subclass, this property would not be enumerable, so my PR currently proposes that. But either would work, so let me know if you'd like me to change it. --- src/errors.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/errors.js b/src/errors.js index 84dc5c2c9..030467266 100644 --- a/src/errors.js +++ b/src/errors.js @@ -86,12 +86,22 @@ function E(code, message, Base) { } } - NodeError.prototype.name = Base.name + Object.defineProperties(NodeError.prototype, { + name: { + value: Base.name, + writable: true, + enumerable: false, + configurable: true, + }, + toString: { + value() { return `${this.name} [${code}]: ${this.message}`; }, + writable: true, + enumerable: false, + configurable: true, + } + }); NodeError.prototype.code = code NodeError.prototype[kIsNodeError] = true - NodeError.prototype.toString = function () { - return `${this.name} [${code}]: ${this.message}` - } codes[code] = NodeError }