forked from indexeddbshim/IndexedDBShim
/
DOMException.js
287 lines (266 loc) · 8.95 KB
/
DOMException.js
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* globals DOMException */
import CFG from './CFG';
/**
* Creates a native DOMException, for browsers that support it
* @returns {DOMException}
*/
function createNativeDOMException (name, message) {
return new DOMException.prototype.constructor(message, name || 'DOMException');
}
const codes = { // From web-platform-tests testharness.js name_code_map (though not in new spec)
IndexSizeError: 1,
HierarchyRequestError: 3,
WrongDocumentError: 4,
InvalidCharacterError: 5,
NoModificationAllowedError: 7,
NotFoundError: 8,
NotSupportedError: 9,
InUseAttributeError: 10,
InvalidStateError: 11,
SyntaxError: 12,
InvalidModificationError: 13,
NamespaceError: 14,
InvalidAccessError: 15,
TypeMismatchError: 17,
SecurityError: 18,
NetworkError: 19,
AbortError: 20,
URLMismatchError: 21,
QuotaExceededError: 22,
TimeoutError: 23,
InvalidNodeTypeError: 24,
DataCloneError: 25,
EncodingError: 0,
NotReadableError: 0,
UnknownError: 0,
ConstraintError: 0,
DataError: 0,
TransactionInactiveError: 0,
ReadOnlyError: 0,
VersionError: 0,
OperationError: 0,
NotAllowedError: 0
};
const legacyCodes = {
INDEX_SIZE_ERR: 1,
DOMSTRING_SIZE_ERR: 2,
HIERARCHY_REQUEST_ERR: 3,
WRONG_DOCUMENT_ERR: 4,
INVALID_CHARACTER_ERR: 5,
NO_DATA_ALLOWED_ERR: 6,
NO_MODIFICATION_ALLOWED_ERR: 7,
NOT_FOUND_ERR: 8,
NOT_SUPPORTED_ERR: 9,
INUSE_ATTRIBUTE_ERR: 10,
INVALID_STATE_ERR: 11,
SYNTAX_ERR: 12,
INVALID_MODIFICATION_ERR: 13,
NAMESPACE_ERR: 14,
INVALID_ACCESS_ERR: 15,
VALIDATION_ERR: 16,
TYPE_MISMATCH_ERR: 17,
SECURITY_ERR: 18,
NETWORK_ERR: 19,
ABORT_ERR: 20,
URL_MISMATCH_ERR: 21,
QUOTA_EXCEEDED_ERR: 22,
TIMEOUT_ERR: 23,
INVALID_NODE_TYPE_ERR: 24,
DATA_CLONE_ERR: 25
};
function createNonNativeDOMExceptionClass () {
function DOMException (message, name) {
// const err = Error.prototype.constructor.call(this, message); // Any use to this? Won't set this.message
this[Symbol.toStringTag] = 'DOMException';
this._code = name in codes ? codes[name] : (legacyCodes[name] || 0);
this._name = name || 'Error';
this._message = message === undefined ? '' : ('' + message); // Not String() which converts Symbols
Object.defineProperty(this, 'code', {
configurable: true,
enumerable: true,
writable: true,
value: this._code
});
if (name !== undefined) {
Object.defineProperty(this, 'name', {
configurable: true,
enumerable: true,
writable: true,
value: this._name
});
}
if (message !== undefined) {
Object.defineProperty(this, 'message', {
configurable: true,
enumerable: false,
writable: true,
value: this._message
});
}
}
// Necessary for W3C tests which complains if `DOMException` has properties on its "own" prototype
// class DummyDOMException extends Error {}; // Sometimes causing problems in Node
const DummyDOMException = function DOMException () {};
DummyDOMException.prototype = Object.create(Error.prototype); // Intended for subclassing
['name', 'message'].forEach((prop) => {
Object.defineProperty(DummyDOMException.prototype, prop, {
enumerable: true,
get () {
if (!(this instanceof DOMException ||
this instanceof DummyDOMException ||
this instanceof Error)) {
throw new TypeError('Illegal invocation');
}
return this['_' + prop];
}
});
});
// DOMException uses the same `toString` as `Error`
Object.defineProperty(DummyDOMException.prototype, 'code', {
configurable: true,
enumerable: true,
get () {
throw new TypeError('Illegal invocation');
}
});
DOMException.prototype = new DummyDOMException();
DOMException.prototype[Symbol.toStringTag] = 'DOMExceptionPrototype';
Object.defineProperty(DOMException, 'prototype', {
writable: false
});
Object.keys(codes).forEach((codeName) => {
Object.defineProperty(DOMException.prototype, codeName, {
enumerable: true,
configurable: false,
value: codes[codeName]
});
Object.defineProperty(DOMException, codeName, {
enumerable: true,
configurable: false,
value: codes[codeName]
});
});
Object.keys(legacyCodes).forEach((codeName) => {
Object.defineProperty(DOMException.prototype, codeName, {
enumerable: true,
configurable: false,
value: legacyCodes[codeName]
});
Object.defineProperty(DOMException, codeName, {
enumerable: true,
configurable: false,
value: legacyCodes[codeName]
});
});
Object.defineProperty(DOMException.prototype, 'constructor', {
writable: true,
configurable: true,
enumerable: false,
value: DOMException
});
return DOMException;
}
const ShimNonNativeDOMException = createNonNativeDOMExceptionClass();
/**
* Creates a generic Error object
* @returns {Error}
*/
function createNonNativeDOMException (name, message) {
return new ShimNonNativeDOMException(message, name);
}
/**
* Logs detailed error information to the console.
* @param {string} name
* @param {string} message
* @param {string|Error|null} error
*/
function logError (name, message, error) {
if (CFG.DEBUG) {
if (error && error.message) {
error = error.message;
}
const method = typeof (console.error) === 'function' ? 'error' : 'log';
console[method](name + ': ' + message + '. ' + (error || ''));
console.trace && console.trace();
}
}
function isErrorOrDOMErrorOrDOMException (obj) {
return obj && typeof obj === 'object' && // We don't use util.isObj here as mutual dependency causing problems in Babel with browser
typeof obj.name === 'string';
}
/**
* Finds the error argument. This is useful because some WebSQL callbacks
* pass the error as the first argument, and some pass it as the second argument.
* @param {array} args
* @returns {Error|DOMException|undefined}
*/
function findError (args) {
let err;
if (args) {
if (args.length === 1) {
return args[0];
}
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (isErrorOrDOMErrorOrDOMException(arg)) {
return arg;
}
if (arg && typeof arg.message === 'string') {
err = arg;
}
}
}
return err;
}
function webSQLErrback (webSQLErr) {
let name, message;
switch (webSQLErr.code) {
case 4: { // SQLError.QUOTA_ERR
name = 'QuotaExceededError';
message = 'The operation failed because there was not enough remaining storage space, or the storage quota was reached and the user declined to give more space to the database.';
break;
}
/*
// Should a WebSQL timeout treat as IndexedDB `TransactionInactiveError` or `UnknownError`?
case 7: { // SQLError.TIMEOUT_ERR
// All transaction errors abort later, so no need to mark inactive
name = 'TransactionInactiveError';
message = 'A request was placed against a transaction which is currently not active, or which is finished (Internal SQL Timeout).';
break;
}
*/
default: {
name = 'UnknownError';
message = 'The operation failed for reasons unrelated to the database itself and not covered by any other errors.';
break;
}
}
message += ' (' + webSQLErr.message + ')--(' + webSQLErr.code + ')';
const err = createDOMException(name, message);
err.sqlError = webSQLErr;
return err;
}
let test, useNativeDOMException = false;
// Test whether we can use the browser's native DOMException class
try {
test = createNativeDOMException('test name', 'test message');
if (isErrorOrDOMErrorOrDOMException(test) && test.name === 'test name' && test.message === 'test message') {
// Native DOMException works as expected
useNativeDOMException = true;
}
} catch (e) {}
let createDOMException, ShimDOMException;
if (useNativeDOMException) {
ShimDOMException = DOMException;
createDOMException = function (name, message, error) {
logError(name, message, error);
return createNativeDOMException(name, message);
};
} else {
ShimDOMException = ShimNonNativeDOMException;
createDOMException = function (name, message, error) {
logError(name, message, error);
return createNonNativeDOMException(name, message);
};
}
export {logError, findError, ShimDOMException, createDOMException, webSQLErrback};