• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 INTERFACES_KITS_JS_ZIP_NAPI_COMMON_BUSINESS_ERROR_H
17 #define INTERFACES_KITS_JS_ZIP_NAPI_COMMON_BUSINESS_ERROR_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "header.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace LIBZIP {
27 constexpr int32_t EARCH = 100;
28 constexpr int32_t EZSTREAM_ERROR = -2;
29 constexpr int32_t ERRNO_NOERR = 0;
30 constexpr int32_t ZLIB_SYS_CAP_TAG = 17800000;
31 const std::string ZLIB_TAG_ERR_CODE = "code";
32 const std::string ZLIB_TAG_ERR_MSG = "message";
33 
34 enum ELegacy {
35     ELEGACY_INVAL = 202,
36     ELEGACY_IO = 300,
37     ELEGACY_NOENT = 301,
38     ELEGACY_ARGUMENT = 400,
39 };
40 
41 enum ErrCodeSystem {
42     ERR_CODE_SYSTEM_LEGACY,
43     ERR_CODE_SYSTEM_POSIX,
44 };
45 
46 enum ErrCodeSuffix {
47     E_PERM = 1,
48     E_NOENT,
49     E_SRCH,
50     E_INTR,
51     E_IO,
52     E_NXIO,
53     E_2BIG,
54     E_BADF,
55     E_MEMORY,
56     E_ARCH,
57 };
58 
59 const std::unordered_map<int32_t, std::pair<int32_t, std::string>> errCodeTable {
60     { EFAULT, { ZLIB_SYS_CAP_TAG + E_PERM, "Bad address" } },
61     { EINVAL, { ELEGACY_ARGUMENT + E_PERM, "The parameter check failed" } },
62     { -1, { ZLIB_SYS_CAP_TAG + E_SRCH, "System error" } },
63     { -2, { ZLIB_SYS_CAP_TAG + E_INTR, "Compression or decompression stream error, which may be caused by an "
64                                        "initialization error in the zlib stream structure or a modified structure." } },
65     { -3, { ZLIB_SYS_CAP_TAG + E_IO,
66               "The input data is incorrect. For example, the data does not conform to the zlib compression format, the "
67               "compressed data is corrupted, or the data is not compressed." } },
68     { -4, { ZLIB_SYS_CAP_TAG + E_NXIO, "Memory allocation failed" } },
69     { -5, { ZLIB_SYS_CAP_TAG + E_2BIG, "The input buffer is incorrect, and the output buffer is too small to "
70                                        "accommodate the compressed or decompressed data." } },
71     { -6, { ZLIB_SYS_CAP_TAG + E_BADF, "Version error" } },
72     { ENOSTR, { ZLIB_SYS_CAP_TAG + E_MEMORY, "Internal structure error" } },
73     { EARCH, { ZLIB_SYS_CAP_TAG + E_ARCH, "System architecture error, compiling with _WIN32" } },
74     { ENOENT, { ZLIB_SYS_CAP_TAG + E_NOENT, "No such file or access mode error" } },
75 };
76 
77 class NapiBusinessError {
78 public:
79     NapiBusinessError();
80     explicit NapiBusinessError(ELegacy eLegacy);
81     explicit NapiBusinessError(int32_t ePosix);
82     explicit NapiBusinessError(int32_t ePosix, bool throwCode);
83     NapiBusinessError(const NapiBusinessError &) = default;
84     ~NapiBusinessError() = default;
85 
86     NapiBusinessError &operator=(const NapiBusinessError &) = default;
87 
88     explicit operator bool() const;
89 
90     void SetErrno(ELegacy eLegacy);
91     void SetErrno(int32_t ePosix);
92     int32_t GetErrno(ErrCodeSystem cs);
93 
94     std::string GetDefaultErrstr();
95     napi_value GetNapiErr(napi_env env);
96     napi_value GetNapiErr(napi_env env, const std::string &errMsg);
97     void ThrowErr(napi_env env);
98     void ThrowErr(napi_env env, int32_t code);
99     void ThrowErr(napi_env env, const std::string &errMsg);
100 
101 private:
102     int32_t errno_ = ERRNO_NOERR;
103     ErrCodeSystem codingSystem_ = ERR_CODE_SYSTEM_POSIX;
104     bool throwCode_ = false;
105 };
106 }  // namespace LIBZIP
107 }  // namespace AppExecFwk
108 }  // namespace OHOS
109 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_BUSINESS_ERROR_H