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