Skip to content

Commit e2f33a0

Browse files
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 <[email protected]>
1 parent b1746a8 commit e2f33a0

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

config/eslint.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"Promise": true
1515
},
1616
"parserOptions": {
17-
"ecmaVersion": 5
17+
"ecmaVersion": 6
1818
},
1919
"extends": "eslint:recommended",
2020
"rules": {

src/util/minimal.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,30 @@ function newError(name) {
280280
merge(this, properties);
281281
}
282282

283-
(CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;
284-
285-
Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
286-
287-
CustomError.prototype.toString = function toString() {
288-
return this.name + ": " + this.message;
289-
};
283+
CustomError.prototype = Object.create(Error.prototype, {
284+
constructor: {
285+
value: CustomError,
286+
writable: true,
287+
enumerable: false,
288+
configurable: true,
289+
},
290+
name: {
291+
get() { return name; },
292+
set: undefined,
293+
enumerable: false,
294+
// configurable: false would accurately preserve the behavior of
295+
// the original, but I'm guessing that was not intentional.
296+
// For an actual error subclass, this property would
297+
// be configurable.
298+
configurable: true,
299+
},
300+
toString: {
301+
value() { return this.name + ": " + this.message; },
302+
writable: true,
303+
enumerable: false,
304+
configurable: true,
305+
},
306+
});
290307

291308
return CustomError;
292309
}

0 commit comments

Comments
 (0)