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