JSFiddle - React, Tailwind, and code Playground
JavaScript
function CustomDOMException(code, message) {
//throw on missing code
if (typeof code !== "number") {
throw TypeError("Wrong argument");
}
//we need the codes, to get the "name" property.
var consts = {
1: "INDEX_SIZE_ERR",
3: "HIERARCHY_REQUEST_ERR",
4: "WRONG_DOCUMENT_ERR",
5: "INVALID_CHARACTER_ERR",
7: "NO_MODIFICATION_ALLOWED_ERR",
8: "NOT_FOUND_ERR",
9: "NOT_SUPPORTED_ERR",
11: "INVALID_STATE_ERR",
12: "SYNTAX_ERR",
13: "INVALID_MODIFICATION_ERR",
14: "NAMESPACE_ERR",
15: "INVALID_ACCESS_ERR",
17: "TYPE_MISMATCH_ERR",
18: "SECURITY_ERR",
19: "NETWORK_ERR",
20: "ABORT_ERR",
21: "URL_MISMATCH_ERR",
22: "QUOTA_EXCEEDED_ERR",
23: "TIMEOUT_ERR",
24: "INVALID_NODE_TYPE_ERR",
25: "DATA_CLONE_ERR"
}
if ((code in consts) === false) {
throw TypeError("Unknown exception code: " + code);
}
//props for adding properties
var props = {};
//generate an exception object
var newException;
try {
//force an exception to be generated;
document.removeChild({})
} catch (e) {
//use it as the prototype
newException = Object.create(e)
}
//get the name of the exception type
var name = consts[code];
//alias for the prototype, helps us populate the new object
var proto = newException.__proto__
//"shadow" the name property on the new object
props = Object.getOwnPropertyDescriptor(proto, "name");
props.value = name;
Object.defineProperty(newException, "name", props);
//"shadow" the code property on the new object
props = Object.getOwnPropertyDescriptor(proto, "code");
props.value = code;
Object.defineProperty(newException, "code", props);
//"shadow" the message property on the new object
props = Object.getOwnPropertyDescriptor(proto,...