-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy path01_dom_exception.js
More file actions
246 lines (225 loc) · 6.89 KB
/
01_dom_exception.js
File metadata and controls
246 lines (225 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2018-2026 the Deno authors. MIT license.
// @ts-check
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../webidl/internal.d.ts" />
/// <reference path="../web/internal.d.ts" />
/// <reference path="../../cli/tsc/dts/lib.deno_web.d.ts" />
import { core, primordials } from "ext:core/mod.js";
const {
Error,
ErrorPrototype,
ObjectDefineProperty,
ObjectCreate,
ObjectEntries,
ObjectHasOwn,
ObjectPrototypeIsPrototypeOf,
ObjectSetPrototypeOf,
ReflectConstruct,
ReflectHas,
Symbol,
SymbolFor,
} = primordials;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "./01_console.js";
const _name = Symbol("name");
const _message = Symbol("message");
const _code = Symbol("code");
// Defined in WebIDL 4.3.
// https://webidl.spec.whatwg.org/#idl-DOMException
const INDEX_SIZE_ERR = 1;
const DOMSTRING_SIZE_ERR = 2;
const HIERARCHY_REQUEST_ERR = 3;
const WRONG_DOCUMENT_ERR = 4;
const INVALID_CHARACTER_ERR = 5;
const NO_DATA_ALLOWED_ERR = 6;
const NO_MODIFICATION_ALLOWED_ERR = 7;
const NOT_FOUND_ERR = 8;
const NOT_SUPPORTED_ERR = 9;
const INUSE_ATTRIBUTE_ERR = 10;
const INVALID_STATE_ERR = 11;
const SYNTAX_ERR = 12;
const INVALID_MODIFICATION_ERR = 13;
const NAMESPACE_ERR = 14;
const INVALID_ACCESS_ERR = 15;
const VALIDATION_ERR = 16;
const TYPE_MISMATCH_ERR = 17;
const SECURITY_ERR = 18;
const NETWORK_ERR = 19;
const ABORT_ERR = 20;
const URL_MISMATCH_ERR = 21;
const QUOTA_EXCEEDED_ERR = 22;
const TIMEOUT_ERR = 23;
const INVALID_NODE_TYPE_ERR = 24;
const DATA_CLONE_ERR = 25;
// Defined in WebIDL 2.8.1.
// https://webidl.spec.whatwg.org/#dfn-error-names-table
/** @type {Record<string, number>} */
// the prototype should be null, to prevent user code from looking
// up Object.prototype properties, such as "toString"
const nameToCodeMapping = ObjectCreate(null, {
IndexSizeError: { value: INDEX_SIZE_ERR },
HierarchyRequestError: { value: HIERARCHY_REQUEST_ERR },
WrongDocumentError: { value: WRONG_DOCUMENT_ERR },
InvalidCharacterError: { value: INVALID_CHARACTER_ERR },
NoModificationAllowedError: { value: NO_MODIFICATION_ALLOWED_ERR },
NotFoundError: { value: NOT_FOUND_ERR },
NotSupportedError: { value: NOT_SUPPORTED_ERR },
InUseAttributeError: { value: INUSE_ATTRIBUTE_ERR },
InvalidStateError: { value: INVALID_STATE_ERR },
SyntaxError: { value: SYNTAX_ERR },
InvalidModificationError: { value: INVALID_MODIFICATION_ERR },
NamespaceError: { value: NAMESPACE_ERR },
InvalidAccessError: { value: INVALID_ACCESS_ERR },
TypeMismatchError: { value: TYPE_MISMATCH_ERR },
SecurityError: { value: SECURITY_ERR },
NetworkError: { value: NETWORK_ERR },
AbortError: { value: ABORT_ERR },
URLMismatchError: { value: URL_MISMATCH_ERR },
QuotaExceededError: { value: QUOTA_EXCEEDED_ERR },
TimeoutError: { value: TIMEOUT_ERR },
InvalidNodeTypeError: { value: INVALID_NODE_TYPE_ERR },
DataCloneError: { value: DATA_CLONE_ERR },
});
// Defined in WebIDL 4.3.
// https://webidl.spec.whatwg.org/#idl-DOMException
class DOMException {
[_message];
[_name];
[_code];
// https://webidl.spec.whatwg.org/#dom-domexception-domexception
// Since Node.js allows the second argument to accept an options object, we need to support that
// https://github.com/nodejs/node/blob/9e201e61fd8e4b8bfb74409151cbcbbc7377ca67/lib/internal/per_context/domexception.js#L82-L96
constructor(message = "", options = "Error") {
message = webidl.converters.DOMString(
message,
"Failed to construct 'DOMException'",
"Argument 1",
);
// execute Error constructor to have stack property and [[ErrorData]] internal slot
const error = ReflectConstruct(Error, [], new.target);
let name;
if (options !== null && typeof options === "object") {
name = webidl.converters.DOMString(
options.name,
"Failed to construct 'DOMException'",
"Argument 2",
);
if (ReflectHas(options, "cause")) {
ObjectDefineProperty(error, "cause", {
__proto__: null,
value: options.cause,
configurable: true,
writable: true,
enumerable: false,
});
}
} else {
name = webidl.converters.DOMString(
options,
"Failed to construct 'DOMException'",
"Argument 2",
);
}
const code = nameToCodeMapping[name] ?? 0;
error[_message] = message;
error[_name] = name;
error[_code] = code;
error[webidl.brand] = webidl.brand;
ObjectDefineProperty(error, core.hostObjectBrand, {
__proto__: null,
value: () => ({
type: "DOMException",
message,
name,
stack: error.stack,
}),
enumerable: false,
configurable: false,
writable: false,
});
return error;
}
get message() {
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_message];
}
get name() {
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_name];
}
get code() {
webidl.assertBranded(this, DOMExceptionPrototype);
return this[_code];
}
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
if (ObjectHasOwn(this, "stack")) {
const stack = this.stack;
if (typeof stack === "string") {
return stack;
}
}
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(DOMExceptionPrototype, this),
keys: [
"message",
"name",
"code",
],
}),
inspectOptions,
);
}
}
ObjectSetPrototypeOf(DOMException.prototype, ErrorPrototype);
webidl.configureInterface(DOMException);
const DOMExceptionPrototype = DOMException.prototype;
const entries = ObjectEntries({
INDEX_SIZE_ERR,
DOMSTRING_SIZE_ERR,
HIERARCHY_REQUEST_ERR,
WRONG_DOCUMENT_ERR,
INVALID_CHARACTER_ERR,
NO_DATA_ALLOWED_ERR,
NO_MODIFICATION_ALLOWED_ERR,
NOT_FOUND_ERR,
NOT_SUPPORTED_ERR,
INUSE_ATTRIBUTE_ERR,
INVALID_STATE_ERR,
SYNTAX_ERR,
INVALID_MODIFICATION_ERR,
NAMESPACE_ERR,
INVALID_ACCESS_ERR,
VALIDATION_ERR,
TYPE_MISMATCH_ERR,
SECURITY_ERR,
NETWORK_ERR,
ABORT_ERR,
URL_MISMATCH_ERR,
QUOTA_EXCEEDED_ERR,
TIMEOUT_ERR,
INVALID_NODE_TYPE_ERR,
DATA_CLONE_ERR,
});
for (let i = 0; i < entries.length; ++i) {
const { 0: key, 1: value } = entries[i];
const desc = { __proto__: null, value, enumerable: true };
ObjectDefineProperty(DOMException, key, desc);
ObjectDefineProperty(DOMException.prototype, key, desc);
}
core.registerCloneableResource("DOMException", (data) => {
const ex = new DOMException(data.message, data.name);
if (data.stack !== undefined) {
ObjectDefineProperty(ex, "stack", {
__proto__: null,
value: data.stack,
configurable: true,
writable: true,
enumerable: false,
});
}
return ex;
});
export { DOMException, DOMExceptionPrototype };