/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef UTIL_NAPI_ERROR_H #define UTIL_NAPI_ERROR_H #include #include "securec.h" #include "util_napi.h" namespace OHOS { namespace MMI { const std::string ERR_CODE = "code"; struct NapiError { int32_t errorCode; std::string msg; }; enum NapiErrorCode : int32_t { OTHER_ERROR = -1, COMMON_PERMISSION_CHECK_ERROR = 201, COMMON_PARAMETER_ERROR = 401, COMMON_USE_SYSAPI_ERROR = 202, INPUT_DEVICE_NOT_SUPPORTED = 801, INPUT_OCCUPIED_BY_SYSTEM = 4200002, INPUT_OCCUPIED_BY_OTHER = 4200003, PRE_KEY_NOT_SUPPORTED = 4100001, COMMON_DEVICE_NOT_EXIST = 3900001, COMMON_KEYBOARD_DEVICE_NOT_EXIST = 3900002, COMMON_NON_INPUT_APPLICATION = 3900003, ERROR_WINDOW_ID_PERMISSION_DENIED = 26500001, }; const std::map NAPI_ERRORS = { { COMMON_PERMISSION_CHECK_ERROR, { COMMON_PERMISSION_CHECK_ERROR, "Permission denied. An attempt was made to %s forbidden by permission:%s." } }, { COMMON_USE_SYSAPI_ERROR, { COMMON_USE_SYSAPI_ERROR, "Permission denied, non-system application called system api." } }, { COMMON_PARAMETER_ERROR, { COMMON_PARAMETER_ERROR, "Parameter error. The type of %s must be %s." } }, { COMMON_DEVICE_NOT_EXIST, { COMMON_DEVICE_NOT_EXIST, "The specified device does not exist." } }, { COMMON_KEYBOARD_DEVICE_NOT_EXIST, { COMMON_KEYBOARD_DEVICE_NOT_EXIST, "The specified keyboard device does not exist." } }, { COMMON_NON_INPUT_APPLICATION, { COMMON_NON_INPUT_APPLICATION, "it is prohibited for non-input applications." } }, { INPUT_DEVICE_NOT_SUPPORTED, { INPUT_DEVICE_NOT_SUPPORTED, "Capability not supported.\n" } }, { ERROR_WINDOW_ID_PERMISSION_DENIED, { ERROR_WINDOW_ID_PERMISSION_DENIED, "Invalid windowId. Possible causes: The window id does not belong to the current process.\n" } }, { PRE_KEY_NOT_SUPPORTED, { PRE_KEY_NOT_SUPPORTED, "Invalid combination of keys." } }, { INPUT_OCCUPIED_BY_SYSTEM, { INPUT_OCCUPIED_BY_SYSTEM, "The hotkey has been subscribed by system." } }, { INPUT_OCCUPIED_BY_OTHER, { INPUT_OCCUPIED_BY_OTHER, "The hotkey has been subscribed by other one." } }, }; #define THROWERR_CUSTOM(env, code, msg) \ do { \ napi_value businessError = nullptr; \ napi_value errorCode = nullptr; \ napi_value errorMsg = nullptr; \ napi_create_int32(env, code, &errorCode); \ napi_create_string_utf8(env, std::string(msg).c_str(), NAPI_AUTO_LENGTH, &errorMsg); \ napi_create_error(env, nullptr, errorMsg, &businessError); \ napi_set_named_property(env, businessError, ERR_CODE.c_str(), errorCode); \ napi_throw(env, businessError); \ } while (0) #define THROWERR_API9(env, code, param1, param2) \ do { \ MMI_HILOGE("ErrorCode:%{public}s", (#code)); \ NapiError codeMsg; \ if (UtilNapiError::GetApiError(code, codeMsg)) { \ char buf[300]; \ if (sprintf_s(buf, sizeof(buf), codeMsg.msg.c_str(), param1, param2) > 0) { \ THROWERR_CUSTOM(env, code, buf); \ } else { \ MMI_HILOGE("Failed to convert string type to char type"); \ } \ } \ } while (0) namespace UtilNapiError { bool GetApiError(int32_t code, NapiError &codeMsg); } // namespace UtilNapiError } // namespace MMI } // namespace OHOS #endif // UTIL_NAPI_ERROR_H