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 PRE_JS_NAPI_ERROR_H
16 #define PRE_JS_NAPI_ERROR_H
17 
18 #include <map>
19 #include <optional>
20 
21 #include "log_print.h"
22 #include "preferences_errno.h"
23 
24 namespace OHOS {
25 namespace PreferencesJsKit {
26 constexpr int MAX_INPUT_COUNT = 10;
27 constexpr int OK = 0;
28 constexpr int ERR = -1;
29 constexpr int EXCEED_MAX_LENGTH = -2;
30 constexpr int NAPI_TYPE_ERROR = -3;
31 
32 constexpr int E_INVALID_PARAM = 401;
33 constexpr int E_INNER_ERROR = 15500000;
34 constexpr int E_NOT_STAGE_MODE = 15501001;
35 constexpr int E_DATA_GROUP_ID_INVALID = 15501002;
36 constexpr int E_NOT_SUPPORTED = 801;
37 constexpr int E_GET_DATAOBSMGRCLIENT_FAIL = 15500019;
38 constexpr int E_DELETE_FILE_FAIL = 15500010;
39 
40 struct JsErrorCode {
41     int32_t nativeCode;
42     int32_t jsCode;
43     const char *message;
44 };
45 
46 const std::optional<JsErrorCode> GetJsErrorCode(int32_t errorCode);
47 
48 #define PRE_REVT_NOTHING
49 
50 #define PRE_NAPI_ASSERT_BASE(env, assertion, error, retVal)                        \
51     do {                                                                           \
52         if (!(assertion)) {                                                        \
53             LOG_ERROR("throw error: code = %{public}d , message = %{public}s",     \
54                       (error)->GetCode(), (error)->GetMsg().c_str());          \
55             napi_throw_error((env), std::to_string((error)->GetCode()).c_str(),    \
56                              (error)->GetMsg().c_str());                       \
57             return retVal;                                                         \
58         }                                                                          \
59     } while (0)
60 
61 #define PRE_NAPI_ASSERT(env, assertion, error) PRE_NAPI_ASSERT_BASE(env, assertion, error, nullptr)
62 
63 #define PRE_NAPI_ASSERT_RETURN_VOID(env, assertion, error) \
64     PRE_NAPI_ASSERT_BASE(env, assertion, error, NAPI_RETVAL_NOTHING)
65 
66 #define PRE_CHECK_RETURN_CORE(assertion, theCall, revt)  \
67     do {                                                 \
68         if (!(assertion)) {                              \
69             theCall;                                     \
70             return revt;                                 \
71         }                                                \
72     } while (0)
73 
74 #define PRE_CHECK_RETURN_VOID_SET(assertion, error) \
75     PRE_CHECK_RETURN_CORE(assertion, context->SetError(error), PRE_REVT_NOTHING)
76 
77 #define PRE_CHECK_RETURN_ERR_SET(assertion, error) \
78     PRE_CHECK_RETURN_CORE(assertion, context->SetError(error), ERR)
79 
80 #define PRE_CHECK_RETURN_NULL(assertion) \
81     PRE_CHECK_RETURN_CORE(assertion, PRE_REVT_NOTHING, nullptr)
82 
83 #define PRE_CHECK_RETURN_VOID(assertion) \
84     PRE_CHECK_RETURN_CORE(assertion, PRE_REVT_NOTHING, PRE_REVT_NOTHING)
85 
86 #define PRE_CHECK_RETURN_ERR(assertion) \
87     PRE_CHECK_RETURN_CORE(assertion, PRE_REVT_NOTHING, ERR)
88 
89 
90 class JSError {
91 public:
~JSError()92     virtual ~JSError(){};
93     virtual std::string GetMsg() = 0;
94     virtual int GetCode() = 0;
95 };
96 
97 class ParamTypeError : public JSError {
98 public:
ParamTypeError(const std::string & errmsg)99     ParamTypeError(const std::string &errmsg) : errmsg_(errmsg){};
GetMsg()100     std::string GetMsg() override
101     {
102         return "Parameter error. " + errmsg_;
103     };
GetCode()104     int GetCode() override
105     {
106         return E_INVALID_PARAM;
107     };
108 
109 private:
110     std::string errmsg_;
111 };
112 
113 class InnerError : public JSError {
114 public:
InnerError(const std::string & msg)115     InnerError(const std::string &msg)
116     {
117         code_ = E_INNER_ERROR;
118         msg_ = "Inner error. " + msg;
119     }
120 
InnerError(int code)121     InnerError(int code)
122     {
123         auto errorMsg = GetJsErrorCode(code);
124         if (errorMsg.has_value()) {
125             auto napiError = errorMsg.value();
126             code_ = napiError.jsCode;
127             msg_ = napiError.message;
128         } else {
129             code_ = E_INNER_ERROR;
130             msg_ = "Inner error. Error code " + std::to_string(code);
131         }
132     }
133 
GetMsg()134     std::string GetMsg() override
135     {
136         return msg_;
137     }
138 
GetCode()139     int GetCode() override
140     {
141         return code_;
142     }
143 private:
144     int code_;
145     std::string msg_;
146 };
147 
148 class ParamNumError : public JSError {
149 public:
ParamNumError(const std::string & wantNum)150     ParamNumError(const std::string &wantNum) : wantNum(wantNum){};
GetMsg()151     std::string GetMsg() override
152     {
153         return "Parameter error. Need " + wantNum + " parameters!";
154     };
GetCode()155     int GetCode() override
156     {
157         return E_INVALID_PARAM;
158     };
159 
160 private:
161     std::string apiname;
162     std::string wantNum;
163 };
164 } // namespace PreferencesJsKit
165 } // namespace OHOS
166 
167 #endif // PRE_JS_NAPI_ERROR_H
168