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 "js_logger.h" 19 20 namespace OHOS { 21 namespace PreferencesJsKit { 22 constexpr int MAX_INPUT_COUNT = 10; 23 constexpr int OK = 0; 24 constexpr int ERR = -1; 25 26 constexpr int E_PARAM_ERROR = 401; 27 constexpr int E_PREFERENCES_ERROR = 15500010; 28 29 #define PRE_NAPI_ASSERT_BASE(env, assertion, error, retVal) \ 30 do { \ 31 if (!(assertion)) { \ 32 LOG_ERROR("throw error: code = %{public}d , message = %{public}s", \ 33 (error)->GetCode(), (error)->GetMessage().c_str()); \ 34 napi_throw_error((env), std::to_string((error)->GetCode()).c_str(), \ 35 (error)->GetMessage().c_str()); \ 36 return retVal; \ 37 } \ 38 } while (0) 39 40 #define PRE_NAPI_ASSERT(env, assertion, error) PRE_NAPI_ASSERT_BASE(env, assertion, error, nullptr) 41 42 #define PRE_NAPI_ASSERT_RETURN_VOID(env, assertion, error) \ 43 PRE_NAPI_ASSERT_BASE(env, assertion, error, NAPI_RETVAL_NOTHING) 44 45 #define PRE_ASYNC_PARAM_CHECK_FUNCTION(theCall) \ 46 do { \ 47 int err = (theCall); \ 48 if (err != OK) { \ 49 return err; \ 50 } \ 51 } while (0) 52 53 #define PRE_CHECK_RETURN_NULLPTR(context, assertion) \ 54 do { \ 55 if (!(assertion)) { \ 56 /* avoid cyclic dependence */ \ 57 (context)->exec_ = nullptr; \ 58 (context)->output_ = nullptr; \ 59 return nullptr; \ 60 } \ 61 } while (0) 62 63 #define PRE_CHECK_RETURN_CALL_RESULT(assertion, theCall) \ 64 do { \ 65 if (!(assertion)) { \ 66 (theCall); \ 67 return ERR; \ 68 } \ 69 } while (0) 70 71 #define PRE_NAPI_ASSERT_RETURN_VOID(env, assertion, error) \ 72 PRE_NAPI_ASSERT_BASE(env, assertion, error, NAPI_RETVAL_NOTHING) 73 74 class Error { 75 public: ~Error()76 virtual ~Error(){}; 77 virtual std::string GetMessage() = 0; 78 virtual int GetCode() = 0; 79 }; 80 81 class ParamTypeError : public Error { 82 public: ParamTypeError(const std::string & name,const std::string & wantType)83 ParamTypeError(const std::string &name, const std::string &wantType) : name(name), wantType(wantType){}; GetMessage()84 std::string GetMessage() override 85 { 86 return "Parameter error. The type of '" + name + "' must be " + wantType; 87 }; GetCode()88 int GetCode() override 89 { 90 return E_PARAM_ERROR; 91 }; 92 93 private: 94 std::string name; 95 std::string wantType; 96 }; 97 98 class ParamNumError : public Error { 99 public: ParamNumError(const std::string & wantNum)100 ParamNumError(const std::string &wantNum) : wantNum(wantNum){}; GetMessage()101 std::string GetMessage() override 102 { 103 return "Parameter error. Need " + wantNum + " parameters!"; 104 }; GetCode()105 int GetCode() override 106 { 107 return E_PARAM_ERROR; 108 }; 109 110 private: 111 std::string apiname; 112 std::string wantNum; 113 }; 114 115 class DeleteError : public Error { 116 public: 117 DeleteError() = default; GetMessage()118 std::string GetMessage() override 119 { 120 return "Failed to delete preferences file."; 121 }; GetCode()122 int GetCode() override 123 { 124 return E_PREFERENCES_ERROR; 125 }; 126 127 private: 128 std::string apiname; 129 }; 130 } // namespace PreferencesJsKit 131 } // namespace OHOS 132 133 #endif // PRE_JS_NAPI_ERROR_H 134