Created
September 29, 2017 20:22
-
-
Save domenic/a4c438614eebd0f04679ee222a884948 to your computer and use it in GitHub Desktop.
OverconstrainedError in V8 extras
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global, binding, v8) { | |
const captureStackTrace = global.Error.captureStackTrace; | |
const defineProperty = global.Object.defineProperty; | |
const setPrototypeOf = global.Object.setPrototypeOf; | |
const Error = global.Error; | |
function OverconstrainedError(constraint, message = undefined) { | |
const obj = new Error(); | |
// This complicated dance is necessary to emulate | |
// https://tc39.github.io/ecma262/#sec-ordinarycreatefromconstructor | |
// because we can't just use `class extends Error {}` as that will not | |
// allow `OverconstrainedError` to be used without `new`. | |
let targetProto = new.target ? new.target.prototype : oceProto; | |
if (targetProto === null || (typeof targetProto !== "object" && typeof targetProto !== "function")) { | |
targetProto = oceProto; | |
} | |
setPrototypeOf(obj, targetProto); | |
captureStackTrace(obj, OverconstrainedError); | |
if (constraint !== undefined) { | |
defineProperty(obj, "constraint", { | |
value: constraint, | |
enumerable: false, | |
writable: false, | |
configurable: false | |
}); | |
} | |
if (message !== undefined) { | |
message = `${message}`; | |
defineProperty(obj, "message", { | |
value: constraint, | |
enumerable: false, | |
writable: true, | |
configurable: true | |
}); | |
} | |
return obj; | |
} | |
const oceProto = OverconstrainedError.prototype; | |
oceProto.name = "OverconstrainedError"; | |
oceProto.message = ""; | |
setPrototypeOf(oceProto, Error.prototype); | |
setPrototypeOf(OverconstrainedError, Error); | |
binding.OverconstrainedError = OverconstrainedError; | |
defineProperty(global, "OverconstrainedError", { | |
value: OverconstrainedError, | |
enumerable: false, | |
writable: true, | |
configurable: true | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment