1 /* 2 * Copyright (c) 2025-2025 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 package com.ohos.hapsigntool.error; 17 18 import com.ohos.hapsigntool.utils.StringUtils; 19 import org.apache.logging.log4j.LogManager; 20 import org.apache.logging.log4j.Logger; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 import java.util.Locale; 25 import java.util.MissingFormatArgumentException; 26 27 /** 28 * ErrorMsg 29 * 30 * @since 2025/01/06 31 */ 32 public class ErrorMsg { 33 private static final Logger log = LogManager.getLogger(ErrorMsg.class); 34 35 private static final String CODE_SIGN_SUB_SYSTEM_CODE = "111"; 36 37 private static final String SIGN_TOOL_SUB_SYSTEM_CODE = "110"; 38 39 private final String code; 40 41 private final String description; 42 43 private final String cause; 44 45 private final String position; 46 47 private final List<String> solutions; 48 49 private MoreInfo moreInfo; 50 51 /** 52 * ErrorMsg constructor 53 * 54 * @param code code 55 * @param description description 56 * @param cause cause 57 * @param position position 58 * @param solutions solutions 59 */ ErrorMsg(String code, String description, String cause, String position, List<String> solutions)60 public ErrorMsg(String code, String description, String cause, 61 String position, List<String> solutions) { 62 this.code = code; 63 this.description = description; 64 this.cause = cause; 65 this.position = position; 66 this.solutions = solutions; 67 this.moreInfo = new MoreInfo(); 68 } 69 70 /** 71 * getCodeSignErrBuilder 72 * 73 * @return Builder 74 */ getCodeSignErrBuilder()75 public static Builder getCodeSignErrBuilder() { 76 return new Builder(CODE_SIGN_SUB_SYSTEM_CODE); 77 } 78 79 /** 80 * getSignToolErrBuilder 81 * 82 * @return Builder 83 */ getSignToolErrBuilder()84 public static Builder getSignToolErrBuilder() { 85 return new Builder(SIGN_TOOL_SUB_SYSTEM_CODE); 86 } 87 88 @Override toString()89 public String toString() { 90 final StringBuilder sb = new StringBuilder(); 91 sb.append(System.lineSeparator()) 92 .append("ERROR: ") 93 .append(code) 94 .append(" ") 95 .append(description) 96 .append(System.lineSeparator()) 97 .append("Error Message: ") 98 .append(cause) 99 .append(System.lineSeparator()); 100 101 if (!StringUtils.isEmpty(position)) { 102 sb.append(" At ").append(position).append(System.lineSeparator()); 103 } 104 105 if (solutions != null && !solutions.isEmpty()) { 106 sb.append(System.lineSeparator()).append("* Try the following: ").append(System.lineSeparator()); 107 for (String s : solutions) { 108 sb.append(" > ").append(s).append(System.lineSeparator()); 109 } 110 } 111 return sb.toString(); 112 } 113 114 /** 115 * to String 116 * 117 * @param args args 118 * @return String 119 */ toString(Object... args)120 public String toString(Object... args) { 121 try { 122 return String.format(Locale.ROOT, this.toString(), args); 123 } catch (MissingFormatArgumentException e) { 124 log.error("args format failed: " + args); 125 return this.toString(); 126 } 127 } 128 129 static class MoreInfo { 130 private String cn; 131 132 private String en; 133 } 134 135 /** 136 * Builder 137 * 138 * @since 2025/01/06 139 */ 140 public static class Builder { 141 private String sysCode; 142 143 private String errCode; 144 145 private String typeCode; 146 147 private String description; 148 149 private String cause; 150 151 private String position; 152 153 private List<String> solutions = new ArrayList<>(); 154 155 /** 156 * Builder Constructor 157 * 158 * @param sysCode sysCode 159 */ Builder(String sysCode)160 public Builder(String sysCode) { 161 this.sysCode = sysCode; 162 } 163 164 /** 165 * addErrCode 166 * 167 * @param errCode errCode 168 * @return Builder 169 */ addErrCode(String errCode)170 public Builder addErrCode(String errCode) { 171 this.errCode = errCode; 172 return this; 173 } 174 175 /** 176 * addTypeCode 177 * 178 * @param typeCode typeCode 179 * @return Builder 180 */ addTypeCode(String typeCode)181 public Builder addTypeCode(String typeCode) { 182 this.typeCode = typeCode; 183 return this; 184 } 185 186 /** 187 * addDescription 188 * 189 * @param description description 190 * @return Builder 191 */ addDescription(String description)192 public Builder addDescription(String description) { 193 this.description = description; 194 return this; 195 } 196 197 /** 198 * addCause 199 * 200 * @param cause cause 201 * @return Builder 202 */ addCause(String cause)203 public Builder addCause(String cause) { 204 this.cause = cause; 205 return this; 206 } 207 208 /** 209 * addPosition 210 * 211 * @param position position 212 * @return Builder 213 */ addPosition(String position)214 public Builder addPosition(String position) { 215 this.position = position; 216 return this; 217 } 218 219 /** 220 * addSolution 221 * 222 * @param solution solution 223 * @return Builder 224 */ addSolution(String solution)225 public Builder addSolution(String solution) { 226 this.solutions.add(solution); 227 return this; 228 } 229 230 /** 231 * build 232 * 233 * @return ErrorMsg 234 */ build()235 public ErrorMsg build() { 236 return new ErrorMsg(sysCode + typeCode + errCode, description, cause, position, solutions); 237 } 238 } 239 } 240