PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/closure/goog/fs/error.js

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