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, "ZStream error"}},
64     {-3, {ZLIB_SYS_CAP_TAG + E_IO, "Data error"}},
65     {-4, {ZLIB_SYS_CAP_TAG + E_NXIO, "Memory allocation failed"}},
66     {-5, {ZLIB_SYS_CAP_TAG + E_2BIG, "Buffer error"}},
67     {-6, {ZLIB_SYS_CAP_TAG + E_BADF, "Version error"}},
68     {ENOSTR, {ZLIB_SYS_CAP_TAG + E_MEMORY, "Internal structure error"}},
69     {EARCH, {ZLIB_SYS_CAP_TAG + E_ARCH, "System architecture error, compiling with _WIN32"}},
70     {ENOENT, {ZLIB_SYS_CAP_TAG + E_NOENT, "No such file or access mode error"}},
71 };
72 
73 class NapiBusinessError {
74 public:
75     NapiBusinessError();
76     explicit NapiBusinessError(ELegacy eLegacy);
77     explicit NapiBusinessError(int32_t ePosix);
78     explicit NapiBusinessError(int32_t ePosix, bool throwCode);
79     NapiBusinessError(const NapiBusinessError &) = default;
80     ~NapiBusinessError() = default;
81 
82     NapiBusinessError &operator=(const NapiBusinessError &) = default;
83 
84     explicit operator bool() const;
85 
86     void SetErrno(ELegacy eLegacy);
87     void SetErrno(int32_t ePosix);
88     int32_t GetErrno(ErrCodeSystem cs);
89 
90     std::string GetDefaultErrstr();
91     napi_value GetNapiErr(napi_env env);
92     napi_value GetNapiErr(napi_env env, const std::string &errMsg);
93     void ThrowErr(napi_env env);
94     void ThrowErr(napi_env env, int32_t code);
95     void ThrowErr(napi_env env, const std::string &errMsg);
96 
97 private:
98     int32_t errno_ = ERRNO_NOERR;
99     ErrCodeSystem codingSystem_ = ERR_CODE_SYSTEM_POSIX;
100     bool throwCode_ = false;
101 };
102 }  // namespace LIBZIP
103 }  // namespace AppExecFwk
104 }  // namespace OHOS
105 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_BUSINESS_ERROR_H