Debian Codegraph @

closure-library/closure/goog/db/error.js

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