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(int code)111 InnerError(int code) 112 { 113 auto iter = ERROR_MAPS.find(code); 114 if (iter != ERROR_MAPS.end()) { 115 code_ = code; 116 msg_ = iter->second; 117 } else { 118 code_ = E_INNER_ERROR; 119 msg_ = "Inner error. Error code " + std::to_string(code); 120 } 121 } 122 GetMsg()123 std::string GetMsg() override 124 { 125 return msg_; 126 } 127 GetCode()128 int GetCode() override 129 { 130 return code_; 131 } 132 private: 133 int code_; 134 std::string msg_; 135 }; 136 137 class ParamNumError : public JSError { 138 public: ParamNumError(const std::string & wantNum)139 ParamNumError(const std::string &wantNum) : wantNum(wantNum){}; GetMsg()140 std::string GetMsg() override 141 { 142 return "Parameter error. Need " + wantNum + " parameters!"; 143 }; GetCode()144 int GetCode() override 145 { 146 return E_INVALID_PARAM; 147 }; 148 149 private: 150 std::string apiname; 151 std::string wantNum; 152 }; 153 } // namespace PreferencesJsKit 154 } // namespace OHOS 155 156 #endif // PRE_JS_NAPI_ERROR_H 157