1 /*
2  *  Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "ExceptionCode.h"
28 
29 #include "EventException.h"
30 #include "IDBDatabaseException.h"
31 #include "RangeException.h"
32 #include "XMLHttpRequestException.h"
33 
34 #if ENABLE(SVG)
35 #include "SVGException.h"
36 #endif
37 
38 #if ENABLE(XPATH)
39 #include "XPathException.h"
40 #endif
41 
42 #if ENABLE(DATABASE)
43 #include "SQLException.h"
44 #endif
45 
46 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
47 #include "FileException.h"
48 #endif
49 
50 namespace WebCore {
51 
52 static const char* const exceptionNames[] = {
53     "INDEX_SIZE_ERR",
54     "DOMSTRING_SIZE_ERR",
55     "HIERARCHY_REQUEST_ERR",
56     "WRONG_DOCUMENT_ERR",
57     "INVALID_CHARACTER_ERR",
58     "NO_DATA_ALLOWED_ERR",
59     "NO_MODIFICATION_ALLOWED_ERR",
60     "NOT_FOUND_ERR",
61     "NOT_SUPPORTED_ERR",
62     "INUSE_ATTRIBUTE_ERR",
63     "INVALID_STATE_ERR",
64     "SYNTAX_ERR",
65     "INVALID_MODIFICATION_ERR",
66     "NAMESPACE_ERR",
67     "INVALID_ACCESS_ERR",
68     "VALIDATION_ERR",
69     "TYPE_MISMATCH_ERR",
70     "SECURITY_ERR",
71     "NETWORK_ERR",
72     "ABORT_ERR",
73     "URL_MISMATCH_ERR",
74     "QUOTA_EXCEEDED_ERR"
75 };
76 
77 static const char* const exceptionDescriptions[] = {
78     "Index or size was negative, or greater than the allowed value.",
79     "The specified range of text did not fit into a DOMString.",
80     "A Node was inserted somewhere it doesn't belong.",
81     "A Node was used in a different document than the one that created it (that doesn't support it).",
82     "An invalid or illegal character was specified, such as in an XML name.",
83     "Data was specified for a Node which does not support data.",
84     "An attempt was made to modify an object where modifications are not allowed.",
85     "An attempt was made to reference a Node in a context where it does not exist.",
86     "The implementation did not support the requested type of object or operation.",
87     "An attempt was made to add an attribute that is already in use elsewhere.",
88     "An attempt was made to use an object that is not, or is no longer, usable.",
89     "An invalid or illegal string was specified.",
90     "An attempt was made to modify the type of the underlying object.",
91     "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces.",
92     "A parameter or an operation was not supported by the underlying object.",
93     "A call to a method such as insertBefore or removeChild would make the Node invalid with respect to \"partial validity\", this exception would be raised and the operation would not be done.",
94     "The type of an object was incompatible with the expected type of the parameter associated to the object.",
95     "An attempt was made to break through the security policy of the user agent.",
96     // FIXME: Couldn't find a description in the HTML/DOM specifications for NETWORK_ERR, ABORT_ERR, URL_MISMATCH_ERR, and QUOTA_EXCEEDED_ERR
97     "A network error occured.",
98     "The user aborted a request.",
99     "A worker global scope represented an absolute URL that is not equal to the resulting absolute URL.",
100     "An attempt was made to add something to storage that exceeded the quota."
101 };
102 
103 static const char* const rangeExceptionNames[] = {
104     "BAD_BOUNDARYPOINTS_ERR",
105     "INVALID_NODE_TYPE_ERR"
106 };
107 
108 static const char* const rangeExceptionDescriptions[] = {
109     "The boundary-points of a Range did not meet specific requirements.",
110     "The container of an boundary-point of a Range was being set to either a node of an invalid type or a node with an ancestor of an invalid type."
111 };
112 
113 static const char* const eventExceptionNames[] = {
114     "UNSPECIFIED_EVENT_TYPE_ERR"
115 };
116 
117 static const char* const eventExceptionDescriptions[] = {
118     "The Event's type was not specified by initializing the event before the method was called."
119 };
120 
121 static const char* const xmlHttpRequestExceptionNames[] = {
122     "NETWORK_ERR",
123     "ABORT_ERR"
124 };
125 
126 static const char* const xmlHttpRequestExceptionDescriptions[] = {
127     "A network error occured in synchronous requests.",
128     "The user aborted a request in synchronous requests."
129 };
130 
131 #if ENABLE(XPATH)
132 static const char* const xpathExceptionNames[] = {
133     "INVALID_EXPRESSION_ERR",
134     "TYPE_ERR"
135 };
136 
137 static const char* const xpathExceptionDescriptions[] = {
138     "The expression had a syntax error or otherwise is not a legal expression according to the rules of the specific XPathEvaluator.",
139     "The expression could not be converted to return the specified type."
140 };
141 #endif
142 
143 #if ENABLE(SVG)
144 static const char* const svgExceptionNames[] = {
145     "SVG_WRONG_TYPE_ERR",
146     "SVG_INVALID_VALUE_ERR",
147     "SVG_MATRIX_NOT_INVERTABLE"
148 };
149 
150 static const char* const svgExceptionDescriptions[] = {
151     "An object of the wrong type was passed to an operation.",
152     "An invalid value was passed to an operation or assigned to an attribute.",
153     "An attempt was made to invert a matrix that is not invertible."
154 };
155 #endif
156 
157 #if ENABLE(DATABASE)
158 static const char* const sqlExceptionNames[] = {
159     "UNKNOWN_ERR",
160     "DATABASE_ERR",
161     "VERSION_ERR",
162     "TOO_LARGE_ERR",
163     "QUOTA_ERR",
164     "SYNTAX_ERR",
165     "CONSTRAINT_ERR",
166     "TIMEOUT_ERR"
167 };
168 
169 static const char* const sqlExceptionDescriptions[] = {
170     "The operation failed for reasons unrelated to the database.",
171     "The operation failed for some reason related to the database.",
172     "The actual database version did not match the expected version.",
173     "Data returned from the database is too large.",
174     "Quota was exceeded.",
175     "Invalid or unauthorized statement; or the number of arguments did not match the number of ? placeholders.",
176     "A constraint was violated.",
177     "A transaction lock could not be acquired in a reasonable time."
178 };
179 #endif
180 
181 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
182 static const char* const fileExceptionNames[] = {
183     "NOT_FOUND_ERR",
184     "SECURITY_ERR",
185     "ABORT_ERR",
186     "NOT_READABLE_ERR",
187     "ENCODING_ERR",
188     "NO_MODIFICATION_ALLOWED_ERR",
189     "INVALID_STATE_ERR",
190     "SYNTAX_ERR",
191     "INVALID_MODIFICATION_ERR",
192     "QUOTA_EXCEEDED_ERR",
193     "TYPE_MISMATCH_ERR",
194     "PATH_EXISTS_ERR"
195 };
196 
197 static const char* const fileExceptionDescriptions[] = {
198     "A requested file or directory could not be found at the time an operation was processed.",
199     "It was determined that certain files are unsafe for access within a Web application, or that too many calls are being made on file resources.",
200     "An ongoing operation was aborted, typically with a call to abort().",
201     "The requested file could not be read, typically due to permission problems that have occured after a reference to a file was acquired.",
202     "A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs.",
203     "An attempt was made to write to a file or directory which could not be modified due to the state of the underlying filesystem.",
204     "An operation that depends on state cached in an interface object was made but the state had changed since it was read from disk.",
205     "An invalid or unsupported argument was given, like an invalid line ending specifier.",
206     "The modification request was illegal.",
207     "The operation failed because it would cause the application to exceed its storage quota.",
208     "The path supplied exists, but was not an entry of requested type.",
209     "An attempt was made to create a file or directory where an element already exists."
210 };
211 #endif
212 
213 #if ENABLE(INDEXED_DATABASE)
214 static const char* const idbDatabaseExceptionNames[] = {
215     "UNKNOWN_ERR",
216     "NON_TRANSIENT_ERR",
217     "NOT_FOUND_ERR",
218     "CONSTRAINT_ERR",
219     "DATA_ERR",
220     "NOT_ALLOWED_ERR",
221     "SERIAL_ERR",
222     "RECOVERABLE_ERR",
223     "TRANSIENT_ERR",
224     "TIMEOUT_ERR",
225     "DEADLOCK_ERR",
226     "READ_ONLY_ERR",
227     "ABORT_ERR"
228 };
229 
230 static const char* const idbDatabaseExceptionDescriptions[] = {
231     "An unknown error occurred within Indexed Database.",
232     "NON_TRANSIENT_ERR", // FIXME: Write a better message if it's ever possible this is thrown.
233     "The name supplied does not match any existing item.",
234     "The request cannot be completed due to a failed constraint.",
235     "The data provided does not meet the requirements of the function.",
236     "This function is not allowed to be called in such a context.",
237     "The data supplied cannot be serialized according to the structured cloning algorithm.",
238     "RECOVERABLE_ERR", // FIXME: This isn't even used.
239     "TRANSIENT_ERR", // FIXME: This isn't even used.
240     "TIMEOUT_ERR", // This can't be thrown.
241     "DEADLOCK_ERR", // This can't be thrown.
242     "Write operations cannot be preformed on a read-only transaction.",
243     "The transaction was aborted, so the request cannot be fulfilled."
244 };
245 #endif
246 
getExceptionCodeDescription(ExceptionCode ec,ExceptionCodeDescription & description)247 void getExceptionCodeDescription(ExceptionCode ec, ExceptionCodeDescription& description)
248 {
249     ASSERT(ec);
250 
251     const char* typeName;
252     int code = ec;
253     const char* const* nameTable;
254     const char* const* descriptionTable;
255     int nameTableSize;
256     int nameTableOffset;
257     ExceptionType type;
258 
259     if (code >= RangeException::RangeExceptionOffset && code <= RangeException::RangeExceptionMax) {
260         type = RangeExceptionType;
261         typeName = "DOM Range";
262         code -= RangeException::RangeExceptionOffset;
263         nameTable = rangeExceptionNames;
264         descriptionTable = rangeExceptionDescriptions;
265         nameTableSize = WTF_ARRAY_LENGTH(rangeExceptionNames);
266         nameTableOffset = RangeException::BAD_BOUNDARYPOINTS_ERR;
267     } else if (code >= EventException::EventExceptionOffset && code <= EventException::EventExceptionMax) {
268         type = EventExceptionType;
269         typeName = "DOM Events";
270         code -= EventException::EventExceptionOffset;
271         nameTable = eventExceptionNames;
272         descriptionTable = eventExceptionDescriptions;
273         nameTableSize = WTF_ARRAY_LENGTH(eventExceptionNames);
274         nameTableOffset = EventException::UNSPECIFIED_EVENT_TYPE_ERR;
275     } else if (code >= XMLHttpRequestException::XMLHttpRequestExceptionOffset && code <= XMLHttpRequestException::XMLHttpRequestExceptionMax) {
276         type = XMLHttpRequestExceptionType;
277         typeName = "XMLHttpRequest";
278         code -= XMLHttpRequestException::XMLHttpRequestExceptionOffset;
279         nameTable = xmlHttpRequestExceptionNames;
280         descriptionTable = xmlHttpRequestExceptionDescriptions;
281         nameTableSize = WTF_ARRAY_LENGTH(xmlHttpRequestExceptionNames);
282         // XMLHttpRequest exception codes start with 101 and we don't want 100 empty elements in the name array
283         nameTableOffset = XMLHttpRequestException::NETWORK_ERR;
284 #if ENABLE(XPATH)
285     } else if (code >= XPathException::XPathExceptionOffset && code <= XPathException::XPathExceptionMax) {
286         type = XPathExceptionType;
287         typeName = "DOM XPath";
288         code -= XPathException::XPathExceptionOffset;
289         nameTable = xpathExceptionNames;
290         descriptionTable = xpathExceptionDescriptions;
291         nameTableSize = WTF_ARRAY_LENGTH(xpathExceptionNames);
292         // XPath exception codes start with 51 and we don't want 51 empty elements in the name array
293         nameTableOffset = XPathException::INVALID_EXPRESSION_ERR;
294 #endif
295 #if ENABLE(SVG)
296     } else if (code >= SVGException::SVGExceptionOffset && code <= SVGException::SVGExceptionMax) {
297         type = SVGExceptionType;
298         typeName = "DOM SVG";
299         code -= SVGException::SVGExceptionOffset;
300         nameTable = svgExceptionNames;
301         descriptionTable = svgExceptionDescriptions;
302         nameTableSize = WTF_ARRAY_LENGTH(svgExceptionNames);
303         nameTableOffset = SVGException::SVG_WRONG_TYPE_ERR;
304 #endif
305 #if ENABLE(DATABASE)
306     } else if (code >= SQLException::SQLExceptionOffset && code <= SQLException::SQLExceptionMax) {
307         type = SQLExceptionType;
308         typeName = "DOM SQL";
309         code -= SQLException::SQLExceptionOffset;
310         nameTable = sqlExceptionNames;
311         descriptionTable = sqlExceptionDescriptions;
312         nameTableSize = WTF_ARRAY_LENGTH(sqlExceptionNames);
313         nameTableOffset = SQLException::UNKNOWN_ERR;
314 #endif
315 #if ENABLE(BLOB) || ENABLE(FILE_SYSTEM)
316     } else if (code >= FileException::FileExceptionOffset && code <= FileException::FileExceptionMax) {
317         type = FileExceptionType;
318         typeName = "DOM File";
319         code -= FileException::FileExceptionOffset;
320         nameTable = fileExceptionNames;
321         descriptionTable = fileExceptionDescriptions;
322         nameTableSize = WTF_ARRAY_LENGTH(fileExceptionNames);
323         nameTableOffset = FileException::NOT_FOUND_ERR;
324 #endif
325 #if ENABLE(INDEXED_DATABASE)
326     } else if (code >= IDBDatabaseException::IDBDatabaseExceptionOffset && code <= IDBDatabaseException::IDBDatabaseExceptionMax) {
327         type = IDBDatabaseExceptionType;
328         typeName = "DOM IDBDatabase";
329         code -= IDBDatabaseException::IDBDatabaseExceptionOffset;
330         nameTable = idbDatabaseExceptionNames;
331         descriptionTable = idbDatabaseExceptionDescriptions;
332         nameTableSize = WTF_ARRAY_LENGTH(idbDatabaseExceptionNames);
333         nameTableOffset = IDBDatabaseException::UNKNOWN_ERR;
334 #endif
335     } else {
336         type = DOMExceptionType;
337         typeName = "DOM";
338         nameTable = exceptionNames;
339         descriptionTable = exceptionDescriptions;
340         nameTableSize = WTF_ARRAY_LENGTH(exceptionNames);
341         nameTableOffset = INDEX_SIZE_ERR;
342     }
343 
344     description.typeName = typeName;
345     description.name = (ec >= nameTableOffset && ec - nameTableOffset < nameTableSize) ? nameTable[ec - nameTableOffset] : 0;
346     description.description = (ec >= nameTableOffset && ec - nameTableOffset < nameTableSize) ? descriptionTable[ec - nameTableOffset] : 0;
347     description.code = code;
348     description.type = type;
349 
350     // All exceptions used in the DOM code should have names.
351     ASSERT(description.name);
352     ASSERT(description.description);
353 }
354 
355 } // namespace WebCore
356