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