1 /*
2 * Copyright (c) 2025 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 SERIAL_NAPI_ERRORS_H
17 #define SERIAL_NAPI_ERRORS_H
18
19 #include <map>
20 #include <string>
21 #include <string_view>
22 #include "napi/native_api.h"
23 #include "hilog_wrapper.h"
24
25 namespace OHOS {
26 namespace USB {
27 void ThrowBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
28 napi_value CreateBusinessError(const napi_env &env, int32_t errCode, const std::string &errMsg);
29
30 enum SerialJsErrCode : int32_t {
31 SYSPARAM_INVALID_INPUT = 401,
32 SERIAL_SYSAPI_NOPERMISSION_CALL = 201,
33 SERIAL_SYSAPI_PERMISSION_DENIED = 202,
34 SERIAL_SERVICE_ABNORMAL = 31400001,
35 SERIAL_INTERFACE_PERMISSION_DENIED = 31400002,
36 SERIAL_PORT_NOT_EXIST = 31400003,
37 SERIAL_PORT_OCCUPIED = 31400004,
38 SERIAL_PORT_NOT_OPEN = 31400005,
39 SERIAL_TIMED_OUT = 31400006,
40 SERIAL_IO_EXCEPTION = 31400007,
41 UEC_COMMON_RIGHT_DATABASE_ERROR = 14400005,
42 };
43
44 #ifndef USB_NAPI_ERRORS_H
45 const std::map<int32_t, std::string_view> ERRCODE_MSG_MAP = {
46 {SYSPARAM_INVALID_INPUT, "BusinessError 401:Parameter error."},
47 {SERIAL_SYSAPI_NOPERMISSION_CALL,
48 "BusinessError 201:Permission denied. The application does not have the permission to call the API."},
49 {SERIAL_SYSAPI_PERMISSION_DENIED, "BusinessError 202:Permission denied. Normal application uses system api."},
50 {SERIAL_SERVICE_ABNORMAL, "BusinessError 31400001:The serial port service is abnormal."},
51 {SERIAL_INTERFACE_PERMISSION_DENIED,
52 "BusinessError 31400002:Permission denied. no access right to serial device, call requestSerialRight first."},
53 {SERIAL_PORT_NOT_EXIST, "BusinessError 31400003:port not exist."},
54 {SERIAL_PORT_OCCUPIED, "BusinessError 31400004:device is using by other application."},
55 {SERIAL_PORT_NOT_OPEN, "BusinessError 31400005:device is not open, call open first."},
56 {SERIAL_TIMED_OUT, "BusinessError 31400006:transfer timeout."},
57 {SERIAL_IO_EXCEPTION, "BusinessError 31400007: IO exception. Possible causes: "
58 "1. The transfer was canceled. "
59 "2. The device offered more data."},
60 };
61 #else
62 #error
63 #endif
64
CheckAndThrowOnError(napi_env env,bool assertion,int errCode,const std::string & errMsg)65 inline bool CheckAndThrowOnError(napi_env env, bool assertion, int errCode, const std::string& errMsg)
66 {
67 if (!assertion) {
68 USB_HILOGE(MODULE_JS_NAPI, "%{public}s", errMsg.c_str());
69 OHOS::USB::ThrowBusinessError(env, errCode, errMsg.c_str());
70 return false;
71 }
72 return true;
73 }
74
CheckNapiResult(napi_env env,napi_status theCall,const std::string & loginfo)75 inline bool CheckNapiResult(napi_env env, napi_status theCall, const std::string& loginfo)
76 {
77 if (theCall != napi_ok) {
78 USB_HILOGE(MODULE_JS_NAPI, "%{public}s: %{public}s", __func__, loginfo.c_str());
79 return false;
80 }
81 return true;
82 }
83
84 } // namespace SERIAL
85 } // namespace OHOS
86 #endif // SERIAL_NAPI_ERRORS_H
87