From d0ae65d40c43c5ac0c397d3e767e12da3e7cee78 Mon Sep 17 00:00:00 2001 From: CoderKK Date: Mon, 4 Aug 2025 17:34:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 BaseException基础异常类,统一错误码和错误消息处理 -重构 BusinessException 类,继承自 BaseException - 更新全局异常处理逻辑,优先使用异常中的错误消息 --- .../base/common/exception/BaseException.java | 33 +++++++++++++++ .../common/exception/BusinessException.java | 41 ++++++++++++------- .../base/handler/GlobalExceptionHandler.java | 6 ++- 3 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BaseException.java diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BaseException.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BaseException.java new file mode 100644 index 00000000..1ebe3990 --- /dev/null +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BaseException.java @@ -0,0 +1,33 @@ +package net.lab1024.sa.base.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import net.lab1024.sa.base.common.code.ErrorCode; + +import java.io.Serial; + +/** + * 基础异常 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = true) +public class BaseException extends RuntimeException { + + @Serial + private static final long serialVersionUID = 1L; + + /** + * 错误码 + */ + private ErrorCode errorCode; + + /** + * 错误消息 + */ + private String message; + +} diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BusinessException.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BusinessException.java index 481210d2..3a4ec257 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BusinessException.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/exception/BusinessException.java @@ -1,38 +1,49 @@ package net.lab1024.sa.base.common.exception; import net.lab1024.sa.base.common.code.ErrorCode; +import net.lab1024.sa.base.common.code.SystemErrorCode; /** - * 业务逻辑异常,全局异常拦截后统一返回ResponseCodeConst.SYSTEM_ERROR + * 业务逻辑异常,全局异常拦截后统一返回 * * @Author 1024创新实验室: 罗伊 * @Date 2020/8/25 21:57 * @Wechat zhuoda1024 * @Email lab1024@163.com - * @Copyright 1024创新实验室 + * @Copyright 1024创新实验室 */ -public class BusinessException extends RuntimeException { +public class BusinessException extends BaseException { public BusinessException() { + super(SystemErrorCode.SYSTEM_ERROR, null); } - public BusinessException(ErrorCode errorCode) { - super(errorCode.getMsg()); - } - + /** + * 业务异常 + * + * @param message 错误提示 + */ public BusinessException(String message) { - super(message); + super(SystemErrorCode.SYSTEM_ERROR, message); } - public BusinessException(String message, Throwable cause) { - super(message, cause); + /** + * 业务异常 + * + * @param errorCode 错误码 + */ + public BusinessException(ErrorCode errorCode) { + super(errorCode, null); } - public BusinessException(Throwable cause) { - super(cause); + /** + * 业务异常 + * + * @param errorCode 错误码 + * @param message 错误提示 + */ + public BusinessException(ErrorCode errorCode, String message) { + super(errorCode, message); } - public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } } diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/handler/GlobalExceptionHandler.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/handler/GlobalExceptionHandler.java index 95e99651..253da2d1 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/handler/GlobalExceptionHandler.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/handler/GlobalExceptionHandler.java @@ -1,6 +1,7 @@ package net.lab1024.sa.base.handler; import cn.dev33.satoken.exception.NotPermissionException; +import cn.hutool.core.util.StrUtil; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import net.lab1024.sa.base.common.code.SystemErrorCode; @@ -99,7 +100,10 @@ public class GlobalExceptionHandler { if (!systemEnvironment.isProd()) { log.error("全局业务异常,URL:{}", getCurrentRequestUrl(), e); } - return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, e.getMessage()); + if (StrUtil.isNotEmpty(e.getMessage())) { + return ResponseDTO.error(e.getErrorCode(), e.getMessage()); + } + return ResponseDTO.error(e.getErrorCode()); } /**