Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/db/error.js
2868 views
1
// Copyright 2011 The Closure Library Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS-IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/**
16
* @fileoverview Error classes for the IndexedDB wrapper.
17
*
18
*/
19
20
21
goog.provide('goog.db.DomErrorLike');
22
goog.provide('goog.db.Error');
23
goog.provide('goog.db.Error.ErrorCode');
24
goog.provide('goog.db.Error.ErrorName');
25
goog.provide('goog.db.Error.VersionChangeBlockedError');
26
27
goog.require('goog.debug.Error');
28
29
30
/** @record */
31
goog.db.DOMErrorLike = function() {};
32
33
/** @type {string|undefined} */
34
goog.db.DOMErrorLike.prototype.name;
35
36
/**
37
* A database error. Since the stack trace can be unhelpful in an asynchronous
38
* context, the error provides a message about where it was produced.
39
*
40
* @param {number|!DOMError|!goog.db.DOMErrorLike} error The DOMError instance
41
* returned by the browser for Chrome22+, or an error code for previous
42
* versions.
43
* @param {string} context A description of where the error occurred.
44
* @param {string=} opt_message Additional message.
45
* @constructor
46
* @extends {goog.debug.Error}
47
* @final
48
*/
49
goog.db.Error = function(error, context, opt_message) {
50
var errorCode = null;
51
var internalError = null;
52
if (goog.isNumber(error)) {
53
errorCode = error;
54
internalError = {name: goog.db.Error.getName(errorCode)};
55
} else {
56
internalError = error;
57
errorCode = goog.db.Error.getCode(error.name);
58
}
59
60
/**
61
* The code for this error.
62
*
63
* @type {number}
64
*/
65
this.code = errorCode;
66
67
/**
68
* The DOMException as returned by the browser.
69
*
70
* @type {!goog.db.DOMErrorLike}
71
* @private
72
*/
73
this.error_ = internalError;
74
75
var msg = 'Error ' + context + ': ' + this.getName();
76
if (opt_message) {
77
msg += ', ' + opt_message;
78
}
79
goog.db.Error.base(this, 'constructor', msg);
80
};
81
goog.inherits(goog.db.Error, goog.debug.Error);
82
83
84
/**
85
* @return {string} The name of the error.
86
*/
87
goog.db.Error.prototype.getName = function() {
88
return this.error_.name || '';
89
};
90
91
92
93
/**
94
* A specific kind of database error. If a Version Change is unable to proceed
95
* due to other open database connections, it will block and this error will be
96
* thrown.
97
*
98
* @constructor
99
* @extends {goog.debug.Error}
100
* @final
101
*/
102
goog.db.Error.VersionChangeBlockedError = function() {
103
goog.db.Error.VersionChangeBlockedError.base(
104
this, 'constructor', 'Version change blocked');
105
};
106
goog.inherits(goog.db.Error.VersionChangeBlockedError, goog.debug.Error);
107
108
109
/**
110
* Synthetic error codes for database errors, for use when IndexedDB
111
* support is not available. This numbering differs in practice
112
* from the browser implementations, but it is not meant to be reliable:
113
* this object merely ensures that goog.db.Error is loadable on platforms
114
* that do not support IndexedDB.
115
*
116
* @enum {number}
117
* @private
118
*/
119
goog.db.Error.DatabaseErrorCode_ = {
120
UNKNOWN_ERR: 1,
121
NON_TRANSIENT_ERR: 2,
122
NOT_FOUND_ERR: 3,
123
CONSTRAINT_ERR: 4,
124
DATA_ERR: 5,
125
NOT_ALLOWED_ERR: 6,
126
TRANSACTION_INACTIVE_ERR: 7,
127
ABORT_ERR: 8,
128
READ_ONLY_ERR: 9,
129
TRANSIENT_ERR: 10,
130
TIMEOUT_ERR: 11,
131
QUOTA_ERR: 12,
132
INVALID_ACCESS_ERR: 13,
133
INVALID_STATE_ERR: 14
134
};
135
136
137
/**
138
* Error codes for database errors.
139
* @see http://www.w3.org/TR/IndexedDB/#idl-def-IDBDatabaseException
140
*
141
* @enum {number}
142
*/
143
goog.db.Error.ErrorCode = {
144
UNKNOWN_ERR: (goog.global.IDBDatabaseException ||
145
goog.global.webkitIDBDatabaseException ||
146
goog.db.Error.DatabaseErrorCode_)
147
.UNKNOWN_ERR,
148
NON_TRANSIENT_ERR: (goog.global.IDBDatabaseException ||
149
goog.global.webkitIDBDatabaseException ||
150
goog.db.Error.DatabaseErrorCode_)
151
.NON_TRANSIENT_ERR,
152
NOT_FOUND_ERR: (goog.global.IDBDatabaseException ||
153
goog.global.webkitIDBDatabaseException ||
154
goog.db.Error.DatabaseErrorCode_)
155
.NOT_FOUND_ERR,
156
CONSTRAINT_ERR: (goog.global.IDBDatabaseException ||
157
goog.global.webkitIDBDatabaseException ||
158
goog.db.Error.DatabaseErrorCode_)
159
.CONSTRAINT_ERR,
160
DATA_ERR: (goog.global.IDBDatabaseException ||
161
goog.global.webkitIDBDatabaseException ||
162
goog.db.Error.DatabaseErrorCode_)
163
.DATA_ERR,
164
NOT_ALLOWED_ERR: (goog.global.IDBDatabaseException ||
165
goog.global.webkitIDBDatabaseException ||
166
goog.db.Error.DatabaseErrorCode_)
167
.NOT_ALLOWED_ERR,
168
TRANSACTION_INACTIVE_ERR: (goog.global.IDBDatabaseException ||
169
goog.global.webkitIDBDatabaseException ||
170
goog.db.Error.DatabaseErrorCode_)
171
.TRANSACTION_INACTIVE_ERR,
172
ABORT_ERR: (goog.global.IDBDatabaseException ||
173
goog.global.webkitIDBDatabaseException ||
174
goog.db.Error.DatabaseErrorCode_)
175
.ABORT_ERR,
176
READ_ONLY_ERR: (goog.global.IDBDatabaseException ||
177
goog.global.webkitIDBDatabaseException ||
178
goog.db.Error.DatabaseErrorCode_)
179
.READ_ONLY_ERR,
180
TIMEOUT_ERR: (goog.global.IDBDatabaseException ||
181
goog.global.webkitIDBDatabaseException ||
182
goog.db.Error.DatabaseErrorCode_)
183
.TIMEOUT_ERR,
184
QUOTA_ERR: (goog.global.IDBDatabaseException ||
185
goog.global.webkitIDBDatabaseException ||
186
goog.db.Error.DatabaseErrorCode_)
187
.QUOTA_ERR,
188
INVALID_ACCESS_ERR:
189
(goog.global.DOMException || goog.db.Error.DatabaseErrorCode_)
190
.INVALID_ACCESS_ERR,
191
INVALID_STATE_ERR:
192
(goog.global.DOMException || goog.db.Error.DatabaseErrorCode_)
193
.INVALID_STATE_ERR
194
};
195
196
197
/**
198
* Translates an error code into a more useful message.
199
*
200
* @param {number} code Error code.
201
* @return {string} A debug message.
202
*/
203
goog.db.Error.getMessage = function(code) {
204
switch (code) {
205
case goog.db.Error.ErrorCode.UNKNOWN_ERR:
206
return 'Unknown error';
207
case goog.db.Error.ErrorCode.NON_TRANSIENT_ERR:
208
return 'Invalid operation';
209
case goog.db.Error.ErrorCode.NOT_FOUND_ERR:
210
return 'Required database object not found';
211
case goog.db.Error.ErrorCode.CONSTRAINT_ERR:
212
return 'Constraint unsatisfied';
213
case goog.db.Error.ErrorCode.DATA_ERR:
214
return 'Invalid data';
215
case goog.db.Error.ErrorCode.NOT_ALLOWED_ERR:
216
return 'Operation disallowed';
217
case goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR:
218
return 'Transaction not active';
219
case goog.db.Error.ErrorCode.ABORT_ERR:
220
return 'Request aborted';
221
case goog.db.Error.ErrorCode.READ_ONLY_ERR:
222
return 'Modifying operation not allowed in a read-only transaction';
223
case goog.db.Error.ErrorCode.TIMEOUT_ERR:
224
return 'Transaction timed out';
225
case goog.db.Error.ErrorCode.QUOTA_ERR:
226
return 'Database storage space quota exceeded';
227
case goog.db.Error.ErrorCode.INVALID_ACCESS_ERR:
228
return 'Invalid operation';
229
case goog.db.Error.ErrorCode.INVALID_STATE_ERR:
230
return 'Invalid state';
231
default:
232
return 'Unrecognized exception with code ' + code;
233
}
234
};
235
236
237
/**
238
* Names of all possible errors as returned from the browser.
239
* @see http://www.w3.org/TR/IndexedDB/#exceptions
240
* @enum {string}
241
*/
242
goog.db.Error.ErrorName = {
243
ABORT_ERR: 'AbortError',
244
CONSTRAINT_ERR: 'ConstraintError',
245
DATA_CLONE_ERR: 'DataCloneError',
246
DATA_ERR: 'DataError',
247
INVALID_ACCESS_ERR: 'InvalidAccessError',
248
INVALID_STATE_ERR: 'InvalidStateError',
249
NOT_FOUND_ERR: 'NotFoundError',
250
QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
251
READ_ONLY_ERR: 'ReadOnlyError',
252
SYNTAX_ERROR: 'SyntaxError',
253
TIMEOUT_ERR: 'TimeoutError',
254
TRANSACTION_INACTIVE_ERR: 'TransactionInactiveError',
255
UNKNOWN_ERR: 'UnknownError',
256
VERSION_ERR: 'VersionError'
257
};
258
259
260
/**
261
* Translates an error name to an error code. This is purely kept for backwards
262
* compatibility with Chrome21.
263
*
264
* @param {string|undefined} name The name of the erorr.
265
* @return {number} The error code corresponding to the error.
266
*/
267
goog.db.Error.getCode = function(name) {
268
switch (name) {
269
case goog.db.Error.ErrorName.UNKNOWN_ERR:
270
return goog.db.Error.ErrorCode.UNKNOWN_ERR;
271
case goog.db.Error.ErrorName.NOT_FOUND_ERR:
272
return goog.db.Error.ErrorCode.NOT_FOUND_ERR;
273
case goog.db.Error.ErrorName.CONSTRAINT_ERR:
274
return goog.db.Error.ErrorCode.CONSTRAINT_ERR;
275
case goog.db.Error.ErrorName.DATA_ERR:
276
return goog.db.Error.ErrorCode.DATA_ERR;
277
case goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR:
278
return goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR;
279
case goog.db.Error.ErrorName.ABORT_ERR:
280
return goog.db.Error.ErrorCode.ABORT_ERR;
281
case goog.db.Error.ErrorName.READ_ONLY_ERR:
282
return goog.db.Error.ErrorCode.READ_ONLY_ERR;
283
case goog.db.Error.ErrorName.TIMEOUT_ERR:
284
return goog.db.Error.ErrorCode.TIMEOUT_ERR;
285
case goog.db.Error.ErrorName.QUOTA_EXCEEDED_ERR:
286
return goog.db.Error.ErrorCode.QUOTA_ERR;
287
case goog.db.Error.ErrorName.INVALID_ACCESS_ERR:
288
return goog.db.Error.ErrorCode.INVALID_ACCESS_ERR;
289
case goog.db.Error.ErrorName.INVALID_STATE_ERR:
290
return goog.db.Error.ErrorCode.INVALID_STATE_ERR;
291
default:
292
return goog.db.Error.ErrorCode.UNKNOWN_ERR;
293
}
294
};
295
296
297
/**
298
* Converts an error code used by the old spec, to an error name used by the
299
* latest spec.
300
* @see http://www.w3.org/TR/IndexedDB/#exceptions
301
*
302
* @param {!goog.db.Error.ErrorCode|number} code The error code to convert.
303
* @return {!goog.db.Error.ErrorName} The corresponding name of the error.
304
*/
305
goog.db.Error.getName = function(code) {
306
switch (code) {
307
case goog.db.Error.ErrorCode.UNKNOWN_ERR:
308
return goog.db.Error.ErrorName.UNKNOWN_ERR;
309
case goog.db.Error.ErrorCode.NOT_FOUND_ERR:
310
return goog.db.Error.ErrorName.NOT_FOUND_ERR;
311
case goog.db.Error.ErrorCode.CONSTRAINT_ERR:
312
return goog.db.Error.ErrorName.CONSTRAINT_ERR;
313
case goog.db.Error.ErrorCode.DATA_ERR:
314
return goog.db.Error.ErrorName.DATA_ERR;
315
case goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR:
316
return goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR;
317
case goog.db.Error.ErrorCode.ABORT_ERR:
318
return goog.db.Error.ErrorName.ABORT_ERR;
319
case goog.db.Error.ErrorCode.READ_ONLY_ERR:
320
return goog.db.Error.ErrorName.READ_ONLY_ERR;
321
case goog.db.Error.ErrorCode.TIMEOUT_ERR:
322
return goog.db.Error.ErrorName.TIMEOUT_ERR;
323
case goog.db.Error.ErrorCode.QUOTA_ERR:
324
return goog.db.Error.ErrorName.QUOTA_EXCEEDED_ERR;
325
case goog.db.Error.ErrorCode.INVALID_ACCESS_ERR:
326
return goog.db.Error.ErrorName.INVALID_ACCESS_ERR;
327
case goog.db.Error.ErrorCode.INVALID_STATE_ERR:
328
return goog.db.Error.ErrorName.INVALID_STATE_ERR;
329
default:
330
return goog.db.Error.ErrorName.UNKNOWN_ERR;
331
}
332
};
333
334
335
/**
336
* Constructs an goog.db.Error instance from an IDBRequest. This abstraction is
337
* necessary to provide backwards compatibility with Chrome21.
338
*
339
* @param {!IDBRequest} request The request that failed.
340
* @param {string} message The error message to add to err if it's wrapped.
341
* @return {!goog.db.Error} The error that caused the failure.
342
*/
343
goog.db.Error.fromRequest = function(request, message) {
344
if ('error' in request) {
345
// Chrome 21 and before.
346
return new goog.db.Error(request.error, message);
347
} else if ('name' in request) {
348
// Chrome 22+.
349
var errorName = goog.db.Error.getName(request.error.severity);
350
return new goog.db.Error({name: errorName}, message);
351
} else {
352
return new goog.db.Error(
353
{name: goog.db.Error.ErrorName.UNKNOWN_ERR}, message);
354
}
355
};
356
357
358
/**
359
* Constructs an goog.db.Error instance from an DOMException. This abstraction
360
* is necessary to provide backwards compatibility with Chrome21.
361
*
362
* @param {!DOMError|!DOMException} ex The exception that was thrown.
363
* @param {string} message The error message to add to err if it's wrapped.
364
* @return {!goog.db.Error} The error that caused the failure.
365
*/
366
goog.db.Error.fromException = function(ex, message) {
367
if ('name' in ex) {
368
// Chrome 22+.
369
var errorMessage = message + ': ' + ex.message;
370
return new goog.db.Error(ex, errorMessage);
371
} else if ('code' in ex) {
372
// Chrome 21 and before.
373
var errorName = goog.db.Error.getName(ex.code);
374
var errorMessage = message + ': ' + ex.message;
375
return new goog.db.Error({name: errorName}, errorMessage);
376
} else {
377
return new goog.db.Error(
378
{name: goog.db.Error.ErrorName.UNKNOWN_ERR}, message);
379
}
380
};
381
382