/* * Copyright (C) 2021-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. */ #include "napi_bluetooth_error.h" #include "bluetooth_errorcode.h" #include "napi_bluetooth_utils.h" namespace OHOS { namespace Bluetooth { static std::map napiErrMsgMap { { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "Service stoped." }, { BtErrCode::BT_ERR_UNBONDED_DEVICE, "Device is not bonded." }, { BtErrCode::BT_ERR_INVALID_STATE, "Bluetooth switch is off." }, { BtErrCode::BT_ERR_PROFILE_DISABLED, "Profile is not supported." }, { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "Device is disconnected" }, { BtErrCode::BT_ERR_MAX_CONNECTION, "Max number connection." }, { BtErrCode::BT_ERR_INTERNAL_ERROR, "Operation failed" }, { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "trans exception." }, { BtErrCode::BT_ERR_PERMISSION_FAILED, "Permission denied." }, { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "Non-system applications are not allowed to use system APIs."}, { BtErrCode::BT_ERR_INVALID_PARAM, "Invalid parameter." }, { BtErrCode::BT_ERR_API_NOT_SUPPORT, "Capability is not supported." }, { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "Gatt read forbiden." }, { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "Gatt write forbiden." }, { BtErrCode::BT_ERR_GATT_MAX_SERVER, "Max gatt server." }, { BtErrCode::BT_ERR_SPP_SERVER_STATE, "SPP server not running." }, { BtErrCode::BT_ERR_SPP_BUSY, "SPP translate busy." }, { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "Device is not inquired." }, { BtErrCode::BT_ERR_SPP_IO, "SPP IO error." }, }; static std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode) { auto iter = napiErrMsgMap.find(errCode); if (iter != napiErrMsgMap.end()) { std::string errMessage = "BussinessError "; errMessage.append(std::to_string(errCode)).append(": ").append(iter->second); return errMessage; } return "Inner error."; } void HandleSyncErr(const napi_env &env, int32_t errCode) { if (errCode == BtErrCode::BT_SUCCESS) { return; } std::string errMsg = GetNapiErrMsg(env, errCode); if (errMsg != "") { napi_throw_error(env, std::to_string(errCode).c_str(), errMsg.c_str()); } } } }