Skip to content

Commit f5a1cef

Browse files
committed
feat: 添加响应码接口及枚举类,优化异常处理
1 parent ed0ebaa commit f5a1cef

12 files changed

Lines changed: 285 additions & 84 deletions

File tree

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package top.cadecode.common.core.exception;
22

33
import lombok.Getter;
4-
import top.cadecode.common.enums.ErrorCode;
4+
import top.cadecode.common.core.response.ResponseCode;
55

66
/**
77
* @author Cade Li
@@ -11,14 +11,56 @@
1111
@Getter
1212
public class CommonException extends RuntimeException {
1313

14-
private final ErrorCode errorCode;
14+
private final ResponseCode responseCode;
15+
public String errorMsg;
1516

16-
public CommonException(ErrorCode errorCode) {
17-
this.errorCode = errorCode;
17+
public CommonException(ResponseCode responseCode) {
18+
super(responseCode.getReason());
19+
this.responseCode = responseCode;
1820
}
1921

20-
public CommonException(ErrorCode errorCode, String errorMsg) {
21-
super(errorMsg);
22-
this.errorCode = errorCode;
22+
public CommonException(ResponseCode responseCode, String errorMsg) {
23+
this(responseCode);
24+
this.errorMsg = errorMsg;
25+
}
26+
27+
/**
28+
* 通过响应码构造通用异常对象
29+
*
30+
* @param responseCode 响应码
31+
* @return CommonException
32+
*/
33+
public static CommonException of(ResponseCode responseCode) {
34+
return new CommonException(responseCode);
35+
}
36+
37+
/**
38+
* 设置错误信息
39+
*
40+
* @param errorMsg 错误信息
41+
* @return CommonException
42+
*/
43+
public CommonException errorMsg(String errorMsg) {
44+
this.errorMsg = errorMsg;
45+
return this;
46+
}
47+
48+
/**
49+
* 添加原始异常
50+
*
51+
* @param throwable 原始异常
52+
* @return CommonException
53+
*/
54+
public CommonException suppressed(Throwable throwable) {
55+
addSuppressed(throwable);
56+
return this;
57+
}
58+
59+
@Override
60+
public String getMessage() {
61+
if (this.errorMsg == null) {
62+
return this.getResponseCode().getReason();
63+
}
64+
return "[" + this.getResponseCode().getReason() + "] " + this.errorMsg;
2365
}
2466
}

simple-common/src/main/java/top/cadecode/common/core/response/CommonResponse.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import lombok.AllArgsConstructor;
66
import lombok.Data;
77
import lombok.NoArgsConstructor;
8-
import top.cadecode.common.enums.ErrorCode;
98

109
/**
1110
* @author Cade Li
@@ -17,7 +16,7 @@
1716
@NoArgsConstructor
1817
public class CommonResponse<T> {
1918

20-
private Integer code;
19+
private String code;
2120
private String reason;
2221
private String path;
2322

@@ -34,33 +33,33 @@ public class CommonResponse<T> {
3433
* @return SimpleRes
3534
*/
3635
public static <T> CommonResponse<T> ok(T data) {
37-
return CommonResponse.of(ErrorCode.SUCCESS, data);
36+
return CommonResponse.of(ResponseCode.SUCCESS, data);
3837
}
3938

