1 /* 2 * Copyright (c) 2022-2023 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 #include "securec.h" 25 26 #include "devicestatus_errors.h" 27 #include "util_napi.h" 28 29 namespace OHOS { 30 namespace Msdp { 31 namespace DeviceStatus { 32 const std::string ERR_CODE { "code" }; 33 34 const std::map<int32_t, std::string> NAPI_ERRORS = { 35 { COMMON_PERMISSION_CHECK_ERROR, "Permission denied. An attempt was made to %s forbidden by permission:%s." }, 36 { COMMON_PARAMETER_ERROR, "Parameter error. The type of %s must be %s." }, 37 { COMMON_NOT_ALLOWED_DISTRIBUTED, "Cross-device dragging is not allowed" }, 38 { COOPERATOR_FAIL, " Service exception. Possible causes: 1. A system error, such as null pointer," 39 "container-related exception, or IPC exception. 2. N-API invocation exception or invalid N-API status." }, 40 { COMMON_NOT_SYSTEM_APP, "Non system applications." } 41 }; 42 43 #define THROWERR_CUSTOM(env, code, msg) \ 44 do { \ 45 napi_value businessError = nullptr; \ 46 napi_value errorMsg = nullptr; \ 47 napi_value errorCode = nullptr; \ 48 napi_create_int32(env, code, &errorCode); \ 49 napi_create_string_utf8(env, std::string(msg).c_str(), NAPI_AUTO_LENGTH, &errorMsg); \ 50 napi_create_error(env, nullptr, errorMsg, &businessError); \ 51 napi_set_named_property(env, businessError, ERR_CODE.c_str(), errorCode); \ 52 napi_throw(env, businessError); \ 53 FI_HILOGE("Raising exceptions, errorCode:%{public}d, errorMsg:%{public}s", \ 54 static_cast<int32_t>(code), std::string(msg).c_str()); \ 55 } while (0) 56 57 #define THROWERR(env, code, param1, param2) \ 58 do { \ 59 std::string codeMsg; \ 60 if (UtilNapiError::GetErrorMsg(code, codeMsg)) { \ 61 char buf[300] = { 0 }; \ 62 int32_t ret = sprintf_s(buf, sizeof(buf), codeMsg.c_str(), param1, param2); \ 63 if (ret > 0) { \ 64 THROWERR_CUSTOM(env, code, buf); \ 65 } else { \ 66 FI_HILOGE("Failed to convert string type to char type, error code:%{public}d", \ 67 static_cast<int32_t>(code)); \ 68 } \ 69 } \ 70 } while (0) 71 72 namespace UtilNapiError { 73 bool GetErrorMsg(int32_t code, std::string &codeMsg); 74 void HandleExecuteResult(napi_env env, int32_t errCode, const std::string param1 = "", const std::string param2 = ""); 75 } // namespace UtilNapiError 76 } // namespace DeviceStatus 77 } // namespace Msdp 78 } // namespace OHOS 79 #endif // UTIL_NAPI_ERROR_H 80