Skip to content

Commit ccca26b

Browse files
committed
refactor: ♻️ 错误状态码重构为全局错误码(http状态码)&业务错误码(业务方自定义,只提供接口定义)
1 parent f4d39db commit ccca26b

File tree

22 files changed

+817
-224
lines changed

22 files changed

+817
-224
lines changed

bubble-core/pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
<groupId>jakarta.servlet</groupId>
2424
<artifactId>jakarta.servlet-api</artifactId>
2525
</dependency>
26-
<!-- knife4j -->
27-
<dependency>
28-
<groupId>com.github.xiaoymin</groupId>
29-
<artifactId>knife4j-core</artifactId>
30-
</dependency>
3126
<!-- swagger-annotations-jakarta -->
3227
<dependency>
3328
<groupId>io.swagger.core.v3</groupId>
@@ -38,10 +33,10 @@
3833
<groupId>cn.hutool</groupId>
3934
<artifactId>hutool-all</artifactId>
4035
</dependency>
41-
<!-- guava -->
36+
<!-- google auto-service -->
4237
<dependency>
43-
<groupId>com.google.guava</groupId>
44-
<artifactId>guava</artifactId>
38+
<groupId>com.google.auto.service</groupId>
39+
<artifactId>auto-service</artifactId>
4540
</dependency>
4641
<!-- validator -->
4742
<dependency>
@@ -67,6 +62,12 @@
6762
<groupId>org.projectlombok</groupId>
6863
<artifactId>lombok</artifactId>
6964
</dependency>
65+
<!-- junit -->
66+
<dependency>
67+
<groupId>org.junit.jupiter</groupId>
68+
<artifactId>junit-jupiter</artifactId>
69+
<scope>test</scope>
70+
</dependency>
7071
</dependencies>
7172

7273
</project>

bubble-core/src/main/java/cn/fxbin/bubble/core/constant/YesOrNo.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.fxbin.bubble.core.dataobject;
2+
3+
/**
4+
* BizErrorCode
5+
*
6+
* <p>
7+
* 业务状态码定义,业务方可做相应实现
8+
* </p>
9+
*
10+
* @author fxbin
11+
* @version v1.0
12+
* @since 2023/8/29 00:15
13+
*/
14+
public non-sealed interface BizErrorCode extends ErrorCode {
15+
16+
/**
17+
* 错误码解析
18+
*
19+
* @param errorCode 错误码
20+
* @return {@link BizErrorCode}
21+
*/
22+
BizErrorCode resolve(int errorCode);
23+
24+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package cn.fxbin.bubble.core.dataobject;
2+
3+
4+
import java.io.Serializable;
5+
6+
/**
7+
* 错误代码
8+
* ErrorCode
9+
*
10+
* <p>x
11+
* 分为两类:
12+
* 全局错误码(参照 {@link org.springframework.http.HttpStatus} 实现),实现为: {@code GlobalErrorCode} <br/>
13+
* 业务错误码 {@code BizErrorCode} <br/><br/>
14+
* <p>
15+
* 参照:<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status">HTTP 响应状态码</a>
16+
* </p>
17+
*
18+
* @author fxbin
19+
* @version v1.0
20+
* @since 2023/08/29 00:44
21+
*/
22+
23+
public sealed interface ErrorCode extends Serializable permits GlobalErrorCode, BizErrorCode {
24+
25+
/**
26+
* Return the integer value of this status code.
27+
*/
28+
int value();
29+
30+
/**
31+
* Return the String value of error reason phrase.
32+
*/
33+
String reasonPhrase();
34+
35+
/**
36+
* Whether this status code is in the Informational class ({@code 1xx}).
37+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.1">RFC 2616</a>
38+
*/
39+
default boolean is1xxInformational() {
40+
return false;
41+
};
42+
43+
/**
44+
* Whether this status code is in the Successful class ({@code 2xx}).
45+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.2">RFC 2616</a>
46+
*/
47+
default boolean is2xxSuccessful() {
48+
return false;
49+
}
50+
51+
/**
52+
* Whether this status code is in the Redirection class ({@code 3xx}).
53+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.3">RFC 2616</a>
54+
*/
55+
default boolean is3xxRedirection() {
56+
return false;
57+
}
58+
59+
/**
60+
* Whether this status code is in the Client Error class ({@code 4xx}).
61+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.4">RFC 2616</a>
62+
*/
63+
default boolean is4xxClientError() {
64+
return false;
65+
}
66+
67+
/**
68+
* Whether this status code is in the Server Error class ({@code 5xx}).
69+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.5">RFC 2616</a>
70+
*/
71+
default boolean is5xxServerError() {
72+
return false;
73+
}
74+
75+
/**
76+
* Whether this status code is in the Client or Server Error class
77+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.4">RFC 2616</a>
78+
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.3">RFC 2616</a>
79+
* ({@code 4xx} or {@code 5xx}).
80+
* @see #is4xxClientError()
81+
* @see #is5xxServerError()
82+
*/
83+
boolean isError();
84+
85+
default boolean isSameCodeAs(ErrorCode other) {
86+
return value() == other.value();
87+
}
88+
89+
static ErrorCode valueOf(int code) {
90+
GlobalErrorCode errorCode = GlobalErrorCode.resolve(code);
91+
if (errorCode != null) {
92+
return errorCode;
93+
}
94+
95+
// TODO 实现对业务错误码的判断
96+
97+
return null;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)