4039
/**
4140
* 根据 ResCode 返回响应
4241
*
43-
* @param errorCode 错误码
42+
* @param responseCode 响应码
4443
* @return SimpleRes
4544
*/
46-
public static CommonResponse<Object> of(ErrorCode errorCode) {
45+
public static CommonResponse<Object> of(ResponseCode responseCode) {
4746
CommonResponse<Object> res = new CommonResponse<>();
48-
res.setCode(errorCode.getCode());
49-
res.setReason(errorCode.getReason());
47+
res.setCode(responseCode.getCode());
48+
res.setReason(responseCode.getReason());
5049
return res;
5150
}
5251

5352
/**
5453
* 根据 ResCode 和 data 返回响应
5554
*
56-
* @param errorCode 错误码
57-
* @param data 数据
55+
* @param responseCode 错误码
56+
* @param data 数据
5857
* @return SimpleRes
5958
*/
60-
public static <T> CommonResponse<T> of(ErrorCode errorCode, T data) {
59+
public static <T> CommonResponse<T> of(ResponseCode responseCode, T data) {
6160
CommonResponse<T> res = new CommonResponse<>();
62-
res.setCode(errorCode.getCode());
63-
res.setReason(errorCode.getReason());
61+
res.setCode(responseCode.getCode());
62+
res.setReason(responseCode.getReason());
6463
res.setData(data);
6564
return res;
6665
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package top.cadecode.common.core.response;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* @author Cade Li
7+
* @date 2021/12/9
8+
* @description 响应码接口
9+
*/
10+
public interface ResponseCode {
11+
12+
// 执行成功的响应码
13+
ResponseCode SUCCESS = new ResponseCode() {
14+
@Override
15+
public String getCode() {
16+
return "SUCCESS";
17+
}
18+
19+
@Override
20+
public String getReason() {
21+
return "执行成功";
22+
}
23+
};
24+
25+
// 未知错误的响应码
26+
ResponseCode UNKNOWN = new ResponseCode() {
27+
@Override
28+
public String getCode() {
29+
return "UNKNOWN";
30+
}
31+
32+
@Override
33+
public String getReason() {
34+
return "未知错误";
35+
}
36+
};
37+
38+
String getCode();
39+
40+
String getReason();
41+
42+
43+
/**
44+
* 异常枚举类 Code 前缀
45+
*/
46+
@Getter
47+
enum CodePrefixEnum {
48+
49+
AUTH("A"),
50+
CLIENT("C"),
51+
FRAME("F"),
52+
SERVICE("S"),
53+
THIRD_PARTY("T"),
54+
;
55+
56+
private final String prefix;
57+
58+
CodePrefixEnum(String prefix) {
59+
this.prefix = prefix;
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package top.cadecode.common.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.common.core.response.ResponseCode;
5+
6+
/**
7+
* @author Cade Li
8+
* @date 2021/12/9
9+
* @description 授权异常响应码枚举类
10+
*/
11+
@Getter
12+
public enum AuthErrorEnum implements ResponseCode {
13+
14+
TOKEN_NOT_EXIST(1000, "未登录"),
15+
TOKEN_ERROR(1001, "Token 错误"),
16+
TOKEN_EXPIRED(1002, "Token 已过期"),
17+
TOKEN_NO_AUTHORITY(1003, "权限不足"),
18+
TOKEN_CREATE_ERROR(1004, "登录失败"),
19+
;
20+
21+
private final String code;
22+
private final String reason;
23+
24+
AuthErrorEnum(int index, String reason) {
25+
this.code = CodePrefixEnum.AUTH.getPrefix() + index;
26+
this.reason = reason;
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package top.cadecode.common.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.common.core.response.ResponseCode;
5+
6+
/**
7+
* @author Cade Li
8+
* @date 2021/12/9
9+
* @description 客户端异常响应码枚举类
10+
*/
11+
@Getter
12+
public enum ClientErrorEnum implements ResponseCode {
13+
14+
REQ_PARAM_INVALID(1000, "请求参数无效"),
15+
REQ_BODY_INVALID(1001, "请求体无效"),
16+
REQ_METHOD_INVALID(1002, "请求方法不支持"),
17+
REQ_PATH_NOT_EXIST(1003, "请求接口不存在"),
18+
;
19+
20+
private final String code;
21+
private final String reason;
22+
23+
ClientErrorEnum(int index, String reason) {
24+
this.code = CodePrefixEnum.CLIENT.getPrefix() + index;
25+
this.reason = reason;
26+
}
27+
}

simple-common/src/main/java/top/cadecode/common/enums/ErrorCode.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package top.cadecode.common.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.common.core.response.ResponseCode;
5+
6+
/**
7+
* @author Cade Li
8+
* @date 2021/12/9
9+
* @description 系统框架异常
10+
*/
11+
@Getter
12+
public enum FrameErrorEnum implements ResponseCode {
13+
14+
JSON_TO_STR_ERROR(1000, "对象转 json 字符串失败"),
15+
JSON_TO_OBJ_ERROR(1000, "json 字符串转对象失败"),
16+
;
17+
18+
private final String code;
19+
private final String reason;
20+
21+
FrameErrorEnum(int index, String reason) {
22+
this.code = CodePrefixEnum.FRAME.getPrefix() + index;
23+
this.reason = reason;
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package top.cadecode.common.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.common.core.response.ResponseCode;
5+
6+
/**
7+
* @author Cade Li
8+
* @date 2021/12/9
9+
* @description 服务异常响应码枚举类
10+
*/
11+
@Getter
12+
public enum ServiceErrorEnum implements ResponseCode {
13+
14+
RES_BODY_INVALID(10000, "响应数据为空"),
15+
;
16+
17+
private final String code;
18+
private final String reason;
19+
20+
ServiceErrorEnum(int index, String reason) {
21+
this.code = CodePrefixEnum.SERVICE.getPrefix() + index;
22+
this.reason = reason;
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package top.cadecode.common.enums;
2+
3+
import lombok.Getter;
4+
import top.cadecode.common.core.response.ResponseCode;
5+
6+
/**
7+
* @author Cade Li
8+
* @date 2021/12/9
9+
* @description 第三方服务异常响应码枚举类
10+
*/
11+
@Getter
12+
public enum ThirdPartyErrorEnum implements ResponseCode {
13+
14+
;
15+
16+
private final String code;
17+
private final String reason;
18+
19+
ThirdPartyErrorEnum(int index, String reason) {
20+
this.code = CodePrefixEnum.THIRD_PARTY.getPrefix() + index;
21+
this.reason = reason;
22+
}
23+
}

0 commit comments

Comments
 (0)