|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { internalBinding } = require('internal/bootstrap/loaders'); |
| 4 | +const { registerDOMException } = internalBinding('messaging'); |
| 5 | +const { ERR_INVALID_THIS } = require('internal/errors').codes; |
| 6 | + |
| 7 | +const internalsMap = new WeakMap(); |
| 8 | + |
| 9 | +const nameToCodeMap = new Map(); |
| 10 | + |
| 11 | +class DOMException extends Error { |
| 12 | + constructor(message = '', name = 'Error') { |
| 13 | + super(); |
| 14 | + internalsMap.set(this, { |
| 15 | + message: `${message}`, |
| 16 | + name: `${name}` |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + get name() { |
| 21 | + const internals = internalsMap.get(this); |
| 22 | + if (internals === undefined) { |
| 23 | + throw new ERR_INVALID_THIS('DOMException'); |
| 24 | + } |
| 25 | + return internals.name; |
| 26 | + } |
| 27 | + |
| 28 | + get message() { |
| 29 | + const internals = internalsMap.get(this); |
| 30 | + if (internals === undefined) { |
| 31 | + throw new ERR_INVALID_THIS('DOMException'); |
| 32 | + } |
| 33 | + return internals.message; |
| 34 | + } |
| 35 | + |
| 36 | + get code() { |
| 37 | + const internals = internalsMap.get(this); |
| 38 | + if (internals === undefined) { |
| 39 | + throw new ERR_INVALID_THIS('DOMException'); |
| 40 | + } |
| 41 | + const code = nameToCodeMap.get(internals.name); |
| 42 | + return code === undefined ? 0 : code; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +for (const [name, codeName, value] of [ |
| 47 | + ['IndexSizeError', 'INDEX_SIZE_ERR', 1], |
| 48 | + ['DOMStringSizeError', 'DOMSTRING_SIZE_ERR', 2], |
| 49 | + ['HierarchyRequestError', 'HIERARCHY_REQUEST_ERR', 3], |
| 50 | + ['WrongDocumentError', 'WRONG_DOCUMENT_ERR', 4], |
| 51 | + ['InvalidCharacterError', 'INVALID_CHARACTER_ERR', 5], |
| 52 | + ['NoDataAllowedError', 'NO_DATA_ALLOWED_ERR', 6], |
| 53 | + ['NoModificationAllowedError', 'NO_MODIFICATION_ALLOWED_ERR', 7], |
| 54 | + ['NotFoundError', 'NOT_FOUND_ERR', 8], |
| 55 | + ['NotSupportedError', 'NOT_SUPPORTED_ERR', 9], |
| 56 | + ['InUseAttributeError', 'INUSE_ATTRIBUTE_ERR', 10], |
| 57 | + ['InvalidStateError', 'INVALID_STATE_ERR', 11], |
| 58 | + ['SyntaxError', 'SYNTAX_ERR', 12], |
| 59 | + ['InvalidModificationError', 'INVALID_MODIFICATION_ERR', 13], |
| 60 | + ['NamespaceError', 'NAMESPACE_ERR', 14], |
| 61 | + ['InvalidAccessError', 'INVALID_ACCESS_ERR', 15], |
| 62 | + ['ValidationError', 'VALIDATION_ERR', 16], |
| 63 | + ['TypeMismatchError', 'TYPE_MISMATCH_ERR', 17], |
| 64 | + ['SecurityError', 'SECURITY_ERR', 18], |
| 65 | + ['NetworkError', 'NETWORK_ERR', 19], |
| 66 | + ['AbortError', 'ABORT_ERR', 20], |
| 67 | + ['URLMismatchError', 'URL_MISMATCH_ERR', 21], |
| 68 | + ['QuotaExceededError', 'QUOTA_EXCEEDED_ERR', 22], |
| 69 | + ['TimeoutError', 'TIMEOUT_ERR', 23], |
| 70 | + ['InvalidNodeTypeError', 'INVALID_NODE_TYPE_ERR', 24], |
| 71 | + ['DataCloneError', 'DATA_CLONE_ERR', 25] |
| 72 | + // There are some more error names, but since they don't have codes assigned, |
| 73 | + // we don't need to care about them. |
| 74 | +]) { |
| 75 | + const desc = { enumerable: true, value }; |
| 76 | + Object.defineProperty(DOMException, codeName, desc); |
| 77 | + Object.defineProperty(DOMException.prototype, codeName, desc); |
| 78 | + nameToCodeMap.set(name, value); |
| 79 | +} |
| 80 | + |
| 81 | +module.exports = DOMException; |
| 82 | + |
| 83 | +registerDOMException(DOMException); |
0 commit comments