update 增强 Mybatis 异常处理,添加根因查找功能

This commit is contained in:
AprilWind 2025-09-26 14:41:32 +08:00
parent 6036f8750b
commit 5c634940c2
2 changed files with 49 additions and 8 deletions

View File

@ -14,6 +14,7 @@ import java.util.concurrent.*;
@Slf4j @Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Threads { public class Threads {
/** /**
* 停止线程池 * 停止线程池
* 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务. * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
@ -60,4 +61,42 @@ public class Threads {
log.error(t.getMessage(), t); log.error(t.getMessage(), t);
} }
} }
/**
* 获取异常的根因递归查找
*
* @param e 当前异常
* @return 根因异常最底层的 cause
* <p>
* 逻辑说明
* 1. 如果 e 没有 cause说明 e 本身就是根因直接返回
* 2. 如果 e cause 和自身相同防止循环引用也返回 e
* 3. 否则递归调用继续向下寻找最底层的 cause
*/
public static Throwable getRootCause(Throwable e) {
Throwable cause = e.getCause();
if (cause == null || cause == e) {
return e;
}
return getRootCause(cause);
}
/**
* 在异常链中查找指定类型的异常
*
* @param e 当前异常
* @param clazz 目标异常类
* @return 找到的指定类型异常如果没有找到返回 null
*/
public static Throwable findCause(Throwable e, Class<? extends Throwable> clazz) {
Throwable t = e;
while (t != null && t != t.getCause()) {
if (clazz.isInstance(t)) {
return t;
}
t = t.getCause();
}
return null;
}
} }

View File

@ -1,10 +1,12 @@
package org.dromara.common.mybatis.handler; package org.dromara.common.mybatis.handler;
import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.http.HttpStatus; import cn.hutool.http.HttpStatus;
import com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R; import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.Threads;
import org.mybatis.spring.MyBatisSystemException; import org.mybatis.spring.MyBatisSystemException;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@ -35,17 +37,17 @@ public class MybatisExceptionHandler {
@ExceptionHandler(MyBatisSystemException.class) @ExceptionHandler(MyBatisSystemException.class)
public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) { public R<Void> handleCannotFindDataSourceException(MyBatisSystemException e, HttpServletRequest request) {
String requestURI = request.getRequestURI(); String requestURI = request.getRequestURI();
String message = e.getMessage(); Throwable root = Threads.getRootCause(e);
if (StringUtils.contains(message, "CannotFindDataSourceException")) { if (root instanceof NotLoginException) {
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, root.getMessage());
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
}
if (root instanceof CannotFindDataSourceException) {
log.error("请求地址'{}', 未找到数据源", requestURI); log.error("请求地址'{}', 未找到数据源", requestURI);
return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "未找到数据源,请联系管理员确认"); return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "未找到数据源,请联系管理员确认");
} }
if (StringUtils.contains(message, "NotLoginException")) {
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
}
log.error("请求地址'{}', Mybatis系统异常", requestURI, e); log.error("请求地址'{}', Mybatis系统异常", requestURI, e);
return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, message); return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, e.getMessage());
} }
} }