Debian Codegraph @

closure-library/closure/goog/fs/error.js

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