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 "logger.h" 19 20 namespace OHOS { 21 namespace AppDataMgrJsKit { 22 constexpr int MAX_INPUT_COUNT = 10; 23 constexpr int OK = 0; 24 constexpr int ERR = -1; 25 constexpr int APIVERSION_V9 = 9; 26 constexpr int APIVERSION_8 = 8; 27 28 constexpr int E_PARAM_ERROR = 401; 29 constexpr int E_INNER_ERROR = 14800000; 30 31 constexpr int E_DB_INVALID = 14800010; 32 constexpr int E_DB_CORRUPTED = 14800011; 33 constexpr int E_RESULT_GET_ERROR = 14800013; 34 constexpr int E_RESULT_GOTO_ERROR = 14800012; 35 36 #define RDB_NAPI_ASSERT_BASE_FROMV9(env, assertion, error, retVal, version) \ 37 do { \ 38 if (!(assertion)) { \ 39 if ((error) == nullptr) { \ 40 LOG_ERROR("throw error: error message is empty,version= %{public}d", version); \ 41 napi_throw_error((env), nullptr, "error message is empty"); \ 42 return retVal; \ 43 } \ 44 if (((version) > (APIVERSION_8)) || (((error)->GetCode()) == (401))) { \ 45 LOG_ERROR("throw error: code = %{public}d , message = %{public}s, version= %{public}d", \ 46 (error)->GetCode(), (error)->GetMessage().c_str(), version); \ 47 napi_throw_error((env), std::to_string((error)->GetCode()).c_str(), (error)->GetMessage().c_str()); \ 48 return retVal; \ 49 } \ 50 LOG_ERROR("nothrow error: code = %{public}d , message = %{public}s, version= %{public}d", \ 51 (error)->GetCode(), (error)->GetMessage().c_str(), version); \ 52 } \ 53 } while (0) 54 55 #define RDB_REVT_NOTHING 56 57 #define RDB_NAPI_ASSERT_FROMV9(env, assertion, error, version) \ 58 RDB_NAPI_ASSERT_BASE_FROMV9(env, assertion, error, nullptr, version) 59 60 #define RDB_NAPI_ASSERT_RETURN_VOID_FROMV9(env, assertion, error, version) \ 61 RDB_NAPI_ASSERT_BASE_FROMV9(env, assertion, error, NAPI_RETVAL_NOTHING, version) 62 63 #define RDB_ASYNC_PARAM_CHECK_FUNCTION(theCall) \ 64 do { \ 65 int err = (theCall); \ 66 if (err != OK) { \ 67 return err; \ 68 } \ 69 } while (0) 70 71 #define RDB_CHECK_RETURN_NULLPTR(assertion, message) \ 72 do { \ 73 if (!(assertion)) { \ 74 LOG_ERROR("%{public}s", message); \ 75 return nullptr; \ 76 } \ 77 } while (0) 78 79 #define RDB_CHECK_RETURN_VOID(assertion, message) \ 80 do { \ 81 if (!(assertion)) { \ 82 LOG_ERROR("%{public}s", message); \ 83 return; \ 84 } \ 85 } while (0) 86 87 #define CHECK_RETURN_CORE(assertion, theCall, revt) \ 88 do { \ 89 if (!(assertion)) { \ 90 theCall; \ 91 return revt; \ 92 } \ 93 } while (0) 94 95 #define CHECK_RETURN_ERR(assertion) \ 96 CHECK_RETURN_CORE(assertion, RDB_REVT_NOTHING, ERR) 97 98 #define RDB_CHECK_RETURN_CALL_RESULT(assertion, theCall) \ 99 do { \ 100 if (!(assertion)) { \ 101 (theCall); \ 102 return ERR; \ 103 } \ 104 } while (0) 105 106 class Error { 107 public: ~Error()108 virtual ~Error(){}; 109 virtual std::string GetMessage() = 0; 110 virtual int GetCode() = 0; 111 }; 112 113 class InnerError : public Error { 114 public: 115 InnerError() = default; GetMessage()116 std::string GetMessage() override 117 { 118 return "System error."; 119 }; GetCode()120 int GetCode() override 121 { 122 return E_INNER_ERROR; 123 }; 124 }; 125 126 class ParamTypeError : public Error { 127 public: ParamTypeError(const std::string & name,const std::string & wantType)128 ParamTypeError(const std::string &name, const std::string &wantType) : name(name), wantType(wantType){}; GetMessage()129 std::string GetMessage() override 130 { 131 return "Parameter error. The type of '" + name + "' must be " + wantType; 132 }; GetCode()133 int GetCode() override 134 { 135 return E_PARAM_ERROR; 136 }; 137 138 private: 139 std::string name; 140 std::string wantType; 141 }; 142 143 class ParamNumError : public Error { 144 public: ParamNumError(const std::string & wantNum)145 ParamNumError(const std::string &wantNum) : wantNum(wantNum){}; GetMessage()146 std::string GetMessage() override 147 { 148 return "Parameter error. Need " + wantNum + " parameters!"; 149 }; GetCode()150 int GetCode() override 151 { 152 return E_PARAM_ERROR; 153 }; 154 155 private: 156 std::string wantNum; 157 }; 158 159 class DbInvalidError : public Error { 160 public: 161 DbInvalidError() = default; GetMessage()162 std::string GetMessage() override 163 { 164 return "Failed open database, invalid database name."; 165 }; GetCode()166 int GetCode() override 167 { 168 return E_DB_INVALID; 169 }; 170 }; 171 172 class DbCorruptedError : public Error { 173 public: 174 DbCorruptedError() = default; GetMessage()175 std::string GetMessage() override 176 { 177 return "Failed open database, database corrupted."; 178 }; GetCode()179 int GetCode() override 180 { 181 return E_DB_CORRUPTED; 182 }; 183 }; 184 185 class ResultGetError : public Error { 186 public: 187 ResultGetError() = default; GetMessage()188 std::string GetMessage() override 189 { 190 return "The column value is null or the column type is incompatible."; 191 }; GetCode()192 int GetCode() override 193 { 194 return E_RESULT_GET_ERROR; 195 }; 196 }; 197 198 class ResultGotoError : public Error { 199 public: 200 ResultGotoError() = default; GetMessage()201 std::string GetMessage() override 202 { 203 return "The result set is empty or the specified location is invalid."; 204 }; GetCode()205 int GetCode() override 206 { 207 return E_RESULT_GOTO_ERROR; 208 }; 209 }; 210 } // namespace AppDataMgrJsKit 211 } // namespace OHOS 212 213 #endif // RDB_JS_NAPI_ERROR_H 214