• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BUSINESS_ERR(ctxt, assertion, errorcode, message) \
60     do {                                                         \
61         if (!(assertion)) {                                      \
62             (ctxt)->isThrowError = true;                         \
63             ThrowNapiError((ctxt)->env, errorcode, message);     \
64             return;                                              \
65         }                                                        \
66     } while (0)
67 
68 #define ASSERT_PERMISSION_ERR(ctxt, assertion, errorCode, message)  \
69     do {                                                            \
70         if (!(assertion)) {                                         \
71             (ctxt)->isThrowError = true;                            \
72             ThrowNapiError((ctxt)->env, errorCode, message, false); \
73             return;                                                 \
74         }                                                           \
75     } while (0)
76 } // namespace UDMF
77 } // namespace OHOS
78 #endif // UDMF_ERROR_UTILS_H
79