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 ohos; 17 import java.util.ArrayList; 18 import java.util.List; 19 import java.util.Locale; 20 import java.util.MissingFormatArgumentException; 21 22 /** 23 * ErrorMsg 24 * 25 * @since 2025/01/21 26 */ 27 public class ErrorMsg { 28 private static final Log LOG = new Log(ErrorMsg.class.toString()); 29 30 private static final String PACKING_TOOL_SUB_SYSTEM_CODE = "100"; 31 32 private final String code; 33 34 private final String description; 35 36 private final String cause; 37 38 private final List<String> solutions; 39 40 /** 41 * ErrorMsg constructor 42 * 43 * @param code code 44 * @param description description 45 * @param cause cause 46 * @param solutions solutions 47 */ ErrorMsg(String code, String description, String cause, List<String> solutions)48 public ErrorMsg(String code, String description, String cause, 49 List<String> solutions) { 50 this.code = code; 51 this.description = description; 52 this.cause = cause; 53 this.solutions = solutions; 54 } 55 56 /** 57 * getPackingToolErrBuilder 58 * 59 * @return Builder 60 */ getPackingToolErrBuilder()61 public static Builder getPackingToolErrBuilder() { 62 return new Builder(PACKING_TOOL_SUB_SYSTEM_CODE); 63 } 64 65 66 @Override toString()67 public String toString() { 68 final StringBuilder sb = new StringBuilder(); 69 sb.append(code) 70 .append(" ") 71 .append(description) 72 .append(System.lineSeparator()) 73 .append("Error Message: ") 74 .append(cause) 75 .append(System.lineSeparator()); 76 77 if (solutions != null && !solutions.isEmpty()) { 78 sb.append(System.lineSeparator()).append("* Try the following: ").append(System.lineSeparator()); 79 for (String s : solutions) { 80 sb.append(" > ").append(s).append(System.lineSeparator()); 81 } 82 } 83 return sb.toString(); 84 } 85 86 /** 87 * to String 88 * 89 * @param args args 90 * @return String 91 */ toString(Object... args)92 public String toString(Object... args) { 93 try { 94 return String.format(Locale.ROOT, this.toString(), args); 95 } catch (MissingFormatArgumentException e) { 96 LOG.error("args format failed: " + args); 97 return this.toString(); 98 } 99 } 100 101 /** 102 * Builder 103 * 104 * @since 2025/01/21 105 */ 106 public static class Builder { 107 private String sysCode; 108 109 private String errCode; 110 111 private String typeCode; 112 113 private String description; 114 115 private String cause; 116 117 private List<String> solutions = new ArrayList<>(); 118 119 /** 120 * Builder Constructor 121 * 122 * @param sysCode sysCode 123 */ Builder(String sysCode)124 public Builder(String sysCode) { 125 this.sysCode = sysCode; 126 } 127 128 /** 129 * setErrCode 130 * 131 * @param errCode errCode 132 * @return Builder 133 */ setErrCode(String errCode)134 public Builder setErrCode(String errCode) { 135 this.errCode = errCode; 136 return this; 137 } 138 139 /** 140 * setTypeCode 141 * 142 * @param typeCode typeCode 143 * @return Builder 144 */ setTypeCode(String typeCode)145 public Builder setTypeCode(String typeCode) { 146 this.typeCode = typeCode; 147 return this; 148 } 149 150 /** 151 * setDescription 152 * 153 * @param description description 154 * @return Builder 155 */ setDescription(String description)156 public Builder setDescription(String description) { 157 this.description = description; 158 return this; 159 } 160 161 /** 162 * setCause 163 * 164 * @param cause cause 165 * @return Builder 166 */ setCause(String cause)167 public Builder setCause(String cause) { 168 this.cause = cause; 169 return this; 170 } 171 172 /** 173 * addSolution 174 * 175 * @param solution solution 176 * @return Builder 177 */ addSolution(String solution)178 public Builder addSolution(String solution) { 179 this.solutions.add(solution); 180 return this; 181 } 182 183 /** 184 * build 185 * 186 * @return ErrorMsg 187 */ build()188 public ErrorMsg build() { 189 return new ErrorMsg(sysCode + typeCode + errCode, description, cause, solutions); 190 } 191 } 192 } 193