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