• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UTIL_NAPI_ERROR_H
17 #define UTIL_NAPI_ERROR_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 #include "securec.h"
26 #include "utils/log.h"
27 
28 #include "util_napi.h"
29 
30 namespace OHOS {
31 namespace MMI {
32 const std::string ERR_CODE = "code";
33 struct NapiError {
34     int32_t errorCode;
35     std::string msg;
36 };
37 
38 enum NapiErrorCode : int32_t {
39     OTHER_ERROR = -1,
40     COMMON_PERMISSION_CHECK_ERROR = 201,
41     COMMON_PARAMETER_ERROR = 401,
42     COOPERATOR_TARGET_DEV_DESCRIPTOR_ERROR = 4400001,
43     COOPERATOR_FAIL = 4400002,
44     COOPERATOR_DEVICE_ID_ERROE = 4400003,
45 };
46 
47 const std::map<int32_t, NapiError> NAPI_ERRORS = {
48     {COMMON_PERMISSION_CHECK_ERROR,
49         {COMMON_PERMISSION_CHECK_ERROR, "Permission denied. An attempt was made to %s forbidden by permission:%s."}},
50     {COMMON_PARAMETER_ERROR, {COMMON_PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}},
51     {COOPERATOR_TARGET_DEV_DESCRIPTOR_ERROR,
52         {COOPERATOR_TARGET_DEV_DESCRIPTOR_ERROR, "Incorrect descriptor for the target device"}},
53     {COOPERATOR_DEVICE_ID_ERROE, {COMMON_PARAMETER_ERROR, "Incorrect ID of the input device"}},
54     {COOPERATOR_FAIL, {COOPERATOR_FAIL, "Input device operation failed"}},
55 };
56 
57 #define THROWERR_CUSTOM(env, code, msg) \
58     do { \
59         napi_value businessError = nullptr; \
60         napi_value errorCode = nullptr; \
61         napi_value errorMsg = nullptr; \
62         napi_create_int32(env, code, &errorCode); \
63         napi_create_string_utf8(env, std::string(msg).c_str(), NAPI_AUTO_LENGTH, &errorMsg); \
64         napi_create_error(env, nullptr, errorMsg, &businessError); \
65         napi_set_named_property(env, businessError, ERR_CODE.c_str(), errorCode); \
66         napi_throw(env, businessError); \
67     } while (0)
68 
69 #define THROWERR_API9(env, code, param1, param2) \
70     do { \
71         MMI_HILOGE("ErrorCode:%{public}s", (#code)); \
72         NapiError codeMsg; \
73         if (UtilNapiError::GetApiError(code, codeMsg)) { \
74             char buf[300]; \
75             if (sprintf_s(buf, sizeof(buf), codeMsg.msg.c_str(), param1, param2) > 0) { \
76                 THROWERR_CUSTOM(env, code, buf); \
77             } else { \
78                 MMI_HILOGE("Failed to convert string type to char type"); \
79             } \
80         } \
81     } while (0)
82 namespace UtilNapiError {
83 bool GetApiError(int32_t code, NapiError& codeMsg);
84 } // namespace UtilNapiError
85 } // namespace MMI
86 } // namespace OHOS
87 #endif // UTIL_NAPI_ERROR_H
88