Path: blob/trunk/third_party/closure/goog/fs/error.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A wrapper for the HTML5 FileError object.16*17*/1819goog.provide('goog.fs.DOMErrorLike');20goog.provide('goog.fs.Error');21goog.provide('goog.fs.Error.ErrorCode');2223goog.require('goog.asserts');24goog.require('goog.debug.Error');25goog.require('goog.object');26goog.require('goog.string');2728/** @record */29goog.fs.DOMErrorLike = function() {};3031/** @type {string|undefined} */32goog.fs.DOMErrorLike.prototype.name;3334/** @type {goog.fs.Error.ErrorCode|undefined} */35goog.fs.DOMErrorLike.prototype.code;36373839/**40* A filesystem error. Since the filesystem API is asynchronous, stack traces41* are less useful for identifying where errors come from, so this includes a42* large amount of metadata in the message.43*44* @param {!DOMError|!goog.fs.DOMErrorLike} error45* @param {string} action The action being undertaken when the error was raised.46* @constructor47* @extends {goog.debug.Error}48* @final49*/50goog.fs.Error = function(error, action) {51/** @type {string} */52this.name;5354/**55* @type {goog.fs.Error.ErrorCode}56* @deprecated Use the 'name' or 'message' field instead.57*/58this.code;5960if (goog.isDef(error.name)) {61this.name = error.name;62// TODO(user): Remove warning suppression after JSCompiler stops63// firing a spurious warning here.64/** @suppress {deprecated} */65this.code = goog.fs.Error.getCodeFromName_(error.name);66} else {67this.code = goog.asserts.assertNumber(error.code);68this.name = goog.fs.Error.getNameFromCode_(error.code);69}70goog.fs.Error.base(71this, 'constructor', goog.string.subs('%s %s', this.name, action));72};73goog.inherits(goog.fs.Error, goog.debug.Error);747576/**77* Names of errors that may be thrown by the File API, the File System API, or78* the File Writer API.79*80* @see http://dev.w3.org/2006/webapi/FileAPI/#ErrorAndException81* @see http://www.w3.org/TR/file-system-api/#definitions82* @see http://dev.w3.org/2009/dap/file-system/file-writer.html#definitions83* @enum {string}84*/85goog.fs.Error.ErrorName = {86ABORT: 'AbortError',87ENCODING: 'EncodingError',88INVALID_MODIFICATION: 'InvalidModificationError',89INVALID_STATE: 'InvalidStateError',90NOT_FOUND: 'NotFoundError',91NOT_READABLE: 'NotReadableError',92NO_MODIFICATION_ALLOWED: 'NoModificationAllowedError',93PATH_EXISTS: 'PathExistsError',94QUOTA_EXCEEDED: 'QuotaExceededError',95SECURITY: 'SecurityError',96SYNTAX: 'SyntaxError',97TYPE_MISMATCH: 'TypeMismatchError'98};99100101/**102* Error codes for file errors.103* @see http://www.w3.org/TR/file-system-api/#idl-def-FileException104*105* @enum {number}106* @deprecated Use the 'name' or 'message' attribute instead.107*/108goog.fs.Error.ErrorCode = {109NOT_FOUND: 1,110SECURITY: 2,111ABORT: 3,112NOT_READABLE: 4,113ENCODING: 5,114NO_MODIFICATION_ALLOWED: 6,115INVALID_STATE: 7,116SYNTAX: 8,117INVALID_MODIFICATION: 9,118QUOTA_EXCEEDED: 10,119TYPE_MISMATCH: 11,120PATH_EXISTS: 12121};122123124/**125* @param {goog.fs.Error.ErrorCode|undefined} code126* @return {string} name127* @private128*/129goog.fs.Error.getNameFromCode_ = function(code) {130var name = goog.object.findKey(131goog.fs.Error.NameToCodeMap_, function(c) { return code == c; });132if (!goog.isDef(name)) {133throw new Error('Invalid code: ' + code);134}135return name;136};137138139/**140* Returns the code that corresponds to the given name.141* @param {string} name142* @return {goog.fs.Error.ErrorCode} code143* @private144*/145goog.fs.Error.getCodeFromName_ = function(name) {146return goog.fs.Error.NameToCodeMap_[name];147};148149150/**151* Mapping from error names to values from the ErrorCode enum.152* @see http://www.w3.org/TR/file-system-api/#definitions.153* @private {!Object<string, goog.fs.Error.ErrorCode>}154*/155goog.fs.Error.NameToCodeMap_ = goog.object.create(156goog.fs.Error.ErrorName.ABORT, goog.fs.Error.ErrorCode.ABORT,157158goog.fs.Error.ErrorName.ENCODING, goog.fs.Error.ErrorCode.ENCODING,159160goog.fs.Error.ErrorName.INVALID_MODIFICATION,161goog.fs.Error.ErrorCode.INVALID_MODIFICATION,162163goog.fs.Error.ErrorName.INVALID_STATE,164goog.fs.Error.ErrorCode.INVALID_STATE,165166goog.fs.Error.ErrorName.NOT_FOUND, goog.fs.Error.ErrorCode.NOT_FOUND,167168goog.fs.Error.ErrorName.NOT_READABLE, goog.fs.Error.ErrorCode.NOT_READABLE,169170goog.fs.Error.ErrorName.NO_MODIFICATION_ALLOWED,171goog.fs.Error.ErrorCode.NO_MODIFICATION_ALLOWED,172173goog.fs.Error.ErrorName.PATH_EXISTS, goog.fs.Error.ErrorCode.PATH_EXISTS,174175goog.fs.Error.ErrorName.QUOTA_EXCEEDED,176goog.fs.Error.ErrorCode.QUOTA_EXCEEDED,177178goog.fs.Error.ErrorName.SECURITY, goog.fs.Error.ErrorCode.SECURITY,179180goog.fs.Error.ErrorName.SYNTAX, goog.fs.Error.ErrorCode.SYNTAX,181182goog.fs.Error.ErrorName.TYPE_MISMATCH,183goog.fs.Error.ErrorCode.TYPE_MISMATCH);184185186