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 OHOS_ROSEN_COLOR_MANAGER_COMMON_H
17 #define OHOS_ROSEN_COLOR_MANAGER_COMMON_H
18 
19 #include <cstdint>
20 #include <map>
21 
22 namespace OHOS {
23 namespace ColorManager {
24 enum class CMError : int32_t {
25     CM_OK = 0,
26     CM_ERROR_NULLPTR,
27     CM_ERROR_INVALID_PARAM,
28     CM_ERROR_INVALID_ENUM_USAGE,
29 };
30 
31 enum class CMErrorCode : int32_t {
32     CM_OK = 0,
33     CM_ERROR_NO_PERMISSION = 201, // the value do not change. It is defined on all system
34     CM_ERROR_INVALID_PARAM = 401, // the value do not change. It is defined on all system
35     CM_ERROR_DEVICE_NOT_SUPPORT = 801, // the value do not change. It is defined on all system
36     CM_ERROR_ABNORMAL_PARAM_VALUE = 18600001, // the value do not change. It is defined on color manager system
37 };
38 
39 const std::map<CMError, CMErrorCode> JS_TO_ERROR_CODE_MAP {
40     { CMError::CM_OK, CMErrorCode::CM_OK },
41     { CMError::CM_ERROR_NULLPTR, CMErrorCode::CM_ERROR_INVALID_PARAM },
42     { CMError::CM_ERROR_INVALID_PARAM, CMErrorCode::CM_ERROR_INVALID_PARAM },
43     { CMError::CM_ERROR_INVALID_ENUM_USAGE, CMErrorCode::CM_ERROR_ABNORMAL_PARAM_VALUE },
44 };
45 
46 #define NAPI_CALL_WITH_ERRCODE(env, theCall, errCode)                                                        \
47     do {                                                                                                     \
48         if ((theCall) != napi_ok) {                                                                          \
49             napi_throw(env,                                                                                  \
50                 CreateJsError(env, static_cast<int32_t>(errCode),                                            \
51                 "napi failed."));                                                                            \
52             return nullptr;                                                                                  \
53         }                                                                                                    \
54     } while (0)
55 
56 #define NAPI_CALL_DEFAULT(theCall)                                                                           \
57     do {                                                                                                     \
58         if ((theCall) != napi_ok) {                                                                          \
59             return nullptr;                                                                                  \
60         }                                                                                                    \
61     } while (0)
62 #define NAPI_CALL_WITH_ERRCODE_DEFAULT(env, theCall) \
63     NAPI_CALL_WITH_ERRCODE(env, theCall, CMErrorCode::CM_ERROR_INVALID_PARAM)
64 } // namespace ColorManager
65 } // namespace OHOS
66 
67 #endif // OHOS_ROSEN_COLOR_MANAGER_COMMON_H