001package run.iget.framework.common.util; 002 003import java.io.PrintWriter; 004import java.io.StringWriter; 005import java.util.Collection; 006import java.util.Map; 007import java.util.Objects; 008 009import cn.hutool.core.collection.CollectionUtil; 010import cn.hutool.core.io.IoUtil; 011import cn.hutool.core.util.StrUtil; 012import run.iget.framework.common.enums.BaseResultEnum; 013import run.iget.framework.common.exception.BusinessException; 014 015/** 016 * 代码千万行,注释第一行,注释不规范,迭代两行泪 017 * ---------------类描述----------------- 018 * 业务异常工具,快速判断参数进而抛出异常 019 * ---------------类描述----------------- 020 * 021 * @author 大周 022 * @date 2022/12/31 18:27 023 */ 024public final class ExceptionThrowUtils { 025 026 public static void ofTrue(Boolean obj) { 027 ofTrue(obj, BaseResultEnum.ERROR_PARAM); 028 } 029 030 public static void ofTrue(Boolean obj, String msg) { 031 ofTrue(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 032 } 033 034 public static void ofTrue(Boolean obj, String code, String msg) { 035 if (Objects.equals(Boolean.TRUE, obj)) { 036 of(code, msg); 037 } 038 } 039 040 public static void ofTrue(Boolean obj, BaseResultEnum resultEnum) { 041 if (Objects.equals(Boolean.TRUE, obj)) { 042 of(resultEnum); 043 } 044 } 045 046 public static void ofAnyTrue(Boolean... obj) { 047 ofAnyTrue(BaseResultEnum.ERROR_PARAM, obj); 048 } 049 050 public static void ofAnyTrue(String msg, Boolean... obj) { 051 ofAnyTrue(BaseResultEnum.ERROR_PARAM.getCode(), msg, obj); 052 } 053 054 public static void ofAnyTrue(String code, String msg, Boolean... obj) { 055 if (CheckUtils.isAnyTrue(obj)) { 056 of(code, msg); 057 } 058 } 059 060 public static void ofAnyTrue(BaseResultEnum resultEnum, Boolean... obj) { 061 if (CheckUtils.isAnyTrue(obj)) { 062 of(resultEnum); 063 } 064 } 065 066 public static void ofAllTrue(Boolean... obj) { 067 ofAllTrue(BaseResultEnum.ERROR_PARAM, obj); 068 } 069 070 public static void ofAllTrue(String msg, Boolean... obj) { 071 ofAllTrue(BaseResultEnum.ERROR_PARAM.getCode(), msg, obj); 072 } 073 074 public static void ofAllTrue(String code, String msg, Boolean... obj) { 075 if (CheckUtils.isAllTrue(obj)) { 076 of(code, msg); 077 } 078 } 079 080 public static void ofAllTrue(BaseResultEnum resultEnum, Boolean... obj) { 081 if (CheckUtils.isAllTrue(obj)) { 082 of(resultEnum); 083 } 084 } 085 086 public static void ofNotTrue(Boolean obj) { 087 ofNotTrue(obj, BaseResultEnum.ERROR_PARAM); 088 } 089 090 public static void ofNotTrue(Boolean obj, String msg) { 091 ofNotTrue(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 092 } 093 094 public static void ofNotTrue(Boolean obj, String code, String msg) { 095 if (!Objects.equals(Boolean.TRUE, obj)) { 096 of(code, msg); 097 } 098 } 099 100 public static void ofNotTrue(Boolean obj, BaseResultEnum resultEnum) { 101 if (!Objects.equals(Boolean.TRUE, obj)) { 102 of(resultEnum); 103 } 104 } 105 106 public static void ofFalse(Boolean obj) { 107 ofFalse(obj, BaseResultEnum.ERROR_PARAM); 108 } 109 110 public static void ofFalse(Boolean obj, String msg) { 111 ofFalse(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 112 } 113 114 public static void ofFalse(Boolean obj, String code, String msg) { 115 if (Objects.equals(Boolean.FALSE, obj)) { 116 of(code, msg); 117 } 118 } 119 120 public static void ofFalse(Boolean obj, BaseResultEnum resultEnum) { 121 if (Objects.equals(Boolean.FALSE, obj)) { 122 of(resultEnum); 123 } 124 } 125 126 public static void ofNotFalse(Boolean obj) { 127 ofNotFalse(obj, BaseResultEnum.ERROR_PARAM); 128 } 129 130 public static void ofNotFalse(Boolean obj, String msg) { 131 ofNotFalse(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 132 } 133 134 public static void ofNotFalse(Boolean obj, String code, String msg) { 135 if (!Objects.equals(Boolean.FALSE, obj)) { 136 of(code, msg); 137 } 138 } 139 140 public static void ofNotFalse(Boolean obj, BaseResultEnum resultEnum) { 141 if (!Objects.equals(Boolean.FALSE, obj)) { 142 of(resultEnum); 143 } 144 } 145 146 /** 147 * 是null的时候,抛出 参数错误异常BusinessException 148 * 149 * @param obj 参数 150 */ 151 public static void ofNull(Object obj) { 152 ofNull(obj, BaseResultEnum.ERROR_PARAM); 153 } 154 155 public static void ofNull(Object obj, String msg) { 156 ofNull(obj, null, msg); 157 } 158 159 public static void ofNull(Object obj, String code, String msg) { 160 ofTrue(Objects.isNull(obj), code, msg); 161 } 162 163 /** 164 * 是null的时候,抛出异常BusinessException(resultEnum) 165 * 166 * @param obj 参数 167 * @param resultEnum 异常参数 168 */ 169 public static void ofNull(Object obj, BaseResultEnum resultEnum) { 170 ofTrue(Objects.isNull(obj), resultEnum); 171 } 172 173 /** 174 * 不是null的时候,抛出 参数错误异常BusinessException 175 * 176 * @param obj 参数 177 */ 178 public static void ofNotNull(Object obj) { 179 ofNull(obj, BaseResultEnum.ERROR_PARAM); 180 } 181 182 public static void ofNotNull(Object obj, String msg) { 183 ofNull(obj, null, msg); 184 } 185 186 public static void ofNotNull(Object obj, String code, String msg) { 187 ofTrue(Objects.nonNull(obj), code, msg); 188 } 189 190 /** 191 * 不是null的时候,抛出异常BusinessException(resultEnum) 192 * 193 * @param obj 参数 194 * @param resultEnum 异常参数 195 */ 196 public static void ofNotNull(Object obj, BaseResultEnum resultEnum) { 197 ofTrue(Objects.nonNull(obj), resultEnum); 198 } 199 200 public static void ofEmpty(Map obj) { 201 ofEmpty(obj, BaseResultEnum.ERROR_PARAM); 202 } 203 204 public static void ofEmpty(Map obj, String msg) { 205 ofEmpty(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 206 } 207 208 public static void ofEmpty(Map obj, String code, String msg) { 209 if (CollectionUtil.isEmpty(obj)) { 210 of(code, msg); 211 } 212 } 213 214 public static void ofEmpty(Map obj, BaseResultEnum resultEnum) { 215 if (CollectionUtil.isEmpty(obj)) { 216 of(resultEnum); 217 } 218 } 219 220 public static void ofNotEmpty(Map obj) { 221 ofNotEmpty(obj, BaseResultEnum.ERROR_PARAM); 222 } 223 224 public static void ofNotEmpty(Map obj, String msg) { 225 ofNotEmpty(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 226 } 227 228 public static void ofNotEmpty(Map obj, String code, String msg) { 229 if (CollectionUtil.isNotEmpty(obj)) { 230 of(code, msg); 231 } 232 } 233 234 public static void ofNotEmpty(Map obj, BaseResultEnum resultEnum) { 235 if (CollectionUtil.isNotEmpty(obj)) { 236 of(resultEnum); 237 } 238 } 239 240 public static void ofEmpty(Collection obj) { 241 ofEmpty(obj, BaseResultEnum.ERROR_PARAM); 242 } 243 244 public static void ofEmpty(Collection obj, String msg) { 245 ofEmpty(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 246 } 247 248 public static void ofEmpty(Collection obj, String code, String msg) { 249 if (CollectionUtil.isEmpty(obj)) { 250 of(code, msg); 251 } 252 } 253 254 public static void ofEmpty(Collection obj, BaseResultEnum resultEnum) { 255 if (CollectionUtil.isEmpty(obj)) { 256 of(resultEnum); 257 } 258 } 259 260 public static void ofNotEmpty(Collection obj) { 261 ofNotEmpty(obj, BaseResultEnum.ERROR_PARAM); 262 } 263 264 public static void ofNotEmpty(Collection obj, String msg) { 265 ofNotEmpty(obj, BaseResultEnum.ERROR_PARAM.getCode(), msg); 266 } 267 268 public static void ofNotEmpty(Collection obj, String code, String msg) { 269 if (CollectionUtil.isNotEmpty(obj)) { 270 of(code, msg); 271 } 272 } 273 274 public static void ofNotEmpty(Collection obj, BaseResultEnum resultEnum) { 275 if (CollectionUtil.isNotEmpty(obj)) { 276 of(resultEnum); 277 } 278 } 279 280 /** 281 * 抛出异常BusinessException(resultEnum) 282 * 283 * @param msg 异常消息 284 */ 285 public static void of(String msg) { 286 of(BaseResultEnum.ERROR_PARAM.getCode(), msg); 287 } 288 289 public static void of(String code, String msg) { 290 of(code, msg, null); 291 } 292 293 public static void of(String code, String msg, Throwable cause) { 294 if (StrUtil.isBlank(code)) { 295 code = BaseResultEnum.ERROR_PARAM.getCode(); 296 } 297 of(BaseResultEnum.of(code, msg), cause); 298 } 299 300 public static void of(String msg, Throwable cause) { 301 of(BaseResultEnum.of(BaseResultEnum.ERROR_PARAM.getCode(), msg), cause); 302 } 303 304 public static void of(BaseResultEnum resultEnum) { 305 throw new BusinessException(resultEnum, null); 306 } 307 308 public static void of(BaseResultEnum resultEnum, Throwable cause) { 309 throw new BusinessException(resultEnum, cause); 310 } 311 312 /** 313 * 获取异常信息 314 * @param e 异常 315 * @return 返回异常信息 316 */ 317 public static String getExceptionMessage(Exception e) { 318 StringWriter sw = new StringWriter(); 319 PrintWriter pw = new PrintWriter(sw, true); 320 e.printStackTrace(pw); 321 322 // 关闭IO流 323 IoUtil.close(pw); 324 IoUtil.close(sw); 325 326 return sw.toString(); 327 } 328 329 public static void ofBlank(String obj) { 330 ofTrue(StrUtil.isBlank(obj), BaseResultEnum.ERROR_PARAM); 331 } 332 333 public static void ofBlank(String obj, String msg) { 334 ofTrue(StrUtil.isBlank(obj), BaseResultEnum.ERROR_PARAM.getCode(), msg); 335 } 336 337 public static void ofBlank(String obj, String code, String msg) { 338 ofTrue(StrUtil.isBlank(obj), code, msg); 339 } 340 341 public static void ofBlank(String obj, BaseResultEnum resultEnum) { 342 ofTrue(StrUtil.isBlank(obj), resultEnum); 343 } 344 345 public static void ofNotBlank(String obj) { 346 ofTrue(StrUtil.isNotBlank(obj), BaseResultEnum.ERROR_PARAM); 347 } 348 349 public static void ofNotBlank(String obj, String msg) { 350 ofTrue(StrUtil.isNotBlank(obj), BaseResultEnum.ERROR_PARAM.getCode(), msg); 351 } 352 353 public static void ofNotBlank(String obj, String code, String msg) { 354 ofTrue(StrUtil.isNotBlank(obj), code, msg); 355 } 356 357 public static void ofNotBlank(String obj, BaseResultEnum resultEnum) { 358 ofTrue(StrUtil.isNotBlank(obj), resultEnum); 359 } 360 361 public static void ofAnyBlank(String... obj) { 362 ofTrue(StrUtil.hasBlank(obj), BaseResultEnum.ERROR_PARAM); 363 } 364 365 public static void ofAnyBlank(String msg, String... obj) { 366 ofTrue(StrUtil.hasBlank(obj), BaseResultEnum.ERROR_PARAM.getCode(), msg); 367 } 368 369 public static void ofAnyBlank(String code, String msg, String... obj) { 370 ofTrue(StrUtil.hasBlank(obj), code, msg); 371 } 372 373 public static void ofBlank(BaseResultEnum resultEnum, String... obj) { 374 ofTrue(StrUtil.hasBlank(obj), resultEnum); 375 } 376 377 public static void ofAllBlank(String... obj) { 378 ofTrue(StrUtil.isAllBlank(obj), BaseResultEnum.ERROR_PARAM); 379 } 380 381 public static void ofAllBlank(String msg, String... obj) { 382 ofTrue(StrUtil.isAllBlank(obj), BaseResultEnum.ERROR_PARAM.getCode(), msg); 383 } 384 385 public static void ofAllBlank(String code, String msg, String... obj) { 386 ofTrue(StrUtil.isAllBlank(obj), code, msg); 387 } 388 389 public static void ofAllBlank(BaseResultEnum resultEnum, String... obj) { 390 ofTrue(StrUtil.isAllBlank(obj), resultEnum); 391 } 392 393 public static void ofAllNotBlank(String... obj) { 394 ofTrue(StrUtil.isAllNotBlank(obj), BaseResultEnum.ERROR_PARAM); 395 } 396 397 public static void ofAllNotBlank(String msg, String... obj) { 398 ofTrue(StrUtil.isAllNotBlank(obj), BaseResultEnum.ERROR_PARAM.getCode(), msg); 399 } 400 401 public static void ofAllNotBlank(String code, String msg, String... obj) { 402 ofTrue(StrUtil.isAllNotBlank(obj), code, msg); 403 } 404 405 public static void ofAllNotBlank(BaseResultEnum resultEnum, String... obj) { 406 ofTrue(StrUtil.isAllNotBlank(obj), resultEnum); 407 } 408}