tizen 2.3 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Filesystem / JSErrors.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        JSErrors.h
20  */
21
22 #ifndef _TIZEN_FILESYSTEM_JSERRORS_H_
23 #define _TIZEN_FILESYSTEM_JSERRORS_H_
24
25 #include <string>
26 #include <memory>
27 #include <CommonsJavaScript/JSCallbackManager.h>
28 #include <JSWebAPIErrorFactory.h>
29 #include <Logger.h>
30
31 #include "FilesystemUtils.h"
32
33 namespace DeviceAPI {
34 namespace Filesystem {
35
36 bool checkReportAsync(
37         const std::string& aName,
38         const std::string& aMessage,
39         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr aCallback,
40         JSContextRef aContext);
41
42 template <class UserData>
43 inline bool checkReportAsyncAPI(
44         const std::string& aName,
45         const std::string& aMessage,
46         std::shared_ptr<UserData> aCallback,
47         JSContextRef aContext)
48 {
49     LOGD("Error mode check for [%s]", aName.c_str());
50     bool mode = "InvalidValuesError" == aName ||
51                 "IOError" == aName ||
52                 "UnknownError" == aName ||
53                 "NotFoundError" == aName;
54     if (mode)
55     {
56         LOGD("Asynchronous error report for Name:[%s] Message[%s]",
57              aName.c_str(), aMessage.c_str());
58         Utils::MainLoop<std::shared_ptr<UserData> >::passErrorLater(
59                 aName,
60                 aMessage,
61                 JSValueMakeUndefined(aContext),
62                 aCallback);
63     }
64     return mode;
65 }
66
67 std::string translateApi2APIErrors(const std::string& aApiError);
68
69 template <class taEvent>
70 inline void reportErrorCode(const taEvent& aEvent,
71                             const std::string& aErrorName,
72                             const std::string& aMessage)
73 {
74     using namespace WrtDeviceApis::Commons;
75     ExceptionCodes::Enumeration exceptionCode;
76     if ("TypeMismatchError" == aErrorName) {
77         exceptionCode = ExceptionCodes::InvalidArgumentException;
78     } else if ("InvalidValuesError" == aErrorName) {
79         exceptionCode = ExceptionCodes::PlatformException;
80     } else if ("IOError" == aErrorName) {
81         exceptionCode = ExceptionCodes::InvalidArgumentException;
82     } else if ("UnknownError" == aErrorName) {
83         exceptionCode = ExceptionCodes::UnknownException;
84     } else if ("ServiceNotAvailableError" == aErrorName) {
85         exceptionCode = ExceptionCodes::PlatformException;
86     } else if ("SecurityError" == aErrorName) {
87         exceptionCode = ExceptionCodes::SecurityException;
88     } else if ("NotSupportedError" == aErrorName) {
89         exceptionCode = ExceptionCodes::UnsupportedException;
90     } else if ("NotFoundError" == aErrorName) {
91         exceptionCode = ExceptionCodes::NotFoundException;
92     } else if ("InvalidAccessError" == aErrorName) {
93         exceptionCode = ExceptionCodes::SecurityException;
94     } else if ("QuotaExceededError" == aErrorName) {
95         exceptionCode = ExceptionCodes::PlatformException;
96     } else {
97         exceptionCode = ExceptionCodes::UnknownException;
98     }
99     LOGD("Exception %s message %s translated to code %d ",
100          aErrorName.c_str(),
101          aMessage.c_str(),
102          exceptionCode);
103     aEvent->setExceptionCode(exceptionCode);
104 }
105
106 }
107 }
108
109 #endif //ifndef _TIZEN_FILESYSTEM_JSERRORS_H_
110