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