Skip to content

Commit 5d7fef2

Browse files
committed
feat: 优化顶级异常,支持异常信息字符串模板
1 parent df8bb93 commit 5d7fef2

File tree

4 files changed

+79
-41
lines changed

4 files changed

+79
-41
lines changed

common/core/src/main/java/com/github/cadecode/uniboot/common/core/exception/ApiException.java

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.cadecode.uniboot.common.core.exception;
22

3+
import cn.hutool.core.util.StrUtil;
34
import com.github.cadecode.uniboot.common.core.enums.ApiErrorCode;
45
import lombok.Getter;
56

@@ -12,7 +13,7 @@
1213
* @date 2022/5/8
1314
*/
1415
@Getter
15-
public class ApiException extends RuntimeException {
16+
public class ApiException extends BaseException {
1617

1718
/**
1819
* 错误信息码
@@ -24,36 +25,53 @@ public class ApiException extends RuntimeException {
2425
*/
2526
private final String moreInfo;
2627

28+
/**
29+
* 构造方法
30+
*
31+
* @param errorCode 错误信息码
32+
* @param throwable cause
33+
* @param moreInfo 更多异常信息
34+
* @param params 字符串模板参数
35+
*/
36+
public ApiException(ApiErrorCode errorCode, Throwable throwable, String moreInfo, Object... params) {
37+
super(geneErrorMessage(errorCode, moreInfo, params), throwable);
38+
this.errorCode = errorCode;
39+
this.moreInfo = StrUtil.format(moreInfo, params);
40+
}
41+
2742
/**
2843
* 抛出未知异常
2944
*
3045
* @param moreInfo 更多异常信息
46+
* @param params 字符串模板参数
3147
* @return ApiException
3248
*/
33-
public static ApiException of(String moreInfo) {
34-
return of(ApiErrorCode.UNKNOWN, moreInfo);
49+
public static ApiException of(String moreInfo, Object... params) {
50+
return of(ApiErrorCode.UNKNOWN, moreInfo, params);
3551
}
3652

3753
/**
3854
* 抛出未知异常
3955
*
4056
* @param throwable cause
4157
* @param moreInfo 更多异常信息
58+
* @param params 字符串模板参数
4259
* @return ApiException
4360
*/
44-
public static ApiException of(Throwable throwable, String moreInfo) {
45-
return of(ApiErrorCode.UNKNOWN, throwable, moreInfo);
61+
public static ApiException of(Throwable throwable, String moreInfo, Object... params) {
62+
return of(ApiErrorCode.UNKNOWN, throwable, moreInfo, params);
4663
}
4764

4865
/**
4966
* 根据 ApiErrorCode 抛出异常
5067
*
5168
* @param errorCode 错误信息码
5269
* @param moreInfo 更多异常信息
70+
* @param params 字符串模板参数
5371
* @return ApiException
5472
*/
55-
public static ApiException of(ApiErrorCode errorCode, String moreInfo) {
56-
return of(errorCode, null, moreInfo);
73+
public static ApiException of(ApiErrorCode errorCode, String moreInfo, Object... params) {
74+
return of(errorCode, null, moreInfo, params);
5775
}
5876

5977
/**
@@ -73,23 +91,13 @@ public static ApiException of(ApiErrorCode errorCode, Throwable throwable) {
7391
* @param errorCode 错误信息码
7492
* @param throwable cause
7593
* @param moreInfo 更多异常信息
94+
* @param params 字符串模板参数
7695
* @return ApiException
7796
*/
78-
public static ApiException of(ApiErrorCode errorCode, Throwable throwable, String moreInfo) {
79-
return new ApiException(errorCode, throwable, moreInfo);
80-
}
81-
82-
/**
83-
* 私有构造
84-
*
85-
* @param errorCode 错误信息码
86-
* @param throwable cause
87-
* @param moreInfo 更多异常信息
88-
*/
89-
private ApiException(ApiErrorCode errorCode, Throwable throwable, String moreInfo) {
90-
super(generateMessage(errorCode, moreInfo), throwable);
91-
this.errorCode = errorCode;
92-
this.moreInfo = moreInfo;
97+
public static ApiException of(ApiErrorCode errorCode, Throwable throwable, String moreInfo, Object... params) {
98+
// 这里直接调用 StrUtil.format 得到 moreInfo
99+
// 避免在构造方法中重复 format 解析字符串模板
100+
return new ApiException(errorCode, throwable, StrUtil.format(moreInfo, params));
93101
}
94102

95103
/**
@@ -99,15 +107,15 @@ private ApiException(ApiErrorCode errorCode, Throwable throwable, String moreInf
99107
* @param moreInfo 更多异常信息
100108
* @return 完整异常信息
101109
*/
102-
private static String generateMessage(ApiErrorCode errorCode, String moreInfo) {
110+
private static String geneErrorMessage(ApiErrorCode errorCode, String moreInfo, Object... params) {
103111
String message = "";
104-
// 拼接 [错误码-错误信息]
112+
// 拼接 [错误码:错误信息]
105113
if (Objects.nonNull(errorCode)) {
106-
message += "[" + errorCode.getCode() + "-" + errorCode.getMessage() + "]";
114+
message += "[" + errorCode.getCode() + ":" + errorCode.getMessage() + "]";
107115
}
108116
// 拼接更多异常信息
109117
if (Objects.nonNull(moreInfo)) {
110-
message += moreInfo;
118+
message += StrUtil.format(moreInfo, params);
111119
}
112120
return message;
113121
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.cadecode.uniboot.common.core.exception;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
5+
/**
6+
* 基础异常类
7+
*<p>message 支持字符串模板,由 hutool {@code StrUtil.format} 提供
8+
*
9+
* @author Cade Li
10+
* @since 2024/4/23
11+
*/
12+
public class BaseException extends RuntimeException {
13+
public BaseException() {
14+
}
15+
16+
public BaseException(String message, Object... params) {
17+
super(StrUtil.format(message, params));
18+
}
19+
20+
public BaseException(String message, Throwable cause, Object... params) {
21+
super(StrUtil.format(message, params), cause);
22+
}
23+
24+
public BaseException(Throwable cause) {
25+
super(cause);
26+
}
27+
28+
public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Object... params) {
29+
super(StrUtil.format(message, params), cause, enableSuppression, writableStackTrace);
30+
}
31+
}

common/core/src/main/java/com/github/cadecode/uniboot/common/core/exception/ExtException.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,23 @@
66
* @author Cade Li
77
* @since 2023/6/24
88
*/
9-
public class ExtException extends RuntimeException {
9+
public class ExtException extends BaseException {
1010
public ExtException() {
11-
super();
1211
}
1312

14-
public ExtException(String message) {
15-
super(message);
13+
public ExtException(String message, Object... params) {
14+
super(message, params);
1615
}
1716

18-
public ExtException(String message, Throwable cause) {
19-
super(message, cause);
17+
public ExtException(String message, Throwable cause, Object... params) {
18+
super(message, cause, params);
2019
}
2120

2221
public ExtException(Throwable cause) {
2322
super(cause);
2423
}
2524

26-
protected ExtException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
27-
super(message, cause, enableSuppression, writableStackTrace);
25+
public ExtException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Object... params) {
26+
super(message, cause, enableSuppression, writableStackTrace, params);
2827
}
2928
}

common/core/src/main/java/com/github/cadecode/uniboot/common/core/exception/UtilException.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
* @author Cade Li
77
* @date 2023/6/9
88
*/
9-
public class UtilException extends RuntimeException {
9+
public class UtilException extends BaseException {
1010
public UtilException() {
1111
}
1212

13-
public UtilException(String message) {
14-
super(message);
13+
public UtilException(String message, Object... params) {
14+
super(message, params);
1515
}
1616

17-
public UtilException(String message, Throwable cause) {
18-
super(message, cause);
17+
public UtilException(String message, Throwable cause, Object... params) {
18+
super(message, cause, params);
1919
}
2020

2121
public UtilException(Throwable cause) {
2222
super(cause);
2323
}
2424

25-
public UtilException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
26-
super(message, cause, enableSuppression, writableStackTrace);
25+
public UtilException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Object... params) {
26+
super(message, cause, enableSuppression, writableStackTrace, params);
2727
}
2828
}

0 commit comments

Comments
 (0)