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