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