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 UTIL_NAPI_ERROR_H 17 #define UTIL_NAPI_ERROR_H 18 19 #include <map> 20 #include <string> 21 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 25 #include "securec.h" 26 #include "utils/log.h" 27 28 #include "util_napi.h" 29 30 namespace OHOS { 31 namespace MMI { 32 const std::string ERR_CODE = "code"; 33 struct NapiError { 34 int32_t errorCode; 35 std::string msg; 36 }; 37 38 enum NapiErrorCode : int32_t { 39 OTHER_ERROR = -1, 40 COMMON_PERMISSION_CHECK_ERROR = 201, 41 COMMON_PARAMETER_ERROR = 401, 42 COMMON_USE_SYSAPI_ERROR = 202, 43 COMMON_CAPABILITY_NOT_SUPPORTED = 801, 44 COMMON_DEVICE_NOT_EXIST = 3900001, 45 COMMON_KEYBOARD_DEVICE_NOT_EXIST = 3900002, 46 COMMON_NON_INPUT_APPLICATION = 3900003, 47 PRE_KEY_NOT_SUPPORTED = 4100001, 48 INPUT_OCCUPIED_BY_SYSTEM = 4200002, 49 INPUT_OCCUPIED_BY_OTHER = 4200003, 50 ERROR_WINDOW_ID_PERMISSION_DENIED = 26500001, 51 }; 52 53 const std::map<int32_t, NapiError> NAPI_ERRORS = { 54 { COMMON_PERMISSION_CHECK_ERROR, 55 { COMMON_PERMISSION_CHECK_ERROR, "Permission denied. An attempt was made to %s forbidden by permission:%s." } }, 56 { COMMON_USE_SYSAPI_ERROR, 57 { COMMON_USE_SYSAPI_ERROR, "Permission denied, non-system application called system api." } }, 58 { COMMON_PARAMETER_ERROR, { COMMON_PARAMETER_ERROR, "Parameter error. The type of %s must be %s." } }, 59 { COMMON_DEVICE_NOT_EXIST, { COMMON_DEVICE_NOT_EXIST, "The specified device does not exist." } }, 60 { COMMON_CAPABILITY_NOT_SUPPORTED, { COMMON_CAPABILITY_NOT_SUPPORTED, "Capability not supported.\n" } }, 61 { COMMON_KEYBOARD_DEVICE_NOT_EXIST, 62 { COMMON_KEYBOARD_DEVICE_NOT_EXIST, "The specified keyboard device does not exist." } }, 63 { COMMON_NON_INPUT_APPLICATION, { COMMON_NON_INPUT_APPLICATION, "it is prohibited for non-input applications." } }, 64 { ERROR_WINDOW_ID_PERMISSION_DENIED, { ERROR_WINDOW_ID_PERMISSION_DENIED, "windowId is invalid.\n" } }, 65 { PRE_KEY_NOT_SUPPORTED, { PRE_KEY_NOT_SUPPORTED, "Invalid combination of keys." } }, 66 { INPUT_OCCUPIED_BY_SYSTEM, { INPUT_OCCUPIED_BY_SYSTEM, "The hotkey has been subscribed by system." } }, 67 { INPUT_OCCUPIED_BY_OTHER, { INPUT_OCCUPIED_BY_OTHER, "The hotkey has been subscribed by other one." } }, 68 }; 69 70 #define THROWERR_CUSTOM(env, code, msg) \ 71 do { \ 72 napi_value businessError = nullptr; \ 73 napi_value errorCode = nullptr; \ 74 napi_value errorMsg = nullptr; \ 75 napi_create_int32(env, code, &errorCode); \ 76 napi_create_string_utf8(env, std::string(msg).c_str(), NAPI_AUTO_LENGTH, &errorMsg); \ 77 napi_create_error(env, nullptr, errorMsg, &businessError); \ 78 napi_set_named_property(env, businessError, ERR_CODE.c_str(), errorCode); \ 79 napi_throw(env, businessError); \ 80 } while (0) 81 82 #define THROWERR_API9(env, code, param1, param2) \ 83 do { \ 84 MMI_HILOGE("ErrorCode:%{public}s", (#code)); \ 85 NapiError codeMsg; \ 86 if (UtilNapiError::GetApiError(code, codeMsg)) { \ 87 char buf[300]; \ 88 if (sprintf_s(buf, sizeof(buf), codeMsg.msg.c_str(), param1, param2) > 0) { \ 89 THROWERR_CUSTOM(env, code, buf); \ 90 } else { \ 91 MMI_HILOGE("Failed to convert string type to char type"); \ 92 } \ 93 } \ 94 } while (0) 95 namespace UtilNapiError { 96 bool GetApiError(int32_t code, NapiError& codeMsg); 97 } // namespace UtilNapiError 98 } // namespace MMI 99 } // namespace OHOS 100 #endif // UTIL_NAPI_ERROR_H 101