Skip to content

Commit

Permalink
fix: update minimal.js to evade override mistake (#1742)
Browse files Browse the repository at this point in the history
* 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 <fenster@google.com>
  • Loading branch information
erights and alexander-fenster committed Jul 7, 2022
1 parent b1746a8 commit e2f33a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/eslint.json
Expand Up @@ -14,7 +14,7 @@
"Promise": true
},
"parserOptions": {
"ecmaVersion": 5
"ecmaVersion": 6
},
"extends": "eslint:recommended",
"rules": {
Expand Down
31 changes: 24 additions & 7 deletions src/util/minimal.js
Expand Up @@ -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;
}
Expand Down

0 comments on commit e2f33a0

Please sign in to comment.