• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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  *     http://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  */
15 #ifndef RDB_JS_NAPI_ERROR_H
16 #define RDB_JS_NAPI_ERROR_H
17 
18 #include <map>
19 #include <optional>
20 #include <string>
21 
22 #include "logger.h"
23 #include "rdb_errno.h"
24 
25 namespace OHOS {
26 namespace RelationalStoreJsKit {
27 constexpr int MAX_INPUT_COUNT = 10;
28 constexpr int OK = 0;
29 constexpr int ERR = -1;
30 
31 constexpr int E_NON_SYSTEM_APP_ERROR = 202;
32 constexpr int E_PARAM_ERROR = 401;
33 constexpr int E_INNER_ERROR = 14800000;
34 constexpr int E_NOT_STAGE_MODE = 14801001;
35 constexpr int E_DATA_GROUP_ID_INVALID = 14801002;
36 constexpr int E_INVALID_FILE_PATH = 14800010;
37 
38 struct JsErrorCode {
39     int32_t status;
40     int32_t jsCode;
41     const char *message;
42 };
43 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode);
44 
45 #define RDB_REVT_NOTHING
46 #define RDB_DO_NOTHING
47 
48 #define RDB_NAPI_ASSERT_BASE(env, assertion, error, retVal)                                                     \
49     do {                                                                                                        \
50         if (!(assertion)) {                                                                                     \
51             if ((error) == nullptr) {                                                                           \
52                 LOG_ERROR("throw error: error message is empty");                                               \
53                 napi_throw_error((env), nullptr, "error message is empty");                                     \
54                 return retVal;                                                                                  \
55             }                                                                                                   \
56             LOG_ERROR("throw error: code = %{public}d , message = %{public}s", (error)->GetCode(),              \
57                 (error)->GetMessage().c_str());                                                                 \
58             napi_throw_error((env), std::to_string((error)->GetCode()).c_str(), (error)->GetMessage().c_str()); \
59             return retVal;                                                                                      \
60         }                                                                                                       \
61     } while (0)
62 
63 #define RDB_NAPI_ASSERT(env, assertion, error) RDB_NAPI_ASSERT_BASE(env, assertion, error, nullptr)
64 
65 #define CHECK_RETURN_CORE(assertion, theCall, revt) \
66     do {                                            \
67         if (!(assertion)) {                         \
68             theCall;                                \
69             return revt;                            \
70         }                                           \
71     } while (0)
72 
73 #define CHECK_RETURN_SET_E(assertion, paramError) \
74     CHECK_RETURN_CORE(assertion, context->SetError(paramError), RDB_REVT_NOTHING)
75 
76 #define CHECK_RETURN_SET(assertion, paramError) CHECK_RETURN_CORE(assertion, context->SetError(paramError), ERR)
77 
78 #define CHECK_RETURN_NULL(assertion) CHECK_RETURN_CORE(assertion, RDB_REVT_NOTHING, nullptr)
79 
80 #define CHECK_RETURN_ERR(assertion) CHECK_RETURN_CORE(assertion, RDB_REVT_NOTHING, ERR)
81 
82 #define CHECK_RETURN(assertion) CHECK_RETURN_CORE(assertion, RDB_REVT_NOTHING, RDB_REVT_NOTHING)
83 
84 class Error {
85 public:
~Error()86     virtual ~Error() {};
87     virtual std::string GetMessage() = 0;
88     virtual int GetCode() = 0;
89 };
90 
91 class InnerError : public Error {
92 public:
93     InnerError(int code, const std::string &msg = "")
94     {
95         auto errorMsg = GetJsErrorCode(code);
96         if (errorMsg.has_value()) {
97             auto napiError = errorMsg.value();
98             code_ = napiError.jsCode;
99             msg_ = napiError.message + msg;
100         } else {
101             code_ = E_INNER_ERROR;
102             msg_ = "Inner error.";
103         }
104     }
105 
InnerError(const std::string & msg)106     InnerError(const std::string &msg)
107     {
108         code_ = E_INNER_ERROR;
109         msg_ = std::string("Inner error. ") + msg;
110     }
111 
GetMessage()112     std::string GetMessage() override
113     {
114         return msg_;
115     }
116 
GetCode()117     int GetCode() override
118     {
119         return code_;
120     }
121 
122 private:
123     int code_;
124     std::string msg_;
125 };
126 
127 class ParamError : public Error {
128 public:
ParamError(const std::string & needed,const std::string & mustbe)129     ParamError(const std::string &needed, const std::string &mustbe)
130     {
131         msg_ = "Parameter error. The " + needed + " must be " + mustbe;
132     };
133 
ParamError(const std::string & errMsg)134     ParamError(const std::string &errMsg)
135     {
136         msg_ = "Parameter error." + errMsg;
137     }
138 
GetMessage()139     std::string GetMessage() override
140     {
141         return msg_;
142     };
143 
GetCode()144     int GetCode() override
145     {
146         return E_PARAM_ERROR;
147     };
148 
149 private:
150     std::string msg_;
151 };
152 
153 class NonSystemError : public Error {
154 public:
NonSystemError()155     NonSystemError()
156     {
157     }
GetMessage()158     std::string GetMessage() override
159     {
160         return "Permission verification failed, application which is not a system application uses system API.";
161     }
GetCode()162     int GetCode() override
163     {
164         return E_NON_SYSTEM_APP_ERROR;
165     }
166 };
167 
168 class ParamNumError : public Error {
169 public:
ParamNumError(const std::string & wantNum)170     ParamNumError(const std::string &wantNum) : wantNum(wantNum) {};
GetMessage()171     std::string GetMessage() override
172     {
173         return "Parameter error. Need " + wantNum + " parameter(s)!";
174     };
GetCode()175     int GetCode() override
176     {
177         return E_PARAM_ERROR;
178     };
179 
180 private:
181     std::string wantNum;
182 };
183 
184 class PathError : public Error {
185 public:
PathError()186     PathError(){};
GetMessage()187     std::string GetMessage() override
188     {
189         return "Failed to open or delete the database by an invalid database path.";
190     };
GetCode()191     int GetCode() override
192     {
193         return E_INVALID_FILE_PATH;
194     };
195 };
196 } // namespace RelationalStoreJsKit
197 } // namespace OHOS
198 
199 #endif // RDB_JS_NAPI_ERROR_H
200