• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 OHOS_RESTOOL_ERRORS_H
17 #define OHOS_RESTOOL_ERRORS_H
18 
19 #include <iostream>
20 #include <memory>
21 #include <securec.h>
22 #include <stdint.h>
23 
24 #include "resource_data.h"
25 
26 namespace OHOS {
27 namespace Global {
28 namespace Restool {
29 constexpr uint32_t RESTOOL_SUCCESS = 0;
30 constexpr uint32_t RESTOOL_ERROR = -1;
31 constexpr uint16_t BUFFER_SIZE = 4096;
32 constexpr uint16_t BUFFER_SIZE_SMALL = 128;
33 const std::string ERROR_MORE_INFO_FILE = "restool_faq.json";
34 // 11200xxx unknown error
35 constexpr uint32_t ERR_CODE_UNDEFINED_ERROR = 11200000;
36 
37 // 11201xxx dependency error
38 const std::string ERR_TYPE_DEPENDENCY = "Dependency Error";
39 constexpr uint32_t ERR_CODE_LOAD_LIBRARY_FAIL = 11201001;
40 // 11202xxx script error
41 
42 // 11203xxx config error
43 const std::string ERR_TYPE_CONFIG = "Config Error";
44 constexpr uint32_t ERR_CODE_OPEN_JSON_FAIL = 11203001;
45 constexpr uint32_t ERR_CODE_JSON_FORMAT_ERROR = 11203002;
46 constexpr uint32_t ERR_CODE_JSON_NODE_MISMATCH = 11203003;
47 constexpr uint32_t ERR_CODE_JSON_NODE_MISSING = 11203004;
48 constexpr uint32_t ERR_CODE_JSON_NODE_EMPTY = 11203005;
49 constexpr uint32_t ERR_CODE_JSON_NOT_ONE_MEMBER = 11203006;
50 constexpr uint32_t ERR_CODE_JSON_INVALID_NODE_NAME = 11203007;
51 
52 // 11204xxx file resource error
53 const std::string ERR_TYPE_FILE_RESOURCE = "File Resource Error";
54 constexpr uint32_t ERR_CODE_CREATE_FILE_ERROR = 11204001;
55 constexpr uint32_t ERR_CODE_REMOVE_FILE_ERROR = 11204003;
56 constexpr uint32_t ERR_CODE_COPY_FILE_ERROR = 11204004;
57 constexpr uint32_t ERR_CODE_OPEN_FILE_ERROR = 11204005;
58 constexpr uint32_t ERR_CODE_READ_FILE_ERROR = 11204006;
59 
60 // 11210xxx parse command error
61 const std::string ERR_TYPE_COMMAND_PARSE = "Command Parse Error";
62 constexpr uint32_t ERR_CODE_UNKNOWN_COMMAND_ERROR = 11210000;
63 constexpr uint32_t ERR_CODE_UNKNOWN_OPTION = 11210001;
64 constexpr uint32_t ERR_CODE_MISSING_ARGUMENT = 11210002;
65 constexpr uint32_t ERR_CODE_INVALID_ARGUMENT = 11210003;
66 constexpr uint32_t ERR_CODE_INVALID_INPUT = 11210004;
67 constexpr uint32_t ERR_CODE_DUPLICATE_INPUT = 11210005;
68 constexpr uint32_t ERR_CODE_DOUBLE_PACKAGE_NAME = 11210006;
69 constexpr uint32_t ERR_CODE_INVALID_OUTPUT = 11210007;
70 constexpr uint32_t ERR_CODE_DOUBLE_OUTPUT = 11210008;
71 constexpr uint32_t ERR_CODE_DUPLICATE_RES_HEADER = 11210009;
72 constexpr uint32_t ERR_CODE_DOUBLE_MODULES = 11210010;
73 constexpr uint32_t ERR_CODE_DUPLICATE_MODULE_NAME = 11210011;
74 constexpr uint32_t ERR_CODE_DOUBLE_CONFIG_JSON = 11210012;
75 constexpr uint32_t ERR_CODE_INVALID_START_ID = 11210013;
76 constexpr uint32_t ERR_CODE_DUPLICATE_APPEND_PATH = 11210014;
77 constexpr uint32_t ERR_CODE_DOUBLE_TARGET_CONFIG = 11210015;
78 constexpr uint32_t ERR_CODE_INVALID_TARGET_CONFIG = 11210016;
79 constexpr uint32_t ERR_CODE_INVALID_SYSTEM_ID_DEFINED = 11210017;
80 constexpr uint32_t ERR_CODE_DUPLICATE_SYSTEM_ID_DEFINED = 11210018;
81 constexpr uint32_t ERR_CODE_DOUBLE_COMPRESSION_PATH = 11210019;
82 constexpr uint32_t ERR_CODE_NON_ASCII = 11210020;
83 constexpr uint32_t ERR_CODE_EXCLUSIVE_OPTION = 11210021;
84 constexpr uint32_t ERR_CODE_PACKAGE_NAME_EMPTY = 11210022;
85 constexpr uint32_t ERR_CODE_RES_HEADER_PATH_EMPTY = 11210023;
86 constexpr uint32_t ERR_CODE_DUMP_MISSING_INPUT = 11210024;
87 constexpr uint32_t ERR_CODE_DUMP_INVALID_INPUT = 11210025;
88 constexpr uint32_t ERR_CODE_INVALID_THREAD_COUNT = 11210026;
89 constexpr uint32_t ERR_CODE_INVALID_IGNORE_FILE = 11210027;
90 
91 // 11211xxx resource pack error
92 const std::string ERR_TYPE_RESOURCE_PACK = "Resource Pack Error";
93 constexpr uint32_t ERR_CODE_OUTPUT_EXIST = 11211001;
94 constexpr uint32_t ERR_CODE_CONFIG_JSON_MISSING = 11211002;
95 constexpr uint32_t ERR_CODE_INVALID_MODULE_TYPE = 11211003;
96 constexpr uint32_t ERR_CODE_EXCLUSIVE_START_ID = 11211004;
97 constexpr uint32_t ERR_CODE_ID_DEFINED_INVALID_TYPE = 11211007;
98 constexpr uint32_t ERR_CODE_ID_DEFINED_INVALID_ID = 11211008;
99 constexpr uint32_t ERR_CODE_ID_DEFINED_ORDER_MISMATCH = 11211010;
100 constexpr uint32_t ERR_CODE_ID_DEFINED_SAME_ID = 11211012;
101 constexpr uint32_t ERR_CODE_MODULE_NAME_NOT_FOUND = 11211014;
102 
103 constexpr uint32_t ERR_CODE_INVALID_RESOURCE_PATH = 11211101;
104 constexpr uint32_t ERR_CODE_INVALID_LIMIT_KEY = 11211103;
105 constexpr uint32_t ERR_CODE_INVALID_RESOURCE_DIR = 11211104;
106 constexpr uint32_t ERR_CODE_INVALID_TRANSLATE_PRIORITY = 11211106;
107 constexpr uint32_t ERR_CODE_INVALID_ELEMENT_TYPE = 11211107;
108 constexpr uint32_t ERR_CODE_INVALID_COLOR_VALUE = 11211108;
109 constexpr uint32_t ERR_CODE_INVALID_RESOURCE_REF = 11211109;
110 constexpr uint32_t ERR_CODE_PARENT_EMPTY = 11211110;
111 constexpr uint32_t ERR_CODE_ARRAY_TOO_LARGE = 11211111;
112 constexpr uint32_t ERR_CODE_INVALID_QUANTITY = 11211112;
113 constexpr uint32_t ERR_CODE_DUPLICATE_QUANTITY = 11211113;
114 constexpr uint32_t ERR_CODE_QUANTITY_NO_OTHER = 11211114;
115 constexpr uint32_t ERR_CODE_INVALID_SYMBOL = 11211115;
116 constexpr uint32_t ERR_CODE_INVALID_RESOURCE_NAME = 11211116;
117 constexpr uint32_t ERR_CODE_RESOURCE_DUPLICATE = 11211117;
118 constexpr uint32_t ERR_CODE_RESOURCE_ID_EXCEED = 11211118;
119 constexpr uint32_t ERR_CODE_RESOURCE_ID_NOT_DEFINED = 11211119;
120 constexpr uint32_t ERR_CODE_REF_NOT_DEFINED = 11211120;
121 constexpr uint32_t ERR_CODE_INVALID_RESOURCE_INDEX = 11211124;
122 
123 // 11212xxx resource dump error
124 const std::string ERR_TYPE_RESOURCE_DUMP = "Resource Dump Error";
125 constexpr uint32_t ERR_CODE_PARSE_HAP_ERROR = 11212001;
126 
127 enum class Language {
128     CN,
129     EN
130 };
131 
132 struct MoreInfo {
133     std::string cn;
134     std::string en;
135 };
136 
137 class ErrorInfo {
138 public:
139     uint32_t code_;
140     std::string description_;
141     std::string cause_;
142     std::string position_;
143     std::vector<std::string> solutions_;
144     MoreInfo moreInfo_;
145 
146     template <class... Args>
FormatDescription(Args...args)147     ErrorInfo &FormatDescription(Args... args)
148     {
149         description_ = FormatString(description_, std::forward<Args>(args)...);
150         return *this;
151     }
152 
153     template <class... Args>
FormatCause(Args...args)154     ErrorInfo &FormatCause(Args... args)
155     {
156         cause_ = FormatString(cause_, std::forward<Args>(args)...);
157         return *this;
158     }
159 
SetPosition(const std::string & position)160     ErrorInfo &SetPosition(const std::string &position)
161     {
162         position_ = position;
163         return *this;
164     }
165 
166 private:
167     template <class... Args>
FormatString(const std::string & fmt,Args...args)168     std::string FormatString(const std::string &fmt, Args... args)
169     {
170         if (fmt.empty()) {
171             return fmt;
172         }
173         size_t paramCount = sizeof...(args);
174         if (paramCount == 0) {
175             return fmt;
176         }
177         std::unique_ptr<char[]> buf = std::make_unique<char[]>(BUFFER_SIZE);
178         auto realSize = snprintf_s(buf.get(), BUFFER_SIZE, BUFFER_SIZE - 1, fmt.c_str(), args...);
179         if (realSize == -1) {
180             return fmt;
181         }
182         return std::string(buf.get(), buf.get() + realSize);
183     }
184 };
185 
186 void InitFaq(const std::string &restoolPath);
187 ErrorInfo GetError(const uint32_t &errCode);
188 void PrintError(const uint32_t &errCode);
189 void PrintError(const ErrorInfo &error);
190 }
191 }
192 }
193 #endif