PageRenderTime 41ms CodeModel.GetById 28ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/mczolko/dependency-injection-container-for-google-closure
JavaScript | 195 lines | 106 code | 19 blank | 70 comment | 3 complexity | b6d1cf15bf9cd7fba59436fd82d92f6d MD5 | raw file
  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
 21goog.provide('goog.db.Error');
 22goog.provide('goog.db.Error.ErrorCode');
 23goog.provide('goog.db.Error.VersionChangeBlockedError');
 24
 25goog.require('goog.debug.Error');
 26
 27
 28
 29/**
 30 * A database error. Since the stack trace can be unhelpful in an asynchronous
 31 * context, the error provides a message about where it was produced.
 32 *
 33 * @param {number} code The error code.
 34 * @param {string} context A description of where the error occured.
 35 * @param {string=} opt_message Additional message.
 36 * @constructor
 37 * @extends {goog.debug.Error}
 38 */
 39goog.db.Error = function(code, context, opt_message) {
 40  var msg = 'Error ' + context + ': ' + goog.db.Error.getMessage(code);
 41  if (opt_message) {
 42    msg += ', ' + opt_message;
 43  }
 44  goog.base(this, msg);
 45
 46  /**
 47   * The code for this error.
 48   *
 49   * @type {number}
 50   */
 51  this.code = code;
 52};
 53goog.inherits(goog.db.Error, goog.debug.Error);
 54
 55
 56
 57/**
 58 * A specific kind of database error. If a Version Change is unable to proceed
 59 * due to other open database connections, it will block and this error will be
 60 * thrown.
 61 *
 62 * @constructor
 63 * @extends {goog.debug.Error}
 64 */
 65goog.db.Error.VersionChangeBlockedError = function() {
 66  goog.base(this, 'Version change blocked');
 67};
 68goog.inherits(goog.db.Error.VersionChangeBlockedError, goog.debug.Error);
 69
 70
 71/**
 72 * Synthetic error codes for database errors, for use when IndexedDB
 73 * support is not available. This numbering differs in practice
 74 * from the browser implementations, but it is not meant to be reliable:
 75 * this object merely ensures that goog.db.Error is loadable on platforms
 76 * that do not support IndexedDB.
 77 *
 78 * @enum {number}
 79 * @private
 80 */
 81goog.db.Error.DatabaseErrorCode_ = {
 82  UNKNOWN_ERR: 1,
 83  NON_TRANSIENT_ERR: 2,
 84  NOT_FOUND_ERR: 3,
 85  CONSTRAINT_ERR: 4,
 86  DATA_ERR: 5,
 87  NOT_ALLOWED_ERR: 6,
 88  TRANSACTION_INACTIVE_ERR: 7,
 89  ABORT_ERR: 8,
 90  READ_ONLY_ERR: 9,
 91  TRANSIENT_ERR: 11,
 92  TIMEOUT_ERR: 10,
 93  QUOTA_ERR: 11,
 94  INVALID_ACCESS_ERR: 12,
 95  INVALID_STATE_ERR: 13
 96};
 97
 98
 99/**
100 * Error codes for database errors.
101 * @see http://www.w3.org/TR/IndexedDB/#idl-def-IDBDatabaseException
102 *
103 * @enum {number}
104 */
105goog.db.Error.ErrorCode = {
106  UNKNOWN_ERR: (goog.global.IDBDatabaseException ||
107      goog.global.webkitIDBDatabaseException ||
108      goog.db.Error.DatabaseErrorCode_).UNKNOWN_ERR,
109  NON_TRANSIENT_ERR: (goog.global.IDBDatabaseException ||
110      goog.global.webkitIDBDatabaseException ||
111      goog.db.Error.DatabaseErrorCode_).NON_TRANSIENT_ERR,
112  NOT_FOUND_ERR: (goog.global.IDBDatabaseException ||
113      goog.global.webkitIDBDatabaseException ||
114      goog.db.Error.DatabaseErrorCode_).NOT_FOUND_ERR,
115  CONSTRAINT_ERR: (goog.global.IDBDatabaseException ||
116      goog.global.webkitIDBDatabaseException ||
117      goog.db.Error.DatabaseErrorCode_).CONSTRAINT_ERR,
118  DATA_ERR: (goog.global.IDBDatabaseException ||
119      goog.global.webkitIDBDatabaseException ||
120      goog.db.Error.DatabaseErrorCode_).DATA_ERR,
121  NOT_ALLOWED_ERR: (goog.global.IDBDatabaseException ||
122      goog.global.webkitIDBDatabaseException ||
123      goog.db.Error.DatabaseErrorCode_).NOT_ALLOWED_ERR,
124  TRANSACTION_INACTIVE_ERR: (goog.global.IDBDatabaseException ||
125      goog.global.webkitIDBDatabaseException ||
126      goog.db.Error.DatabaseErrorCode_).TRANSACTION_INACTIVE_ERR,
127  ABORT_ERR: (goog.global.IDBDatabaseException ||
128      goog.global.webkitIDBDatabaseException ||
129      goog.db.Error.DatabaseErrorCode_).ABORT_ERR,
130  READ_ONLY_ERR: (goog.global.IDBDatabaseException ||
131      goog.global.webkitIDBDatabaseException ||
132      goog.db.Error.DatabaseErrorCode_).READ_ONLY_ERR,
133  TIMEOUT_ERR: (goog.global.IDBDatabaseException ||
134      goog.global.webkitIDBDatabaseException ||
135      goog.db.Error.DatabaseErrorCode_).TIMEOUT_ERR,
136  QUOTA_ERR: (goog.global.IDBDatabaseException ||
137      goog.global.webkitIDBDatabaseException ||
138      goog.db.Error.DatabaseErrorCode_).QUOTA_ERR,
139  INVALID_ACCESS_ERR: (goog.global.DOMException ||
140      goog.db.Error.DatabaseErrorCode_).INVALID_ACCESS_ERR,
141  INVALID_STATE_ERR: (goog.global.DOMException ||
142      goog.db.Error.DatabaseErrorCode_).INVALID_STATE_ERR
143};
144
145
146/**
147 * Translates an error code into a more useful message.
148 *
149 * @param {number} code Error code.
150 * @return {string} A debug message.
151 */
152goog.db.Error.getMessage = function(code) {
153  switch (code) {
154    case goog.db.Error.ErrorCode.UNKNOWN_ERR:
155      return 'Unknown error';
156    case goog.db.Error.ErrorCode.NON_TRANSIENT_ERR:
157      return 'Invalid operation';
158    case goog.db.Error.ErrorCode.NOT_FOUND_ERR:
159      return 'Required database object not found';
160    case goog.db.Error.ErrorCode.CONSTRAINT_ERR:
161      return 'Constraint unsatisfied';
162    case goog.db.Error.ErrorCode.DATA_ERR:
163      return 'Invalid data';
164    case goog.db.Error.ErrorCode.NOT_ALLOWED_ERR:
165      return 'Operation disallowed';
166    case goog.db.Error.ErrorCode.TRANSACTION_INACTIVE_ERR:
167      return 'Transaction not active';
168    case goog.db.Error.ErrorCode.ABORT_ERR:
169      return 'Request aborted';
170    case goog.db.Error.ErrorCode.READ_ONLY_ERR:
171      return 'Modifying operation not allowed in a read-only transaction';
172    case goog.db.Error.ErrorCode.TIMEOUT_ERR:
173      return 'Transaction timed out';
174    case goog.db.Error.ErrorCode.QUOTA_ERR:
175      return 'Database storage space quota exceeded';
176    case goog.db.Error.ErrorCode.INVALID_ACCESS_ERR:
177      return 'Invalid operation';
178    default:
179      return 'Unrecognized exception with code ' + code;
180  }
181};
182
183
184/**
185 * Returns an error, wrapping it in a {@link goog.db.Error} if it's an IndexedDB
186 * error.
187 *
188 * @param {Error} err The error object to possibly wrap.
189 * @param {string} message The error message to add to err if it's wrapped.
190 * @return {goog.db.Error|Error} The possibly-wrapped error.
191 */
192goog.db.Error.create = function(err, message) {
193  if (!('code' in err)) return err;
194  return new goog.db.Error(err.code, message);
195};