diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java index c1517e7082e2d66886e860875d7db99ab735eb41..cc9332cf20b1c46f1abad8aecb9697b07175a431 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java @@ -26,6 +26,11 @@ public enum UnexpectedErrorCode implements ErrorCode { */ PAY_ORDER_ID_ERROR(20002, "付款单id发生了异常,请联系技术人员排查"), + /** + * 错误码错误 + */ + UNKNOWN_ERROR_CODE(20004, "错误码竟然没有找到,请联系技术人员排查"), + ; private final int code; diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..2102b2bd2f02ddf507d434b94d23949add74f61a --- /dev/null +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java @@ -0,0 +1,97 @@ +package net.lab1024.sa.base.common.util; + +import lombok.extern.slf4j.Slf4j; +import net.lab1024.sa.base.common.code.ErrorCode; +import net.lab1024.sa.base.common.code.SystemErrorCode; +import net.lab1024.sa.base.common.code.UnexpectedErrorCode; +import net.lab1024.sa.base.common.code.UserErrorCode; + +/** + * 错误码工具类 + * + * @author zhaoshuo + * @version 1.0 + * @since 2024/10/25 + */ +@Slf4j +public class SmartErrorCodeUtils { + + /** + * 默认的 ErrorCode 枚举类数组 + * + * @author zhaoshuo + * @since 2024/10/25 + **/ + private static final Class[] DEFAULT_ERROR_CODE_ENUMS = new Class[]{ + UserErrorCode.class, + SystemErrorCode.class, + UnexpectedErrorCode.class + }; + + /** + * 根据指定的错误码查找,使用默认的 errorCodeEnums + * + * @param code + * @return net.lab1024.sa.base.common.code.ErrorCode + * @author zhaoshuo + * @since 2024/10/25 + **/ + public static ErrorCode findErrorCodeByCode(int code) { + return findErrorCodeByCode(code, DEFAULT_ERROR_CODE_ENUMS); + } + + /** + * 根据指定的错误码查找,使用传入的 errorCodeEnums + * + * @param code + * @param errorCodeEnums + * @return net.lab1024.sa.base.common.code.ErrorCode + * @author zhaoshuo + * @since 2024/10/25 + **/ + @SafeVarargs + public static ErrorCode findErrorCodeByCode(int code, Class... errorCodeEnums) { + for (Class errorCodeEnum : errorCodeEnums) { + for (ErrorCode errorCode : errorCodeEnum.getEnumConstants()) { + if (errorCode.getCode() == code) { + return errorCode; + } + } + } + // 未找到对应的错误码 + return UnexpectedErrorCode.UNKNOWN_ERROR_CODE; + } + + /** + * 打印所有默认的 errorCodeEnums 中的错误码 + * + * @return void + * @author zhaoshuo + * @since 2024/10/25 + **/ + public static void printAllErrorCodes() { + printAllErrorCodes(DEFAULT_ERROR_CODE_ENUMS); + } + + /** + * 打印指定的 errorCodeEnums 中的错误码 + * + * @param errorCodeEnums + * @return void + * @author zhaoshuo + * @since 2024/10/25 + **/ + @SafeVarargs + public static void printAllErrorCodes(Class... errorCodeEnums) { + log.info("All Error Codes:"); + for (Class errorCodeEnum : errorCodeEnums) { + log.info("Error Codes in {}:", errorCodeEnum.getSimpleName()); + for (ErrorCode errorCode : errorCodeEnum.getEnumConstants()) { + log.info("Code: {}, Message: {}", errorCode.getCode(), errorCode.getMsg()); + } + // 空行分隔每个枚举类的输出 + log.info(""); + } + } + +} \ No newline at end of file diff --git a/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java b/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java index c1517e7082e2d66886e860875d7db99ab735eb41..cc9332cf20b1c46f1abad8aecb9697b07175a431 100644 --- a/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java +++ b/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/code/UnexpectedErrorCode.java @@ -26,6 +26,11 @@ public enum UnexpectedErrorCode implements ErrorCode { */ PAY_ORDER_ID_ERROR(20002, "付款单id发生了异常,请联系技术人员排查"), + /** + * 错误码错误 + */ + UNKNOWN_ERROR_CODE(20004, "错误码竟然没有找到,请联系技术人员排查"), + ; private final int code; diff --git a/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java b/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..2102b2bd2f02ddf507d434b94d23949add74f61a --- /dev/null +++ b/smart-admin-api/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartErrorCodeUtils.java @@ -0,0 +1,97 @@ +package net.lab1024.sa.base.common.util; + +import lombok.extern.slf4j.Slf4j; +import net.lab1024.sa.base.common.code.ErrorCode; +import net.lab1024.sa.base.common.code.SystemErrorCode; +import net.lab1024.sa.base.common.code.UnexpectedErrorCode; +import net.lab1024.sa.base.common.code.UserErrorCode; + +/** + * 错误码工具类 + * + * @author zhaoshuo + * @version 1.0 + * @since 2024/10/25 + */ +@Slf4j +public class SmartErrorCodeUtils { + + /** + * 默认的 ErrorCode 枚举类数组 + * + * @author zhaoshuo + * @since 2024/10/25 + **/ + private static final Class[] DEFAULT_ERROR_CODE_ENUMS = new Class[]{ + UserErrorCode.class, + SystemErrorCode.class, + UnexpectedErrorCode.class + }; + + /** + * 根据指定的错误码查找,使用默认的 errorCodeEnums + * + * @param code + * @return net.lab1024.sa.base.common.code.ErrorCode + * @author zhaoshuo + * @since 2024/10/25 + **/ + public static ErrorCode findErrorCodeByCode(int code) { + return findErrorCodeByCode(code, DEFAULT_ERROR_CODE_ENUMS); + } + + /** + * 根据指定的错误码查找,使用传入的 errorCodeEnums + * + * @param code + * @param errorCodeEnums + * @return net.lab1024.sa.base.common.code.ErrorCode + * @author zhaoshuo + * @since 2024/10/25 + **/ + @SafeVarargs + public static ErrorCode findErrorCodeByCode(int code, Class... errorCodeEnums) { + for (Class errorCodeEnum : errorCodeEnums) { + for (ErrorCode errorCode : errorCodeEnum.getEnumConstants()) { + if (errorCode.getCode() == code) { + return errorCode; + } + } + } + // 未找到对应的错误码 + return UnexpectedErrorCode.UNKNOWN_ERROR_CODE; + } + + /** + * 打印所有默认的 errorCodeEnums 中的错误码 + * + * @return void + * @author zhaoshuo + * @since 2024/10/25 + **/ + public static void printAllErrorCodes() { + printAllErrorCodes(DEFAULT_ERROR_CODE_ENUMS); + } + + /** + * 打印指定的 errorCodeEnums 中的错误码 + * + * @param errorCodeEnums + * @return void + * @author zhaoshuo + * @since 2024/10/25 + **/ + @SafeVarargs + public static void printAllErrorCodes(Class... errorCodeEnums) { + log.info("All Error Codes:"); + for (Class errorCodeEnum : errorCodeEnums) { + log.info("Error Codes in {}:", errorCodeEnum.getSimpleName()); + for (ErrorCode errorCode : errorCodeEnum.getEnumConstants()) { + log.info("Code: {}, Message: {}", errorCode.getCode(), errorCode.getMsg()); + } + // 空行分隔每个枚举类的输出 + log.info(""); + } + } + +} \ No newline at end of file