1/*
2 * Copyright (C) 2011 Google 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 *
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 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.
16 *
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.
27 */
28
29#include "config.h"
30#include "DOMException.h"
31
32#include "Exception.h"
33
34namespace WebCore {
35
36// This array needs to be kept in sync with the ExceptionCode enumeration.
37// http://heycam.github.io/webidl/#idl-DOMException-error-names
38static const DOMException::Description descriptions[] = {
39 { "IndexSizeError"_s, "The index is not in the allowed range."_s, 1 },
40 { "HierarchyRequestError"_s, "The operation would yield an incorrect node tree."_s, 3 },
41 { "WrongDocumentError"_s, "The object is in the wrong document."_s, 4 },
42 { "InvalidCharacterError"_s, "The string contains invalid characters."_s, 5 },
43 { "NoModificationAllowedError"_s, "The object can not be modified."_s, 7 },
44 { "NotFoundError"_s, "The object can not be found here."_s, 8 },
45 { "NotSupportedError"_s, "The operation is not supported."_s, 9 },
46 { "InUseAttributeError"_s, "The attribute is in use."_s, 10 },
47 { "InvalidStateError"_s, "The object is in an invalid state."_s, 11 },
48 { "SyntaxError"_s, "The string did not match the expected pattern."_s, 12 },
49 { "InvalidModificationError"_s, " The object can not be modified in this way."_s, 13 },
50 { "NamespaceError"_s, "The operation is not allowed by Namespaces in XML."_s, 14 },
51 { "InvalidAccessError"_s, "The object does not support the operation or argument."_s, 15 },
52 { "TypeMismatchError"_s, "The type of an object was incompatible with the expected type of the parameter associated to the object."_s, 17 },
53 { "SecurityError"_s, "The operation is insecure."_s, 18 },
54 { "NetworkError"_s, " A network error occurred."_s, 19 },
55 { "AbortError"_s, "The operation was aborted."_s, 20 },
56 { "URLMismatchError"_s, "The given URL does not match another URL."_s, 21 },
57 { "QuotaExceededError"_s, "The quota has been exceeded."_s, 22 },
58 { "TimeoutError"_s, "The operation timed out."_s, 23 },
59 { "InvalidNodeTypeError"_s, "The supplied node is incorrect or has an incorrect ancestor for this operation."_s, 24 },
60 { "DataCloneError"_s, "The object can not be cloned."_s, 25 },
61 { "EncodingError"_s, "The encoding operation (either encoded or decoding) failed."_s, 0 },
62 { "NotReadableError"_s, "The I/O read operation failed."_s, 0 },
63 { "UnknownError"_s, "The operation failed for an unknown transient reason (e.g. out of memory)."_s, 0 },
64 { "ConstraintError"_s, "A mutation operation in a transaction failed because a constraint was not satisfied."_s, 0 },
65 { "DataError"_s, "Provided data is inadequate."_s, 0 },
66 { "TransactionInactiveError"_s, "A request was placed against a transaction which is currently not active, or which is finished."_s, 0 },
67 { "ReadOnlyError"_s, "The mutating operation was attempted in a \"readonly\" transaction."_s, 0 },
68 { "VersionError"_s, "An attempt was made to open a database using a lower version than the existing version."_s, 0 },
69 { "OperationError"_s, "The operation failed for an operation-specific reason."_s, 0 },
70 { "NotAllowedError"_s, "The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission."_s, 0 }
71};
72static_assert(!IndexSizeError, "This table needs to be kept in sync with DOMException names in ExceptionCode enumeration");
73static_assert(NotAllowedError == WTF_ARRAY_LENGTH(descriptions) - 1, "This table needs to be kept in sync with DOMException names in ExceptionCode enumeration");
74
75auto DOMException::description(ExceptionCode ec) -> const Description&
76{
77 if (ec < WTF_ARRAY_LENGTH(descriptions))
78 return descriptions[ec];
79
80 static const Description emptyDescription { ASCIILiteral::null(), ASCIILiteral::null(), 0 };
81 return emptyDescription;
82}
83
84static DOMException::LegacyCode legacyCodeFromName(const String& name)
85{
86 for (auto& description : descriptions) {
87 if (description.name == name)
88 return description.legacyCode;
89 }
90 return 0;
91}
92
93Ref<DOMException> DOMException::create(ExceptionCode ec, const String& message)
94{
95 auto& description = DOMException::description(ec);
96 return adoptRef(*new DOMException(description.legacyCode, description.name, !message.isEmpty() ? message : description.message));
97}
98
99Ref<DOMException> DOMException::create(const String& message, const String& name)
100{
101 return adoptRef(*new DOMException(legacyCodeFromName(name), name, message));
102}
103
104Ref<DOMException> DOMException::create(const Exception& exception)
105{
106 auto& description = DOMException::description(exception.code());
107 return adoptRef(*new DOMException(description.legacyCode, description.name, exception.message().isEmpty() ? description.message : exception.message()));
108}
109
110DOMException::DOMException(LegacyCode legacyCode, const String& name, const String& message)
111 : m_legacyCode(legacyCode)
112 , m_name(name)
113 , m_message(message)
114{
115}
116
117} // namespace WebCore
118