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 16 #ifndef WIFI_NAPI_ERRCODE_H_ 17 #define WIFI_NAPI_ERRCODE_H_ 18 19 #include "wifi_napi_utils.h" 20 #include <string> 21 #include "wifi_napi_errcode.h" 22 23 namespace OHOS { 24 namespace Wifi { 25 static const std::string BUSINESS_ERROR_PROPERTY_CODE = "code"; 26 static const std::string BUSINESS_ERROR_PROPERTY_MESSAGE = "message"; 27 static const std::string BUSINESS_ERROR_PROPERTY_DATA = "data"; 28 29 enum WifiNapiErrCode { 30 WIFI_ERRCODE_SUCCESS = 0, /* successfully */ 31 WIFI_ERRCODE_PERMISSION_DENIED = 201, /* permission denied */ 32 WIFI_ERRCODE_INVALID_PARAM = 401, /* invalid params */ 33 WIFI_ERRCODE_NOT_SUPPORTED = 801, /* not supported */ 34 WIFI_ERRCODE_OPERATION_FAILED = 1000, /* failed */ 35 WIFI_ERRCODE_WIFI_NOT_OPENED = 1001, /* sta service not opened */ 36 WIFI_ERRCODE_OPEN_FAIL_WHEN_CLOSING = 1003, /* forbid when current airplane opened */ 37 WIFI_ERRCODE_CLOSE_FAIL_WHEN_OPENING = 1004, /* forbid when current powersaving opened */ 38 }; 39 40 #ifdef ENABLE_NAPI_WIFI_MANAGER 41 #ifndef WIFI_NAPI_ASSERT 42 #define WIFI_NAPI_ASSERT(env, cond, errCode, sysCap) \ 43 do { \ 44 if (!(cond)) { \ 45 napi_value res = nullptr; \ 46 HandleSyncErrCode(env, errCode, sysCap); \ 47 napi_get_undefined(env, &res); \ 48 return res; \ 49 } \ 50 } while (0) 51 #endif 52 53 #ifndef WIFI_NAPI_RETURN 54 #define WIFI_NAPI_RETURN(env, cond, errCode, sysCap) \ 55 do { \ 56 napi_value res = nullptr; \ 57 if (!(cond)) { \ 58 HandleSyncErrCode(env, errCode, sysCap); \ 59 } \ 60 napi_get_undefined(env, &res); \ 61 return res; \ 62 } while (0) 63 #endif 64 65 #else /* #else ENABLE_NAPI_WIFI_MANAGER */ 66 67 #ifndef WIFI_NAPI_ASSERT 68 #define WIFI_NAPI_ASSERT(env, cond, errCode, sysCap) \ 69 do { \ 70 if (!(cond)) { \ 71 napi_value res = nullptr; \ 72 napi_get_boolean(env, cond, &res); \ 73 return res; \ 74 } \ 75 } while (0) 76 #endif 77 78 #ifndef WIFI_NAPI_RETURN 79 #define WIFI_NAPI_RETURN(env, cond, errCode, sysCap) \ 80 do { \ 81 napi_value res = nullptr; \ 82 napi_get_boolean(env, cond, &res); \ 83 return res; \ 84 } while (0) 85 #endif 86 #endif /* #endif ENABLE_NAPI_WIFI_MANAGER */ 87 88 /** 89 * @brief Thow error code for async-callback function. 90 * 91 * @param env The env. 92 * @param info The input data. 93 */ 94 void HandleCallbackErrCode(const napi_env &env, const AsyncContext &info); 95 96 /** 97 * @brief Thow error code for async-promise function. 98 * 99 * @param env The env. 100 * @param info The input data. 101 */ 102 void HandlePromiseErrCode(const napi_env &env, const AsyncContext &info); 103 104 105 #ifdef ENABLE_NAPI_WIFI_MANAGER 106 /** 107 * @brief Thow error code for async function. 108 * 109 * @param env The env. 110 * @param errCode The error code. 111 * @param sysCap System capability code. 112 */ 113 void HandleSyncErrCode(const napi_env &env, int32_t errCode, int32_t sysCap); 114 #endif 115 } // namespace Wifi 116 } // namespace OHOS 117 #endif 118 119