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     COMMON_USE_SYSAPI_ERROR = 202,
43     COMMON_CAPABILITY_NOT_SUPPORTED = 801,
44     INPUT_OCCUPIED_BY_SYSTEM = 4200002,
45     INPUT_OCCUPIED_BY_OTHER = 4200003,
46 };
47 
48 const std::map<int32_t, NapiError> NAPI_ERRORS = {
49     { COMMON_PERMISSION_CHECK_ERROR,
50         { COMMON_PERMISSION_CHECK_ERROR, "Permission denied. An attempt was made to %s forbidden by permission:%s." } },
51     { COMMON_PARAMETER_ERROR, { COMMON_PARAMETER_ERROR, "Parameter error. The type of %s must be %s." } },
52 };
53 
54 #define THROWERR_CUSTOM(env, code, msg) \
55     do { \
56         napi_value businessError = nullptr; \
57         napi_value errorCode = nullptr; \
58         napi_value errorMsg = nullptr; \
59         napi_create_int32(env, code, &errorCode); \
60         napi_create_string_utf8(env, std::string(msg).c_str(), NAPI_AUTO_LENGTH, &errorMsg); \
61         napi_create_error(env, nullptr, errorMsg, &businessError); \
62         napi_set_named_property(env, businessError, ERR_CODE.c_str(), errorCode); \
63         napi_throw(env, businessError); \
64     } while (0)
65 
66 #define THROWERR_API9(env, code, param1, param2) \
67     do { \
68         MMI_HILOGE("ErrorCode:%{public}s", (#code)); \
69         NapiError codeMsg; \
70         if (UtilNapiError::GetApiError(code, codeMsg)) { \
71             char buf[300]; \
72             if (sprintf_s(buf, sizeof(buf), codeMsg.msg.c_str(), param1, param2) > 0) { \
73                 THROWERR_CUSTOM(env, code, buf); \
74             } else { \
75                 MMI_HILOGE("Failed to convert string type to char type"); \
76             } \
77         } \
78     } while (0)
79 namespace UtilNapiError {
80 bool GetApiError(int32_t code, NapiError &codeMsg);
81 } // namespace UtilNapiError
82 } // namespace MMI
83 } // namespace OHOS
84 #endif // UTIL_NAPI_ERROR_H
85