Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / DOMException.cpp
blob4289a5544e206d8e764f6fb96dda2b1047f5f208
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "config.h"
30 #include "core/dom/DOMException.h"
32 #include "core/dom/ExceptionCode.h"
34 namespace blink {
36 static const struct CoreException {
37 const char* const name;
38 const char* const message;
39 const int code;
40 } coreExceptions[] = {
41 { "IndexSizeError", "Index or size was negative, or greater than the allowed value.", 1 },
42 { "HierarchyRequestError", "A Node was inserted somewhere it doesn't belong.", 3 },
43 { "WrongDocumentError", "A Node was used in a different document than the one that created it (that doesn't support it).", 4 },
44 { "InvalidCharacterError", "The string contains invalid characters.", 5 },
45 { "NoModificationAllowedError", "An attempt was made to modify an object where modifications are not allowed.", 7 },
46 { "NotFoundError", "An attempt was made to reference a Node in a context where it does not exist.", 8 },
47 { "NotSupportedError", "The implementation did not support the requested type of object or operation.", 9 },
48 { "InUseAttributeError", "An attempt was made to add an attribute that is already in use elsewhere.", 10 },
49 { "InvalidStateError", "An attempt was made to use an object that is not, or is no longer, usable.", 11 },
50 { "SyntaxError", "An invalid or illegal string was specified.", 12 },
51 { "InvalidModificationError", "The object can not be modified in this way.", 13 },
52 { "NamespaceError", "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces.", 14 },
53 { "InvalidAccessError", "A parameter or an operation was not supported by the underlying object.", 15 },
54 { "TypeMismatchError", "The type of an object was incompatible with the expected type of the parameter associated to the object.", 17 },
55 { "SecurityError", "An attempt was made to break through the security policy of the user agent.", 18 },
56 { "NetworkError", "A network error occurred.", 19 },
57 { "AbortError", "The user aborted a request.", 20 },
58 { "URLMismatchError", "A worker global scope represented an absolute URL that is not equal to the resulting absolute URL.", 21 },
59 { "QuotaExceededError", "An attempt was made to add something to storage that exceeded the quota.", 22 },
60 { "TimeoutError", "A timeout occurred.", 23 },
61 { "InvalidNodeTypeError", "The supplied node is invalid or has an invalid ancestor for this operation.", 24 },
62 { "DataCloneError", "An object could not be cloned.", 25 },
64 // Indexed DB
65 { "UnknownError", "An unknown error occurred within Indexed Database.", 0 },
66 { "ConstraintError", "A mutation operation in the transaction failed because a constraint was not satisfied.", 0 },
67 { "DataError", "The data provided does not meet requirements.", 0 },
68 { "TransactionInactiveError", "A request was placed against a transaction which is either currently not active, or which is finished.", 0 },
69 { "ReadOnlyError", "A write operation was attempted in a read-only transaction.", 0 },
70 { "VersionError", "An attempt was made to open a database using a lower version than the existing version.", 0 },
72 // File system
73 { "NotReadableError", "The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.", 0 },
74 { "EncodingError", "A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs.", 0 },
75 { "PathExistsError", "An attempt was made to create a file or directory where an element already exists.", 0 },
77 // SQL
78 { "DatabaseError", "The operation failed for some reason related to the database.", 0 },
80 // Web Crypto
81 { "OperationError", "The operation failed for an operation-specific reason", 0 },
83 // Push API
84 { "PermissionDeniedError", "User or security policy denied the request.", 0 },
87 static const CoreException* getErrorEntry(ExceptionCode ec)
89 size_t tableSize = WTF_ARRAY_LENGTH(coreExceptions);
90 size_t tableIndex = ec - IndexSizeError;
92 return tableIndex < tableSize ? &coreExceptions[tableIndex] : 0;
95 static int getErrorCode(const String& name)
97 for (const CoreException& entry : coreExceptions) {
98 if (entry.name == name)
99 return entry.code;
101 return 0;
104 DOMException::DOMException(unsigned short code, const String& name, const String& sanitizedMessage, const String& unsanitizedMessage)
106 ASSERT(name);
107 m_code = code;
108 m_name = name;
109 m_sanitizedMessage = sanitizedMessage;
110 m_unsanitizedMessage = unsanitizedMessage;
113 DOMException* DOMException::create(ExceptionCode ec, const String& sanitizedMessage, const String& unsanitizedMessage)
115 const CoreException* entry = getErrorEntry(ec);
116 ASSERT(entry);
117 return new DOMException(entry->code,
118 entry->name ? entry->name : "Error",
119 sanitizedMessage.isNull() ? String(entry->message) : sanitizedMessage,
120 unsanitizedMessage);
123 DOMException* DOMException::create(const String& message, const String& name)
125 return new DOMException(getErrorCode(name), name, message, message);
128 String DOMException::toString() const
130 return name() + ": " + message();
133 String DOMException::toStringForConsole() const
135 return name() + ": " + messageForConsole();
138 String DOMException::getErrorName(ExceptionCode ec)
140 const CoreException* entry = getErrorEntry(ec);
141 ASSERT(entry);
142 if (!entry)
143 return "UnknownError";
145 return entry->name;
148 String DOMException::getErrorMessage(ExceptionCode ec)
150 const CoreException* entry = getErrorEntry(ec);
151 ASSERT(entry);
152 if (!entry)
153 return "Unknown error.";
155 return entry->message;
158 } // namespace blink