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 16 #ifndef USB_NAPI_ERRORS_H 17 #define USB_NAPI_ERRORS_H 18 19 #include <map> 20 #include <string_view> 21 #include "napi/native_api.h" 22 23 namespace OHOS { 24 namespace USB { 25 enum UsbJsErrCode : int32_t { 26 SYSPARAM_INVALID_INPUT = 401, 27 USB_DEVICE_PERMISSION_DENIED = 14400001, 28 }; 29 30 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = { 31 {SYSPARAM_INVALID_INPUT, "BusinessError 401:Parameter error." }, 32 {USB_DEVICE_PERMISSION_DENIED, "BusinessError 14400001:Permission denied."}, 33 }; 34 35 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 36 37 #define USB_ASSERT_BASE(env, assertion, errCode, errMsg, retVal) \ 38 do { \ 39 if (!(assertion)) { \ 40 USB_HILOGE(MODULE_JS_NAPI, #errMsg); \ 41 ThrowBusinessError((env), errCode, errMsg); \ 42 return retVal; \ 43 } \ 44 } while (0) 45 46 #define NOTHING 47 #define USB_ASSERT(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, nullptr) 48 #define USB_ASSERT_RETURN_VOID(env, assertion, errCode, errMsg) \ 49 USB_ASSERT_BASE(env, assertion, errCode, errMsg, NOTHING) 50 #define USB_ASSERT_RETURN_FALSE(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, false) 51 52 #define NAPI_CHECK(env, theCall, loginfo) \ 53 do { \ 54 if ((theCall) != napi_ok) { \ 55 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 56 napi_value obj = nullptr; \ 57 napi_get_undefined(env, &obj); \ 58 return obj; \ 59 } \ 60 } while (0) 61 62 #define NAPI_CHECK_BASE(theCall, loginfo, retVal) \ 63 do { \ 64 if ((theCall) != napi_ok) { \ 65 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 66 return retVal; \ 67 } \ 68 } while (0) 69 70 #define NAPI_CHECK_RETURN_VOID(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, NOTHING) 71 #define NAPI_CHECK_RETURN_FALSE(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, false) 72 } // namespace USB 73 } // namespace OHOS 74 #endif // USB_NAPI_ERRORS_H 75