• 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 \
64         caused by an initialization error in the zlib stream structure or a modified structure."}},
65     {-3, {ZLIB_SYS_CAP_TAG + E_IO, "The input data is incorrect. For example, the data does not conform \
66         to the zlib compression format, the compressed data is corrupted, or the data is not compressed."}},
67     {-4, {ZLIB_SYS_CAP_TAG + E_NXIO, "Memory allocation failed"}},
68     {-5, {ZLIB_SYS_CAP_TAG + E_2BIG, "The input buffer is incorrect, and the output buffer is too \
69         small to accommodate the compressed or decompressed data."}},
70     {-6, {ZLIB_SYS_CAP_TAG + E_BADF, "Version error"}},
71     {ENOSTR, {ZLIB_SYS_CAP_TAG + E_MEMORY, "Internal structure error"}},
72     {EARCH, {ZLIB_SYS_CAP_TAG + E_ARCH, "System architecture error, compiling with _WIN32"}},
73     {ENOENT, {ZLIB_SYS_CAP_TAG + E_NOENT, "No such file or access mode error"}},
74 };
75 
76 class NapiBusinessError {
77 public:
78     NapiBusinessError();
79     explicit NapiBusinessError(ELegacy eLegacy);
80     explicit NapiBusinessError(int32_t ePosix);
81     explicit NapiBusinessError(int32_t ePosix, bool throwCode);
82     NapiBusinessError(const NapiBusinessError &) = default;
83     ~NapiBusinessError() = default;
84 
85     NapiBusinessError &operator=(const NapiBusinessError &) = default;
86 
87     explicit operator bool() const;
88 
89     void SetErrno(ELegacy eLegacy);
90     void SetErrno(int32_t ePosix);
91     int32_t GetErrno(ErrCodeSystem cs);
92 
93     std::string GetDefaultErrstr();
94     napi_value GetNapiErr(napi_env env);
95     napi_value GetNapiErr(napi_env env, const std::string &errMsg);
96     void ThrowErr(napi_env env);
97     void ThrowErr(napi_env env, int32_t code);
98     void ThrowErr(napi_env env, const std::string &errMsg);
99 
100 private:
101     int32_t errno_ = ERRNO_NOERR;
102     ErrCodeSystem codingSystem_ = ERR_CODE_SYSTEM_POSIX;
103     bool throwCode_ = false;
104 };
105 }  // namespace LIBZIP
106 }  // namespace AppExecFwk
107 }  // namespace OHOS
108 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_BUSINESS_ERROR_H