Skip to content

Commit

Permalink
refactor: ♻️ 错误状态码重构为全局错误码(http状态码)&业务错误码(业务方自定义,只提供接口定义)
Browse files Browse the repository at this point in the history
  • Loading branch information
fxbin committed Sep 1, 2023
1 parent f4d39db commit ccca26b
Show file tree
Hide file tree
Showing 22 changed files with 817 additions and 224 deletions.
17 changes: 9 additions & 8 deletions bubble-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<!-- knife4j -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-core</artifactId>
</dependency>
<!-- swagger-annotations-jakarta -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
Expand All @@ -38,10 +33,10 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!-- guava -->
<!-- google auto-service -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</dependency>
<!-- validator -->
<dependency>
Expand All @@ -67,6 +62,12 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- junit -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cn.fxbin.bubble.core.dataobject;

/**
* BizErrorCode
*
* <p>
* 业务状态码定义,业务方可做相应实现
* </p>
*
* @author fxbin
* @version v1.0
* @since 2023/8/29 00:15
*/
public non-sealed interface BizErrorCode extends ErrorCode {

/**
* 错误码解析
*
* @param errorCode 错误码
* @return {@link BizErrorCode}
*/
BizErrorCode resolve(int errorCode);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cn.fxbin.bubble.core.dataobject;


import java.io.Serializable;

/**
* 错误代码
* ErrorCode
*
* <p>x
* 分为两类:
* 全局错误码(参照 {@link org.springframework.http.HttpStatus} 实现),实现为: {@code GlobalErrorCode} <br/>
* 业务错误码 {@code BizErrorCode} <br/><br/>
* <p>
* 参照:<a href="https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status">HTTP 响应状态码</a>
* </p>
*
* @author fxbin
* @version v1.0
* @since 2023/08/29 00:44
*/

public sealed interface ErrorCode extends Serializable permits GlobalErrorCode, BizErrorCode {

/**
* Return the integer value of this status code.
*/
int value();

/**
* Return the String value of error reason phrase.
*/
String reasonPhrase();

/**
* Whether this status code is in the Informational class ({@code 1xx}).
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.1">RFC 2616</a>
*/
default boolean is1xxInformational() {
return false;
};

/**
* Whether this status code is in the Successful class ({@code 2xx}).
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.2">RFC 2616</a>
*/
default boolean is2xxSuccessful() {
return false;
}

/**
* Whether this status code is in the Redirection class ({@code 3xx}).
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.3">RFC 2616</a>
*/
default boolean is3xxRedirection() {
return false;
}

/**
* Whether this status code is in the Client Error class ({@code 4xx}).
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.4">RFC 2616</a>
*/
default boolean is4xxClientError() {
return false;
}

/**
* Whether this status code is in the Server Error class ({@code 5xx}).
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.5">RFC 2616</a>
*/
default boolean is5xxServerError() {
return false;
}

/**
* Whether this status code is in the Client or Server Error class
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.4">RFC 2616</a>
* @see <a href="https://datatracker.ietf.org/doc/html/rfc2616#section-10.3">RFC 2616</a>
* ({@code 4xx} or {@code 5xx}).
* @see #is4xxClientError()
* @see #is5xxServerError()
*/
boolean isError();

default boolean isSameCodeAs(ErrorCode other) {
return value() == other.value();
}

static ErrorCode valueOf(int code) {
GlobalErrorCode errorCode = GlobalErrorCode.resolve(code);
if (errorCode != null) {
return errorCode;
}

// TODO 实现对业务错误码的判断

return null;
}

}
Loading

0 comments on commit ccca26b

Please sign in to comment.