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