Shaka Player Embedded
dom_exception.cc
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
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 // https://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 
16 
17 #include <glog/logging.h>
18 
19 #include <unordered_map>
20 
21 #include "src/util/macros.h"
22 
23 namespace shaka {
24 namespace js {
25 namespace dom {
26 
27 namespace {
28 
29 constexpr const char* kDefaultErrorName = "Error";
30 
31 // See: https://www.w3.org/TR/WebIDL-1/#error-names
32 #define DEFINE_MAPPING(code, msg, number) \
33  { #code, msg, code, number }
34 struct ExceptionInfo {
35  const char* name;
36  const char* message;
39 } g_exception_map_[] = {
40  DEFINE_MAPPING(IndexSizeError, "The index is not in the allowed range.", 1),
42  "The operation would yield an incorrect node tree.", 3),
43  DEFINE_MAPPING(WrongDocumentError, "The object is in the wrong Document.",
44  4),
46  "The string contains invalid characters.", 5),
47  DEFINE_MAPPING(NoModificationAllowedError, "The object cannot be modified.",
48  7),
49  DEFINE_MAPPING(NotFoundError, "The object can not be found here.", 8),
50  DEFINE_MAPPING(NotSupportedError, "The operation is not supported.", 9),
51  DEFINE_MAPPING(InUseAttributeError, "The attribute is in use.", 10),
52  DEFINE_MAPPING(InvalidStateError, "The object is in an invalid state.", 11),
54  "The string did not match the expected pattern.", 12),
55  DEFINE_MAPPING(InvalidModificationError, "The object cannot be modified.",
56  13),
58  "The operation is not allowed by Namespaces in XML.", 14),
60  "The object does not support the operation or argument.",
61  15),
63  "The type of the object does not match the expected type.",
64  17),
65  DEFINE_MAPPING(SecurityError, "The operation is insecure.", 18),
66  DEFINE_MAPPING(NetworkError, "A network error occurred.", 19),
67  DEFINE_MAPPING(AbortError, "The operation was aborted.", 20),
69  "The given URL does not match another URL.", 21),
70  DEFINE_MAPPING(QuotaExceededError, "The quota has been exceeded.", 22),
71  DEFINE_MAPPING(TimeoutError, "The operation timed out.", 23),
73  "The node is incorrect or has an incorrect ancestor for "
74  "this operation.",
75  24),
76  DEFINE_MAPPING(DataCloneError, "The object can not be cloned.", 25),
77  DEFINE_MAPPING(EncodingError, "The encoding or decoding operation failed.",
78  0),
79  DEFINE_MAPPING(NotReadableError, "The input/output read operation failed.",
80  0),
81  DEFINE_MAPPING(UnknownError,
82  "The operation failed for an unknown transient reason "
83  "(e.g. out of memory).",
84  0),
86  "A mutation operation in a transaction failed because a "
87  "constraint was not satisfied.",
88  0),
89  DEFINE_MAPPING(DataError, "Provided data is inadequate.", 0),
91  "A request was placed against a transaction which is "
92  "currently not active, or which is finished.",
93  0),
95  "The mutating operation was attempted in a \"readonly\" "
96  "transaction.",
97  0),
99  "An attempt was made to open a database using a lower "
100  "version than the existing version.",
101  0),
103  "The operation failed for an operation-specific reason.", 0),
105  "The request is not allowed by the user agent or the "
106  "platform in the current context, possibly because the "
107  "user denied permission.",
108  0)};
109 static_assert(sizeof(g_exception_map_) / sizeof(g_exception_map_[0]) ==
111  "Not all exceptions appear in map");
112 #undef DEFINE_MAPPING
113 
114 const ExceptionInfo& GetInfo(ExceptionCode type) {
115  for (const auto& except : g_exception_map_) {
116  if (except.type == type) {
117  return except;
118  }
119  }
120  LOG(FATAL) << "Unknown exception type: " << type;
121 }
122 
123 const ExceptionInfo* GetInfoByName(optional<std::string> name) {
124  if (!name.has_value()) {
125  return nullptr;
126  }
127 
128  for (const auto& except : g_exception_map_) {
129  if (except.name == name) {
130  return &except;
131  }
132  }
133 
134  return nullptr;
135 }
136 
137 int GetNativeCode(optional<std::string> name) {
138  const ExceptionInfo* except = GetInfoByName(name);
139  return except ? except->native_code : 0;
140 }
141 
142 std::string GetDefaultMessage(optional<std::string> name) {
143  const ExceptionInfo* except = GetInfoByName(name);
144  return except ? except->message : "";
145 }
146 
147 } // namespace
148 
150  : error_name(GetInfo(type).name),
151  message(GetInfo(type).message),
152  code(GetInfo(type).native_code) {}
153 
155  : error_name(GetInfo(type).name),
156  message(message.value_or(GetInfo(type).message)),
157  code(GetInfo(type).native_code) {}
158 
161  : error_name(name.value_or(kDefaultErrorName)),
162  message(message.value_or(GetDefaultMessage(name))),
163  code(GetNativeCode(name)) {}
164 
165 // \cond Doxygen_Skip
166 DOMException::~DOMException() {}
167 // \endcond Doxygen_Skip
168 
170  AddConstant("INDEX_SIZE_ERR", GetInfo(IndexSizeError).native_code);
171  AddConstant("HIERARCHY_REQUEST_ERR",
172  GetInfo(HierarchyRequestError).native_code);
173  AddConstant("WRONG_DOCUMENT_ERR", GetInfo(WrongDocumentError).native_code);
174  AddConstant("INVALID_CHARACTER_ERR",
175  GetInfo(InvalidCharacterError).native_code);
176  AddConstant("NO_MODIFICATION_ALLOWED_ERR",
177  GetInfo(NoModificationAllowedError).native_code);
178  AddConstant("NOT_FOUND_ERR", GetInfo(NotFoundError).native_code);
179  AddConstant("INUSE_ATTRIBUTE_ERR", GetInfo(InUseAttributeError).native_code);
180  AddConstant("NOT_SUPPORTED_ERR", GetInfo(NotSupportedError).native_code);
181  AddConstant("INVALID_STATE_ERR", GetInfo(InvalidStateError).native_code);
182  AddConstant("SYNTAX_ERR", GetInfo(SyntaxError).native_code);
183  AddConstant("INVALID_MODIFICATION_ERR",
184  GetInfo(InvalidModificationError).native_code);
185  AddConstant("NAMESPACE_ERR", GetInfo(NamespaceError).native_code);
186  AddConstant("INVALID_ACCESS_ERR", GetInfo(InvalidAccessError).native_code);
187  AddConstant("TYPE_MISMATCH_ERR", GetInfo(TypeMismatchError).native_code);
188  AddConstant("SECURITY_ERR", GetInfo(SecurityError).native_code);
189  AddConstant("NETWORK_ERR", GetInfo(NetworkError).native_code);
190  AddConstant("ABORT_ERR", GetInfo(AbortError).native_code);
191  AddConstant("URL_MISMATCH_ERR", GetInfo(URLMismatchError).native_code);
192  AddConstant("QUOTA_EXCEEDED_ERR", GetInfo(QuotaExceededError).native_code);
193  AddConstant("TIMEOUT_ERR", GetInfo(TimeoutError).native_code);
194  AddConstant("INVALID_NODE_TYPE_ERR",
195  GetInfo(InvalidNodeTypeError).native_code);
196  AddConstant("DATA_CLONE_ERR", GetInfo(DataCloneError).native_code);
197 
198  AddReadOnlyProperty("name", &DOMException::error_name);
199  AddReadOnlyProperty("message", &DOMException::message);
200  AddReadOnlyProperty("code", &DOMException::code);
201  AddReadOnlyProperty("stack", &DOMException::stack);
202 }
203 
204 } // namespace dom
205 } // namespace js
206 } // namespace shaka
#define DEFINE_MAPPING(code, msg, number)
const char * name
ExceptionCode type
T value_or(U &&default_value) const &
Definition: optional.h:165
int native_code
DOMException(ExceptionCode type)
const std::string error_name
Definition: dom_exception.h:42
const char * message
const std::string message
Definition: dom_exception.h:43
std::string name() const