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 #ifndef NAPI_BLUETOOTH_ERROR_H_
16 #define NAPI_BLUETOOTH_ERROR_H_
17 
18 
19 #include <cstdint>
20 #include <string>
21 #include "bluetooth_log.h"
22 #include "napi/native_api.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 
27 #ifdef ENABLE_NAPI_BLUETOOTH_MANAGER
28 #ifndef NAPI_BT_ASSERT_RETURN
29 #define NAPI_BT_ASSERT_RETURN(env, cond, errCode, retObj) \
30 do {                                                      \
31     if (!(cond)) {                                        \
32         HandleSyncErr((env), (errCode));                  \
33         HILOGE("bluetoothManager napi assert failed.");                    \
34         return (retObj);                                  \
35     }                                                     \
36 } while (0)
37 #endif
38 #else // ENABLE_NAPI_BLUETOOTH_MANAGER
39 #ifndef NAPI_BT_ASSERT_RETURN
40 #define NAPI_BT_ASSERT_RETURN(env, cond, errCode, retObj) \
41 do {                                                      \
42     if (!(cond)) {                                        \
43         HILOGE("bluetooth napi assert failed.");                    \
44         return (retObj);                                  \
45     }                                                     \
46 } while (0)
47 #endif
48 #endif
49 
50 #define NAPI_BT_ASSERT_RETURN_UNDEF(env, cond, errCode)  \
51 do {                                                     \
52     napi_value res = nullptr;                            \
53     napi_get_undefined((env), &res);                     \
54     NAPI_BT_ASSERT_RETURN((env), (cond), (errCode), res); \
55 } while (0)
56 
57 #define NAPI_BT_ASSERT_RETURN_FALSE(env, cond, errCode)  \
58 do {                                                     \
59     napi_value res = nullptr;                            \
60     napi_get_boolean((env), false, &res);                \
61     NAPI_BT_ASSERT_RETURN((env), (cond), (errCode), res); \
62 } while (0)
63 
64 std::string GetNapiErrMsg(const napi_env &env, const int32_t errCode);
65 void HandleSyncErr(const napi_env &env, int32_t errCode);
66 }
67 }
68 #endif