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 USB_SYSAPI_PERMISSION_DENIED = 202, 29 USB_HDC_PERMISSION_DENIED = 14400002, 30 USB_NOT_SUPPORT_SWITCH_PORT = 14400003, 31 }; 32 33 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = { 34 {SYSPARAM_INVALID_INPUT, "BusinessError 401:Parameter error." }, 35 {USB_DEVICE_PERMISSION_DENIED, "BusinessError 14400001:Permission denied." }, 36 {USB_SYSAPI_PERMISSION_DENIED, "BusinessError 202:Permission denied. Normal application uses system api." }, 37 {USB_HDC_PERMISSION_DENIED, "BusinessError 14400002:Permission denied. The HDC is disabled by the system."}, 38 {USB_NOT_SUPPORT_SWITCH_PORT, 39 "BusinessError 14400003:Unsupported operation.The current device does not support port role switching." }, 40 }; 41 42 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 43 napi_value CreateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg); 44 45 #define USB_ASSERT_BASE(env, assertion, errCode, errMsg, retVal) \ 46 do { \ 47 if (!(assertion)) { \ 48 USB_HILOGE(MODULE_JS_NAPI, #errMsg); \ 49 ThrowBusinessError((env), errCode, errMsg); \ 50 return retVal; \ 51 } \ 52 } while (0) 53 54 #define NOTHING 55 #define USB_ASSERT(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, nullptr) 56 #define USB_ASSERT_RETURN_VOID(env, assertion, errCode, errMsg) \ 57 USB_ASSERT_BASE(env, assertion, errCode, errMsg, NOTHING) 58 #define USB_ASSERT_RETURN_FALSE(env, assertion, errCode, errMsg) USB_ASSERT_BASE(env, assertion, errCode, errMsg, false) 59 #define USB_ASSERT_RETURN_UNDEF(env, assertion, errCode, errMsg) \ 60 do { \ 61 napi_value obj = nullptr; \ 62 napi_get_undefined(env, &obj); \ 63 USB_ASSERT_BASE(env, assertion, errCode, errMsg, obj); \ 64 } while (0) 65 66 #define NAPI_CHECK(env, theCall, loginfo) \ 67 do { \ 68 if ((theCall) != napi_ok) { \ 69 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 70 napi_value obj = nullptr; \ 71 napi_get_undefined(env, &obj); \ 72 return obj; \ 73 } \ 74 } while (0) 75 76 #define NAPI_CHECK_BASE(theCall, loginfo, retVal) \ 77 do { \ 78 if ((theCall) != napi_ok) { \ 79 USB_HILOGE(MODULE_JS_NAPI, "%{public}s " #loginfo " ", __func__); \ 80 return retVal; \ 81 } \ 82 } while (0) 83 84 #define NAPI_CHECK_RETURN_VOID(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, NOTHING) 85 #define NAPI_CHECK_RETURN_FALSE(theCall, loginfo) NAPI_CHECK_BASE(theCall, loginfo, false) 86 } // namespace USB 87 } // namespace OHOS 88 #endif // USB_NAPI_ERRORS_H 89