• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  Error,
5  ObjectDefineProperties,
6  ObjectDefineProperty,
7  SafeWeakMap,
8  SafeMap,
9  SymbolToStringTag,
10} = primordials;
11
12class ERR_INVALID_THIS extends TypeError {
13  constructor(type) {
14    super('Value of "this" must be of ' + type);
15  }
16
17  get code() { return 'ERR_INVALID_THIS'; }
18}
19
20let internalsMap;
21let nameToCodeMap;
22let isInitialized = false;
23
24// We need to instantiate the maps lazily because they render
25// the snapshot non-rehashable.
26// https://bugs.chromium.org/p/v8/issues/detail?id=6593
27function ensureInitialized() {
28  if (isInitialized) {
29    return;
30  }
31  internalsMap = new SafeWeakMap();
32  nameToCodeMap = new SafeMap();
33  forEachCode((name, codeName, value) => {
34    nameToCodeMap.set(name, value);
35  });
36  isInitialized = true;
37}
38
39class DOMException extends Error {
40  constructor(message = '', name = 'Error') {
41    ensureInitialized();
42    super();
43    internalsMap.set(this, {
44      message: `${message}`,
45      name: `${name}`
46    });
47  }
48
49  get name() {
50    ensureInitialized();
51    const internals = internalsMap.get(this);
52    if (internals === undefined) {
53      throw new ERR_INVALID_THIS('DOMException');
54    }
55    return internals.name;
56  }
57
58  get message() {
59    ensureInitialized();
60    const internals = internalsMap.get(this);
61    if (internals === undefined) {
62      throw new ERR_INVALID_THIS('DOMException');
63    }
64    return internals.message;
65  }
66
67  get code() {
68    ensureInitialized();
69    const internals = internalsMap.get(this);
70    if (internals === undefined) {
71      throw new ERR_INVALID_THIS('DOMException');
72    }
73    const code = nameToCodeMap.get(internals.name);
74    return code === undefined ? 0 : code;
75  }
76}
77
78ObjectDefineProperties(DOMException.prototype, {
79  [SymbolToStringTag]: { configurable: true, value: 'DOMException' },
80  name: { enumerable: true, configurable: true },
81  message: { enumerable: true, configurable: true },
82  code: { enumerable: true, configurable: true }
83});
84
85function forEachCode(fn) {
86  fn('IndexSizeError', 'INDEX_SIZE_ERR', 1);
87  fn('DOMStringSizeError', 'DOMSTRING_SIZE_ERR', 2);
88  fn('HierarchyRequestError', 'HIERARCHY_REQUEST_ERR', 3);
89  fn('WrongDocumentError', 'WRONG_DOCUMENT_ERR', 4);
90  fn('InvalidCharacterError', 'INVALID_CHARACTER_ERR', 5);
91  fn('NoDataAllowedError', 'NO_DATA_ALLOWED_ERR', 6);
92  fn('NoModificationAllowedError', 'NO_MODIFICATION_ALLOWED_ERR', 7);
93  fn('NotFoundError', 'NOT_FOUND_ERR', 8);
94  fn('NotSupportedError', 'NOT_SUPPORTED_ERR', 9);
95  fn('InUseAttributeError', 'INUSE_ATTRIBUTE_ERR', 10);
96  fn('InvalidStateError', 'INVALID_STATE_ERR', 11);
97  fn('SyntaxError', 'SYNTAX_ERR', 12);
98  fn('InvalidModificationError', 'INVALID_MODIFICATION_ERR', 13);
99  fn('NamespaceError', 'NAMESPACE_ERR', 14);
100  fn('InvalidAccessError', 'INVALID_ACCESS_ERR', 15);
101  fn('ValidationError', 'VALIDATION_ERR', 16);
102  fn('TypeMismatchError', 'TYPE_MISMATCH_ERR', 17);
103  fn('SecurityError', 'SECURITY_ERR', 18);
104  fn('NetworkError', 'NETWORK_ERR', 19);
105  fn('AbortError', 'ABORT_ERR', 20);
106  fn('URLMismatchError', 'URL_MISMATCH_ERR', 21);
107  fn('QuotaExceededError', 'QUOTA_EXCEEDED_ERR', 22);
108  fn('TimeoutError', 'TIMEOUT_ERR', 23);
109  fn('InvalidNodeTypeError', 'INVALID_NODE_TYPE_ERR', 24);
110  fn('DataCloneError', 'DATA_CLONE_ERR', 25);
111  // There are some more error names, but since they don't have codes assigned,
112  // we don't need to care about them.
113}
114
115forEachCode((name, codeName, value) => {
116  const desc = { enumerable: true, value };
117  ObjectDefineProperty(DOMException, codeName, desc);
118  ObjectDefineProperty(DOMException.prototype, codeName, desc);
119});
120
121exports.DOMException = DOMException;
122