Skip to content

Commit

Permalink
refactor: 🎨 错误码定义验证实现
Browse files Browse the repository at this point in the history
  • Loading branch information
fxbin committed Sep 18, 2023
1 parent 68389cd commit f846094
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public interface CharPool {
*/
char UNDERLINE = '_';

/**
* 字符常量:冒号 {@code ':'}
*/
char COLON = ':';

/**
* 字符常量:艾特 {@code '@'}
*/
char AT = '@';

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public interface StringPool {
*/
String COMMA = ",";

/**
* 冒号
*/
String COLON = ":";

/**
* 点
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.io.Serializable;
import java.util.ServiceLoader;

/**
* 错误代码
Expand Down Expand Up @@ -80,19 +81,64 @@ default boolean is5xxServerError() {
* @see #is4xxClientError()
* @see #is5xxServerError()
*/
boolean isError();
default boolean isError() {
return true;
};

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

/**
* 值
*
* 这是一种示例,关于BizErrorCodee 实现的
*
* @AutoService(BizErrorCode.class)
* public class AppErrorCode implements BizErrorCode {
* public static final AppErrorCode USERNAME_FORMAT_ERROR = new AppErrorCode(1000000, "用户名格式有误");
* private int value;
* private String reasonPhrase;
* public AppErrorCode() {}
* public AppErrorCode(int value, String reasonPhrase) {
* this.value = value;
* this.reasonPhrase = reasonPhrase;
* }
* @Override
* public int value() {
* return this.value;
* }
* @Override
* public String reasonPhrase() {
* return this.reasonPhrase;
* }
* public BizErrorCode resolve(int errorCode) {
* return Arrays.stream(ReflectUtil.getFields(AppErrorCode.class))
* .filter(field -> !"value".equals(field.getName()) && !"reasonPhrase".equals(field.getName()))
* .filter(field -> ((AppErrorCode) ReflectUtil.getStaticFieldValue(field)).value == errorCode)
* .map(field -> ((AppErrorCode) ReflectUtil.getStaticFieldValue(field)))
* .findAny().orElse(null);
* }
* }
*
* @param code code
* @return {@link ErrorCode}
*/
static ErrorCode valueOf(int code) {
GlobalErrorCode errorCode = GlobalErrorCode.resolve(code);
if (errorCode != null) {
return errorCode;
}

// TODO 实现对业务错误码的判断
// BizErrorCode 实现类的一种可行的示例:
//
ServiceLoader<BizErrorCode> errCodeLoader = ServiceLoader.load(BizErrorCode.class);
for (BizErrorCode codeLoader : errCodeLoader) {
BizErrorCode bizErrorCode = codeLoader.resolve(code);
if (bizErrorCode.value() == code) {
return bizErrorCode;
}
}

return null;
}
Expand Down

0 comments on commit f846094

Please sign in to comment.