1 /*
2  * Copyright (c) 2023 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 UDMF_ERROR_UTILS_H
17 #define UDMF_ERROR_UTILS_H
18 
19 #include <map>
20 #include <optional>
21 #include <string>
22 
23 #include "js_native_api.h"
24 #include "napi/native_common.h"
25 
26 #include "error_code.h"
27 #include "logger.h"
28 
29 
30 namespace OHOS {
31 namespace UDMF {
32 struct NapiErrorCode {
33     int32_t status;
34     int32_t jsCode;
35     const char *message;
36 };
37 
38 const std::optional<NapiErrorCode> GetErrorCode(int32_t errorCode);
39 Status GenerateNapiError(Status error, int32_t &errCode, std::string &errMessage);
40 void ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage, bool isParamsCheck = true);
41 napi_value GenerateErrorMsg(napi_env env, NapiErrorCode jsInfo);
42 
43 #define ASSERT_ERR(env, assertion, errorcode, message) \
44     do {                                               \
45         if (!(assertion)) {                            \
46             ThrowNapiError(env, errorcode, message);   \
47             return nullptr;                            \
48         }                                              \
49     } while (0)
50 
51 #define ASSERT_ERR_VOID(env, assertion, errorcode, message) \
52     do {                                                    \
53         if (!(assertion)) {                                 \
54             ThrowNapiError(env, errorcode, message);        \
55             return;                                         \
56         }                                                   \
57     } while (0)
58 
59 #define ASSERT_ERR_STATUS(env, assertion, errorcode, message) \
60     do {                                                    \
61         if (!(assertion)) {                                 \
62             ThrowNapiError(env, errorcode, message);        \
63             return napi_generic_failure;                    \
64         }                                                   \
65     } while (0)
66 
67 #define ASSERT_BUSINESS_ERR(ctxt, assertion, errorcode, message) \
68     do {                                                         \
69         if (!(assertion)) {                                      \
70             (ctxt)->isThrowError = true;                         \
71             ThrowNapiError((ctxt)->env, errorcode, message);     \
72             return;                                              \
73         }                                                        \
74     } while (0)
75 
76 #define ASSERT_BUSINESS_ERR_VOID(ctxt, assertion, errorCode, message)  \
77     do {                                                               \
78         if (!(assertion)) {                                            \
79             (ctxt)->isThrowError = true;                               \
80             ThrowNapiError((ctxt)->env, errorCode, message, false);    \
81             return nullptr;                                            \
82         }                                                              \
83     } while (0)
84 } // namespace UDMF
85 } // namespace OHOS
86 #endif // UDMF_ERROR_UTILS_H
87