2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "JSWebAPIErrorFactory.h"
20 #include <dpl/assert.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/ScopedJSStringRef.h>
23 #include "JSWebAPIError2.h"
26 #define INDEX_SIZE_ERROR_NAME_STR "IndexSizeError"
27 #define DOMSTRING_SIZE_ERROR_NAME_STR "DOMStringSizeError"
28 #define HIERARCHY_REQUEST_ERROR_NAME_STR "HierarchyRequestError"
29 #define WRONG_DOCUMENT_ERROR_NAME_STR "WrongDocumentError"
30 #define INVALID_CHARACTER_ERROR_NAME_STR "InvalidCharacterError"
31 #define NO_DATA_ALLOWED_ERROR_NAME_STR "NoDataAllowedError"
32 #define NO_MODIFICATION_ALLOWED_ERROR_NAME_STR "NoModificationAllowedError"
33 #define NOT_FOUND_ERROR_NAME_STR "NotFoundError"
34 #define NOT_SUPPORTED_ERROR_NAME_STR "NotSupportedError"
35 #define INUSE_ATTRIBUTE_ERROR_NAME_STR "InuseAttributeError"
36 #define INVALID_STATE_ERROR_NAME_STR "InvalidStateError"
37 #define SYNTAX_ERROR_NAME_STR "SyntaxError"
38 #define INVALID_MODIFICATION_ERROR_NAME_STR "InvalidModificationError"
39 #define NAMESPACE_ERROR_NAME_STR "NamespaceError"
40 #define INVALID_ACCESS_ERROR_NAME_STR "InvalidAccessError"
41 #define VALIDATION_ERROR_NAME_STR "ValidationError"
42 #define TYPE_MISMATCH_ERROR_NAME_STR "TypeMismatchError"
43 #define SECURITY_ERROR_NAME_STR "SecurityError"
44 #define NETWORK_ERROR_NAME_STR "NetworkError"
45 #define ABORT_ERROR_NAME_STR "AbortError"
46 #define URL_MISMATCH_ERROR_NAME_STR "URLMismatchError"
47 #define QUOTA_EXCEEDED_ERROR_NAME_STR "QuotaExceededError"
48 #define TIMEOUT_ERROR_NAME_STR "TimeoutError"
49 #define INVALID_NODE_TYPE_ERROR_NAME_STR "InvalidNodeTypeError"
50 #define DATA_CLONE_ERROR_NAME_STR "DataCloneError"
51 #define ENCODING_ERROR_NAME_STR "EncodingError"
53 #define UNKNOWN_ERROR_NAME_STR "UnknownError"
54 #define INVALID_VALUES_ERROR_NAME_STR "InvalidValuesError"
55 #define IO_ERROR_NAME_STR "IOError"
56 #define PERMISSION_DENIED_ERROR_NAME_STR "PermissionDeniedError"
57 #define SERVICE_NOT_AVAILABLE_ERROR_NAME_STR "ServiceNotAvailableError"
59 #define ECMA_ERROR_NAME_STR "Error"
60 #define ECMA_EVAL_ERROR_NAME_STR "EvalError"
61 #define ECMA_RANGE_ERROR_NAME_STR "RangeError"
62 #define ECMA_REFERENCE_ERROR_NAME_STR "ReferenceError"
63 #define ECMA_SYNTAX_ERROR_NAME_STR "SyntaxError"
64 #define ECMA_TYPE_ERROR_NAME_STR "TypeError"
65 #define ECMA_URI_ERROR_NAME_STR "URIError"
67 #define CUSTOM_ERROR_NAME_STR "CustomError"
80 struct DeviceAPIErrorProperties
82 const ErrorClass type;
83 const unsigned int code;
86 typedef std::map<std::string, DeviceAPIErrorProperties> DeviceAPIErrorPropertiesMap;
88 static DeviceAPIErrorPropertiesMap s_errorProperties = {
89 { INDEX_SIZE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 1, INDEX_SIZE_ERROR_NAME_STR } },
90 { DOMSTRING_SIZE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 2, DOMSTRING_SIZE_ERROR_NAME_STR } },
91 { HIERARCHY_REQUEST_ERROR_NAME_STR, { ERROR_TYPE_DOM, 3, HIERARCHY_REQUEST_ERROR_NAME_STR } },
92 { WRONG_DOCUMENT_ERROR_NAME_STR, { ERROR_TYPE_DOM, 4, WRONG_DOCUMENT_ERROR_NAME_STR } },
93 { INVALID_CHARACTER_ERROR_NAME_STR, { ERROR_TYPE_DOM, 5, INVALID_CHARACTER_ERROR_NAME_STR } },
94 { NO_DATA_ALLOWED_ERROR_NAME_STR, { ERROR_TYPE_DOM, 6, NO_DATA_ALLOWED_ERROR_NAME_STR } },
95 { NO_MODIFICATION_ALLOWED_ERROR_NAME_STR, { ERROR_TYPE_DOM, 7, NO_MODIFICATION_ALLOWED_ERROR_NAME_STR } },
96 { NOT_FOUND_ERROR_NAME_STR, { ERROR_TYPE_DOM, 8, NOT_FOUND_ERROR_NAME_STR } },
97 { NOT_SUPPORTED_ERROR_NAME_STR, { ERROR_TYPE_DOM, 9, NOT_SUPPORTED_ERROR_NAME_STR } },
98 { INUSE_ATTRIBUTE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 10, INUSE_ATTRIBUTE_ERROR_NAME_STR } },
99 { INVALID_STATE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 11, INVALID_STATE_ERROR_NAME_STR } },
100 { SYNTAX_ERROR_NAME_STR, { ERROR_TYPE_DOM, 12, SYNTAX_ERROR_NAME_STR } },
101 { INVALID_MODIFICATION_ERROR_NAME_STR, { ERROR_TYPE_DOM, 13, INVALID_MODIFICATION_ERROR_NAME_STR } },
102 { NAMESPACE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 14, NAMESPACE_ERROR_NAME_STR } },
103 { INVALID_ACCESS_ERROR_NAME_STR, { ERROR_TYPE_DOM, 15, INVALID_ACCESS_ERROR_NAME_STR } },
104 { VALIDATION_ERROR_NAME_STR, { ERROR_TYPE_DOM, 16, VALIDATION_ERROR_NAME_STR } },
105 { TYPE_MISMATCH_ERROR_NAME_STR, { ERROR_TYPE_DOM, 17, TYPE_MISMATCH_ERROR_NAME_STR } },
106 { SECURITY_ERROR_NAME_STR, { ERROR_TYPE_DOM, 18, SECURITY_ERROR_NAME_STR } },
107 { NETWORK_ERROR_NAME_STR, { ERROR_TYPE_DOM, 19, NETWORK_ERROR_NAME_STR } },
108 { ABORT_ERROR_NAME_STR, { ERROR_TYPE_DOM, 20, ABORT_ERROR_NAME_STR } },
109 { URL_MISMATCH_ERROR_NAME_STR, { ERROR_TYPE_DOM, 21, URL_MISMATCH_ERROR_NAME_STR } },
110 { QUOTA_EXCEEDED_ERROR_NAME_STR, { ERROR_TYPE_DOM, 22, QUOTA_EXCEEDED_ERROR_NAME_STR } },
111 { TIMEOUT_ERROR_NAME_STR, { ERROR_TYPE_DOM, 23, TIMEOUT_ERROR_NAME_STR } },
112 { INVALID_NODE_TYPE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 24, INVALID_NODE_TYPE_ERROR_NAME_STR } },
113 { DATA_CLONE_ERROR_NAME_STR, { ERROR_TYPE_DOM, 25, DATA_CLONE_ERROR_NAME_STR } },
114 { ENCODING_ERROR_NAME_STR, { ERROR_TYPE_DOM, 0, ENCODING_ERROR_NAME_STR } },
115 { UNKNOWN_ERROR_NAME_STR, { ERROR_TYPE_TIZEN, 0, UNKNOWN_ERROR_NAME_STR } },
116 { INVALID_VALUES_ERROR_NAME_STR, { ERROR_TYPE_TIZEN, 0, INVALID_VALUES_ERROR_NAME_STR } },
117 { IO_ERROR_NAME_STR, { ERROR_TYPE_TIZEN, 0, IO_ERROR_NAME_STR } },
118 { PERMISSION_DENIED_ERROR_NAME_STR, { ERROR_TYPE_TIZEN, 0, PERMISSION_DENIED_ERROR_NAME_STR } },
119 { SERVICE_NOT_AVAILABLE_ERROR_NAME_STR, { ERROR_TYPE_TIZEN, 0, SERVICE_NOT_AVAILABLE_ERROR_NAME_STR } },
122 static DeviceAPIErrorProperties s_customErrorProperties = { ERROR_TYPE_CUSTOM, 0, CUSTOM_ERROR_NAME_STR };
124 const std::string JSWebAPIErrorFactory::INDEX_SIZE_ERROR = INDEX_SIZE_ERROR_NAME_STR;
125 const std::string JSWebAPIErrorFactory::DOMSTRING_SIZE_ERROR = DOMSTRING_SIZE_ERROR_NAME_STR;
126 const std::string JSWebAPIErrorFactory::HIERARCHY_REQUEST_ERROR = HIERARCHY_REQUEST_ERROR_NAME_STR;
127 const std::string JSWebAPIErrorFactory::WRONG_DOCUMENT_ERROR = WRONG_DOCUMENT_ERROR_NAME_STR;
128 const std::string JSWebAPIErrorFactory::INVALID_CHARACTER_ERROR = INVALID_CHARACTER_ERROR_NAME_STR;
129 const std::string JSWebAPIErrorFactory::NO_DATA_ALLOWED_ERROR = NO_DATA_ALLOWED_ERROR_NAME_STR;
130 const std::string JSWebAPIErrorFactory::NO_MODIFICATION_ALLOWED_ERROR = NO_MODIFICATION_ALLOWED_ERROR_NAME_STR;
131 const std::string JSWebAPIErrorFactory::NOT_FOUND_ERROR = NOT_FOUND_ERROR_NAME_STR;
132 const std::string JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR = NOT_SUPPORTED_ERROR_NAME_STR;
133 const std::string JSWebAPIErrorFactory::INUSE_ATTRIBUTE_ERROR = INUSE_ATTRIBUTE_ERROR_NAME_STR;
134 const std::string JSWebAPIErrorFactory::INVALID_STATE_ERROR = INVALID_STATE_ERROR_NAME_STR;
135 const std::string JSWebAPIErrorFactory::SYNTAX_ERROR = SYNTAX_ERROR_NAME_STR;
136 const std::string JSWebAPIErrorFactory::INVALID_MODIFICATION_ERROR = INVALID_MODIFICATION_ERROR_NAME_STR;
137 const std::string JSWebAPIErrorFactory::NAMESPACE_ERROR = NAMESPACE_ERROR_NAME_STR;
138 const std::string JSWebAPIErrorFactory::INVALID_ACCESS_ERROR = INVALID_ACCESS_ERROR_NAME_STR;
139 const std::string JSWebAPIErrorFactory::VALIDATION_ERROR = VALIDATION_ERROR_NAME_STR;
140 const std::string JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR = TYPE_MISMATCH_ERROR_NAME_STR;
141 const std::string JSWebAPIErrorFactory::SECURITY_ERROR = SECURITY_ERROR_NAME_STR;
142 const std::string JSWebAPIErrorFactory::NETWORK_ERROR = NETWORK_ERROR_NAME_STR;
143 const std::string JSWebAPIErrorFactory::ABORT_ERROR = ABORT_ERROR_NAME_STR;
144 const std::string JSWebAPIErrorFactory::URL_MISMATCH_ERROR = URL_MISMATCH_ERROR_NAME_STR;
145 const std::string JSWebAPIErrorFactory::QUOTA_EXCEEDED_ERROR = QUOTA_EXCEEDED_ERROR_NAME_STR;
146 const std::string JSWebAPIErrorFactory::TIMEOUT_ERROR = TIMEOUT_ERROR_NAME_STR;
147 const std::string JSWebAPIErrorFactory::INVALID_NODE_TYPE_ERROR = INVALID_NODE_TYPE_ERROR_NAME_STR;
148 const std::string JSWebAPIErrorFactory::DATA_CLONE_ERROR = DATA_CLONE_ERROR_NAME_STR;
149 const std::string JSWebAPIErrorFactory::ENCODING_ERROR = ENCODING_ERROR_NAME_STR;
151 const std::string JSWebAPIErrorFactory::UNKNOWN_ERROR = UNKNOWN_ERROR_NAME_STR;
152 const std::string JSWebAPIErrorFactory::INVALID_VALUES_ERROR = INVALID_VALUES_ERROR_NAME_STR;
153 const std::string JSWebAPIErrorFactory::IO_ERROR = IO_ERROR_NAME_STR;
154 const std::string JSWebAPIErrorFactory::PERMISSION_DENIED_ERROR = PERMISSION_DENIED_ERROR_NAME_STR;
155 const std::string JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR = SERVICE_NOT_AVAILABLE_ERROR_NAME_STR;
157 JSWebAPIErrorFactory::JSWebAPIErrorFactory()
161 JSWebAPIErrorFactory::~JSWebAPIErrorFactory()
165 JSObjectRef JSWebAPIErrorFactory::makeErrorObject(JSContextRef context,
166 const std::string& name,
167 const std::string& message)
169 return createException(context, name, message);
172 JSObjectRef JSWebAPIErrorFactory::makeErrorObject(JSContextRef context,
173 const BasePlatformException& error)
175 return createException(context, error.getName(), error.getMessage());
178 JSValueRef JSWebAPIErrorFactory::postException(JSContextRef context,
179 JSValueRef* exception,
180 const std::string& name,
181 const std::string& message)
183 Assert(exception && "Exception object can't be NULL.");
185 *exception = createException(context, name, message);
187 return JSValueMakeUndefined(context);
190 JSValueRef JSWebAPIErrorFactory::postException(JSContextRef context,
191 JSValueRef* exception,
192 const BasePlatformException& error)
194 Assert(exception && "Exception object can't be NULL.");
196 return postException(context, exception, error.getName(), error.getMessage());
199 JSObjectRef JSWebAPIErrorFactory::createException(JSContextRef context,
200 const std::string& name,
201 const std::string& message)
203 JSObjectRef errorObject = NULL;
205 DeviceAPIErrorProperties *properties;
207 DeviceAPIErrorPropertiesMap::iterator iter = s_errorProperties.find(name);
208 if(iter != s_errorProperties.end())
209 properties = &(iter->second);
211 properties = &s_customErrorProperties;
213 switch(properties->type)
216 case ERROR_TYPE_TIZEN:
217 errorObject = JSWebAPIError2::createJSObject(context,
218 WebAPIErrorPtr(new WebAPIError(properties->code, properties->name, message)));
220 case ERROR_TYPE_ECMA:
221 errorObject = createECMAException(context, properties->name, message);
223 case ERROR_TYPE_CUSTOM:
224 errorObject = JSWebAPIError2::createJSObject(context,
225 WebAPIErrorPtr(new WebAPIError(0, name, message)));
228 errorObject = JSWebAPIError2::createJSObject(context,
229 WebAPIErrorPtr(new WebAPIError(0, "Error", message)));
236 JSObjectRef JSWebAPIErrorFactory::createECMAException(JSContextRef context,
237 const std::string& name,
238 const std::string& message)
240 Assert( (name == ECMA_ERROR_NAME_STR ||
241 name == ECMA_EVAL_ERROR_NAME_STR ||
242 name == ECMA_RANGE_ERROR_NAME_STR ||
243 name == ECMA_REFERENCE_ERROR_NAME_STR ||
244 name == ECMA_SYNTAX_ERROR_NAME_STR ||
245 name == ECMA_TYPE_ERROR_NAME_STR ||
246 name == ECMA_URI_ERROR_NAME_STR ) &&
247 "Name MUST be one of NativeError objects.");
249 std::string jsCodeStr;
250 jsCodeStr = "new " + name + "(";
252 jsCodeStr += "\"" + message + "\"";
255 WrtDeviceApis::CommonsJavaScript::ScopedJSStringRef jsCode(
256 JSStringCreateWithUTF8CString(jsCodeStr.c_str()));
258 JSValueRef errValue = JSEvaluateScript(context, jsCode.get(), NULL, NULL, 0, NULL);
260 return JSValueToObject(context, errValue, NULL);