From e2f33a04eddbcffcbc2ae16c72ac3820410057bc Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 7 Jul 2022 10:47:55 -0700 Subject: [PATCH] fix: update minimal.js to evade override mistake (#1742) * Update minimal.js See https://github.com/Agoric/agoric-sdk/blob/master/patches/%40confio%2Bics23%2B%2Bprotobufjs%2B6.11.3.patch The original code used assignment to override the `constructor` 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. `configurable: false` would accurately preserve the behavior of the original, but I'm guessing that was not intentional. For an actual error subclass, this property would be configurable. But either would work, so let me know if you'd like me to change it. * chore: use ecmaVersion=6 for eslint Co-authored-by: Alexander Fenster --- config/eslint.json | 2 +- src/util/minimal.js | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/config/eslint.json b/config/eslint.json index 2800db370..b87a6eb4e 100644 --- a/config/eslint.json +++ b/config/eslint.json @@ -14,7 +14,7 @@ "Promise": true }, "parserOptions": { - "ecmaVersion": 5 + "ecmaVersion": 6 }, "extends": "eslint:recommended", "rules": { diff --git a/src/util/minimal.js b/src/util/minimal.js index 3c406dee7..35008ecc4 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -280,13 +280,30 @@ function newError(name) { merge(this, properties); } - (CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError; - - Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } }); - - CustomError.prototype.toString = function toString() { - return this.name + ": " + this.message; - }; + CustomError.prototype = Object.create(Error.prototype, { + constructor: { + value: CustomError, + writable: true, + enumerable: false, + configurable: true, + }, + name: { + get() { return name; }, + set: undefined, + enumerable: false, + // configurable: false would accurately preserve the behavior of + // the original, but I'm guessing that was not intentional. + // For an actual error subclass, this property would + // be configurable. + configurable: true, + }, + toString: { + value() { return this.name + ": " + this.message; }, + writable: true, + enumerable: false, + configurable: true, + }, + }); return CustomError; }