Skip to content

Commit 424eb9e

Browse files
committed
feat: 添加根据异常类获取 SimpleRes 对象的方法
1 parent c255af3 commit 424eb9e

1 file changed

Lines changed: 44 additions & 32 deletions

File tree

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package info.cadecode.simple.common.response;
22

3+
import info.cadecode.simple.common.exception.SimpleException;
34
import info.cadecode.simple.constant.ReasonEnum;
45
import info.cadecode.simple.util.JsonUtil;
56

@@ -14,24 +15,15 @@ public class SimpleRes {
1415
private String msg;
1516
private Object data;
1617

17-
/**
18-
* 返回执行成功的响应
19-
*
20-
* @param data 返回的数据
21-
* @return ResBuilder
22-
*/
23-
public static SimpleRes ok(Object data) {
24-
return new SimpleRes(ReasonEnum.OK, data);
18+
private SimpleRes(ReasonEnum reasonEnum) {
19+
this.code = reasonEnum.getCode();
20+
this.msg = reasonEnum.getMsg();
2521
}
2622

27-
/**
28-
* 返回执行失败的响应
29-
*
30-
* @param data 返回的数据
31-
* @return ResBuilder
32-
*/
33-
public static SimpleRes error(Object data) {
34-
return new SimpleRes(ReasonEnum.ERROR, data);
23+
24+
private SimpleRes(SimpleException exception) {
25+
this.code = exception.getCode();
26+
this.msg = exception.getMsg();
3527
}
3628

3729
/**
@@ -44,27 +36,35 @@ public static SimpleRes reason(ReasonEnum reasonEnum) {
4436
return new SimpleRes(reasonEnum);
4537
}
4638

47-
private SimpleRes(ReasonEnum reasonEnum) {
48-
this.code = reasonEnum.getCode();
49-
this.msg = reasonEnum.getMsg();
50-
}
5139

52-
private SimpleRes(ReasonEnum reasonEnum, Object data) {
53-
this.code = reasonEnum.getCode();
54-
this.msg = reasonEnum.getMsg();
55-
this.data = data;
56-
}
57-
58-
public Integer getCode() {
59-
return code;
40+
/**
41+
* 根据 SimpleException 绑定 code、msg
42+
*
43+
* @param exception Reason
44+
* @return ResBuilder
45+
*/
46+
public static SimpleRes exception(SimpleException exception) {
47+
return new SimpleRes(exception);
6048
}
6149

62-
public String getMsg() {
63-
return msg;
50+
/**
51+
* 返回执行成功的响应 200
52+
*
53+
* @param data 返回的数据
54+
* @return ResBuilder
55+
*/
56+
public static SimpleRes ok(Object data) {
57+
return reason(ReasonEnum.OK).data(data);
6458
}
6559

66-
public Object getData() {
67-
return data;
60+
/**
61+
* 返回执行失败的响应 500
62+
*
63+
* @param data 返回的数据
64+
* @return ResBuilder
65+
*/
66+
public static SimpleRes error(Object data) {
67+
return reason(ReasonEnum.ERROR).data(data);
6868
}
6969

7070
public SimpleRes code(Integer code) {
@@ -85,4 +85,16 @@ public SimpleRes data(Object data) {
8585
public String json() {
8686
return JsonUtil.objToStr(this);
8787
}
88+
89+
public Integer getCode() {
90+
return code;
91+
}
92+
93+
public String getMsg() {
94+
return msg;
95+
}
96+
97+
public Object getData() {
98+
return data;
99+
}
88100
}

0 commit comments

Comments
 (0)