refactor(exception): 重构异常处理逻辑

- 新增 BaseException基础异常类,统一错误码和错误消息处理
-重构 BusinessException 类,继承自 BaseException
- 更新全局异常处理逻辑,优先使用异常中的错误消息
This commit is contained in:
CoderKK 2025-08-04 17:34:52 +08:00
parent acecebf4b3
commit d0ae65d40c
3 changed files with 64 additions and 16 deletions

View File

@ -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;
}

View File

@ -1,38 +1,49 @@
package net.lab1024.sa.base.common.exception; package net.lab1024.sa.base.common.exception;
import net.lab1024.sa.base.common.code.ErrorCode; import net.lab1024.sa.base.common.code.ErrorCode;
import net.lab1024.sa.base.common.code.SystemErrorCode;
/** /**
* 业务逻辑异常,全局异常拦截后统一返回ResponseCodeConst.SYSTEM_ERROR * 业务逻辑异常,全局异常拦截后统一返回
* *
* @Author 1024创新实验室: 罗伊 * @Author 1024创新实验室: 罗伊
* @Date 2020/8/25 21:57 * @Date 2020/8/25 21:57
* @Wechat zhuoda1024 * @Wechat zhuoda1024
* @Email lab1024@163.com * @Email lab1024@163.com
* @Copyright <a href="https://1024lab.net">1024创新实验室</a> * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
*/ */
public class BusinessException extends RuntimeException { public class BusinessException extends BaseException {
public BusinessException() { public BusinessException() {
super(SystemErrorCode.SYSTEM_ERROR, null);
} }
public BusinessException(ErrorCode errorCode) { /**
super(errorCode.getMsg()); * 业务异常
} *
* @param message 错误提示
*/
public BusinessException(String 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);
}
} }

View File

@ -1,6 +1,7 @@
package net.lab1024.sa.base.handler; package net.lab1024.sa.base.handler;
import cn.dev33.satoken.exception.NotPermissionException; import cn.dev33.satoken.exception.NotPermissionException;
import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.code.SystemErrorCode; import net.lab1024.sa.base.common.code.SystemErrorCode;
@ -99,7 +100,10 @@ public class GlobalExceptionHandler {
if (!systemEnvironment.isProd()) { if (!systemEnvironment.isProd()) {
log.error("全局业务异常,URL:{}", getCurrentRequestUrl(), e); 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());
} }
/** /**