mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 19:06:39 +08:00
refactor(exception): 重构异常处理逻辑
- 新增 BaseException基础异常类,统一错误码和错误消息处理 -重构 BusinessException 类,继承自 BaseException - 更新全局异常处理逻辑,优先使用异常中的错误消息
This commit is contained in:
parent
acecebf4b3
commit
d0ae65d40c
@ -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;
|
||||
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
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
|
||||
@ -11,28 +12,38 @@ import net.lab1024.sa.base.common.code.ErrorCode;
|
||||
* @Email lab1024@163.com
|
||||
* @Copyright <a href="https://1024lab.net">1024创新实验室</a>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user