1 /*
2 * Copyright (C) 2021-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 #include "napi_bluetooth_error.h"
17
18 #include "bluetooth_errorcode.h"
19 #include "napi_bluetooth_utils.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
23
24 static std::map<int32_t, std::string> napiErrMsgMap {
25 { BtErrCode::BT_ERR_SERVICE_DISCONNECTED, "Service stoped." },
26 { BtErrCode::BT_ERR_UNBONDED_DEVICE, "Device is not bonded." },
27 { BtErrCode::BT_ERR_INVALID_STATE, "Bluetooth switch is off." },
28 { BtErrCode::BT_ERR_PROFILE_DISABLED, "Profile is not supported." },
29 { BtErrCode::BT_ERR_DEVICE_DISCONNECTED, "Device is disconnected" },
30 { BtErrCode::BT_ERR_MAX_CONNECTION, "Max number connection." },
31 { BtErrCode::BT_ERR_INTERNAL_ERROR, "Operation failed" },
32 { BtErrCode::BT_ERR_IPC_TRANS_FAILED, "trans exception." },
33 { BtErrCode::BT_ERR_PERMISSION_FAILED, "Permission denied." },
34 { BtErrCode::BT_ERR_SYSTEM_PERMISSION_FAILED, "Non-system applications are not allowed to use system APIs."},
35 { BtErrCode::BT_ERR_INVALID_PARAM, "Invalid parameter." },
36 { BtErrCode::BT_ERR_API_NOT_SUPPORT, "Capability is not supported." },
37 { BtErrCode::BT_ERR_GATT_READ_NOT_PERMITTED, "Gatt read forbiden." },
38 { BtErrCode::BT_ERR_GATT_WRITE_NOT_PERMITTED, "Gatt write forbiden." },
39 { BtErrCode::BT_ERR_GATT_MAX_SERVER, "Max gatt server." },
40 { BtErrCode::BT_ERR_SPP_SERVER_STATE, "SPP server not running." },
41 { BtErrCode::BT_ERR_SPP_BUSY, "SPP translate busy." },
42 { BtErrCode::BT_ERR_SPP_DEVICE_NOT_FOUND, "Device is not inquired." },
43 { BtErrCode::BT_ERR_SPP_IO, "SPP IO error." },
44 };
45
GetNapiErrMsg(const napi_env & env,const int32_t errCode)46 static std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode)
47 {
48 auto iter = napiErrMsgMap.find(errCode);
49 if (iter != napiErrMsgMap.end()) {
50 std::string errMessage = "BussinessError ";
51 errMessage.append(std::to_string(errCode)).append(": ").append(iter->second);
52 return errMessage;
53 }
54 return "Inner error.";
55 }
56
HandleSyncErr(const napi_env & env,int32_t errCode)57 void HandleSyncErr(const napi_env &env, int32_t errCode)
58 {
59 if (errCode == BtErrCode::BT_SUCCESS) {
60 return;
61 }
62 std::string errMsg = GetNapiErrMsg(env, errCode);
63 if (errMsg != "") {
64 napi_throw_error(env, std::to_string(errCode).c_str(), errMsg.c_str());
65 }
66 }
67 }
68 }