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