error.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 A wrapper for the HTML5 FileError object.
  16. *
  17. */
  18. goog.provide('goog.fs.DOMErrorLike');
  19. goog.provide('goog.fs.Error');
  20. goog.provide('goog.fs.Error.ErrorCode');
  21. goog.require('goog.asserts');
  22. goog.require('goog.debug.Error');
  23. goog.require('goog.object');
  24. goog.require('goog.string');
  25. /** @record */
  26. goog.fs.DOMErrorLike = function() {};
  27. /** @type {string|undefined} */
  28. goog.fs.DOMErrorLike.prototype.name;
  29. /** @type {goog.fs.Error.ErrorCode|undefined} */
  30. goog.fs.DOMErrorLike.prototype.code;
  31. /**
  32. * A filesystem error. Since the filesystem API is asynchronous, stack traces
  33. * are less useful for identifying where errors come from, so this includes a
  34. * large amount of metadata in the message.
  35. *
  36. * @param {!DOMError|!goog.fs.DOMErrorLike} error
  37. * @param {string} action The action being undertaken when the error was raised.
  38. * @constructor
  39. * @extends {goog.debug.Error}
  40. * @final
  41. */
  42. goog.fs.Error = function(error, action) {
  43. /** @type {string} */
  44. this.name;
  45. /**
  46. * @type {goog.fs.Error.ErrorCode}
  47. * @deprecated Use the 'name' or 'message' field instead.
  48. */
  49. this.code;
  50. if (goog.isDef(error.name)) {
  51. this.name = error.name;
  52. // TODO(user): Remove warning suppression after JSCompiler stops
  53. // firing a spurious warning here.
  54. /** @suppress {deprecated} */
  55. this.code = goog.fs.Error.getCodeFromName_(error.name);
  56. } else {
  57. this.code = goog.asserts.assertNumber(error.code);
  58. this.name = goog.fs.Error.getNameFromCode_(error.code);
  59. }
  60. goog.fs.Error.base(
  61. this, 'constructor', goog.string.subs('%s %s', this.name, action));
  62. };
  63. goog.inherits(goog.fs.Error, goog.debug.Error);
  64. /**
  65. * Names of errors that may be thrown by the File API, the File System API, or
  66. * the File Writer API.
  67. *
  68. * @see http://dev.w3.org/2006/webapi/FileAPI/#ErrorAndException
  69. * @see http://www.w3.org/TR/file-system-api/#definitions
  70. * @see http://dev.w3.org/2009/dap/file-system/file-writer.html#definitions
  71. * @enum {string}
  72. */
  73. goog.fs.Error.ErrorName = {
  74. ABORT: 'AbortError',
  75. ENCODING: 'EncodingError',
  76. INVALID_MODIFICATION: 'InvalidModificationError',
  77. INVALID_STATE: 'InvalidStateError',
  78. NOT_FOUND: 'NotFoundError',
  79. NOT_READABLE: 'NotReadableError',
  80. NO_MODIFICATION_ALLOWED: 'NoModificationAllowedError',
  81. PATH_EXISTS: 'PathExistsError',
  82. QUOTA_EXCEEDED: 'QuotaExceededError',
  83. SECURITY: 'SecurityError',
  84. SYNTAX: 'SyntaxError',
  85. TYPE_MISMATCH: 'TypeMismatchError'
  86. };
  87. /**
  88. * Error codes for file errors.
  89. * @see http://www.w3.org/TR/file-system-api/#idl-def-FileException
  90. *
  91. * @enum {number}
  92. * @deprecated Use the 'name' or 'message' attribute instead.
  93. */
  94. goog.fs.Error.ErrorCode = {
  95. NOT_FOUND: 1,
  96. SECURITY: 2,
  97. ABORT: 3,
  98. NOT_READABLE: 4,
  99. ENCODING: 5,
  100. NO_MODIFICATION_ALLOWED: 6,
  101. INVALID_STATE: 7,
  102. SYNTAX: 8,
  103. INVALID_MODIFICATION: 9,
  104. QUOTA_EXCEEDED: 10,
  105. TYPE_MISMATCH: 11,
  106. PATH_EXISTS: 12
  107. };
  108. /**
  109. * @param {goog.fs.Error.ErrorCode|undefined} code
  110. * @return {string} name
  111. * @private
  112. */
  113. goog.fs.Error.getNameFromCode_ = function(code) {
  114. var name = goog.object.findKey(
  115. goog.fs.Error.NameToCodeMap_, function(c) { return code == c; });
  116. if (!goog.isDef(name)) {
  117. throw new Error('Invalid code: ' + code);
  118. }
  119. return name;
  120. };
  121. /**
  122. * Returns the code that corresponds to the given name.
  123. * @param {string} name
  124. * @return {goog.fs.Error.ErrorCode} code
  125. * @private
  126. */
  127. goog.fs.Error.getCodeFromName_ = function(name) {
  128. return goog.fs.Error.NameToCodeMap_[name];
  129. };
  130. /**
  131. * Mapping from error names to values from the ErrorCode enum.
  132. * @see http://www.w3.org/TR/file-system-api/#definitions.
  133. * @private {!Object<string, goog.fs.Error.ErrorCode>}
  134. */
  135. goog.fs.Error.NameToCodeMap_ = goog.object.create(
  136. goog.fs.Error.ErrorName.ABORT, goog.fs.Error.ErrorCode.ABORT,
  137. goog.fs.Error.ErrorName.ENCODING, goog.fs.Error.ErrorCode.ENCODING,
  138. goog.fs.Error.ErrorName.INVALID_MODIFICATION,
  139. goog.fs.Error.ErrorCode.INVALID_MODIFICATION,
  140. goog.fs.Error.ErrorName.INVALID_STATE,
  141. goog.fs.Error.ErrorCode.INVALID_STATE,
  142. goog.fs.Error.ErrorName.NOT_FOUND, goog.fs.Error.ErrorCode.NOT_FOUND,
  143. goog.fs.Error.ErrorName.NOT_READABLE, goog.fs.Error.ErrorCode.NOT_READABLE,
  144. goog.fs.Error.ErrorName.NO_MODIFICATION_ALLOWED,
  145. goog.fs.Error.ErrorCode.NO_MODIFICATION_ALLOWED,
  146. goog.fs.Error.ErrorName.PATH_EXISTS, goog.fs.Error.ErrorCode.PATH_EXISTS,
  147. goog.fs.Error.ErrorName.QUOTA_EXCEEDED,
  148. goog.fs.Error.ErrorCode.QUOTA_EXCEEDED,
  149. goog.fs.Error.ErrorName.SECURITY, goog.fs.Error.ErrorCode.SECURITY,
  150. goog.fs.Error.ErrorName.SYNTAX, goog.fs.Error.ErrorCode.SYNTAX,
  151. goog.fs.Error.ErrorName.TYPE_MISMATCH,
  152. goog.fs.Error.ErrorCode.TYPE_MISMATCH);