Sfoglia il codice sorgente

恢复异常处理

Gaokun Wang 3 settimane fa
parent
commit
8231b7a7d6

+ 5 - 9
eco-common/com-orm/src/main/java/org/eco/vip/orm/listener/EntityInsertListener.java

@@ -11,8 +11,6 @@ import cn.hutool.http.HttpStatus;
 import com.mybatisflex.annotation.InsertListener;
 import org.eco.vip.orm.domain.BaseEntity;
 import org.eco.vip.orm.exception.BusinessException;
-import org.eco.vip.orm.pojo.LoginUserStorage;
-import org.eco.vip.orm.utils.ObjUtils;
 import org.eco.vip.security.utils.LoginHelper;
 
 import java.util.Date;
@@ -29,11 +27,8 @@ public class EntityInsertListener implements InsertListener {
     public void onInsert(Object entity) {
         try {
             if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
-                LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
-                String loginUserId = null;
-                if (ObjUtils.isNotNull(loginUserStorage)) {
-                    loginUserId = loginUserStorage.getUserId();
-                }
+                String loginUserId = LoginHelper.getUserId();
+                String tenantId = LoginHelper.getTenantId();
                 Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
                         ? baseEntity.getCreateTime() : new Date();
                 if (ObjectUtil.isNull(baseEntity.getCreateBy())) {
@@ -43,10 +38,11 @@ public class EntityInsertListener implements InsertListener {
                 if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
                     baseEntity.setUpdateBy(loginUserId);
                 }
+                baseEntity.setTenantId(tenantId);
                 baseEntity.setUpdateTime(createTime);
             }
-        } catch (Exception e) {
-            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局插入数据监听器注入异常 => {}", e.getMessage());
+        } catch (BusinessException e) {
+            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局插入数据监听器注入异常");
         }
     }
 }

+ 2 - 8
eco-common/com-orm/src/main/java/org/eco/vip/orm/listener/EntityUpdateListener.java

@@ -11,8 +11,6 @@ import cn.hutool.http.HttpStatus;
 import com.mybatisflex.annotation.UpdateListener;
 import org.eco.vip.orm.domain.BaseEntity;
 import org.eco.vip.orm.exception.BusinessException;
-import org.eco.vip.orm.pojo.LoginUserStorage;
-import org.eco.vip.orm.utils.ObjUtils;
 import org.eco.vip.security.utils.LoginHelper;
 
 import java.util.Date;
@@ -28,18 +26,14 @@ public class EntityUpdateListener implements UpdateListener {
     public void onUpdate(Object entity) {
         try {
             if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
-                LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
-                String loginUserId = null;
-                if (ObjUtils.isNotNull(loginUserStorage)) {
-                    loginUserId = loginUserStorage.getUserId();
-                }
+                String loginUserId = LoginHelper.getUserId();
                 if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
                     baseEntity.setUpdateBy(loginUserId);
                 }
                 baseEntity.setUpdateTime(new Date());
             }
         } catch (BusinessException e) {
-            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局更新数据监听器注入异常 => {} ", e.getMessage());
+            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局更新数据监听器注入异常");
         }
     }
 }

+ 30 - 24
eco-common/com-security/src/main/java/org/eco/vip/security/handler/GlobalExceptionHandler.java

@@ -18,6 +18,8 @@ import jakarta.validation.ValidationException;
 import lombok.extern.slf4j.Slf4j;
 import org.eco.vip.orm.exception.BusinessException;
 import org.eco.vip.orm.pojo.CommonResult;
+import org.eco.vip.orm.utils.StrUtils;
+import org.mybatis.spring.MyBatisSystemException;
 import org.springframework.validation.FieldError;
 import org.springframework.validation.ObjectError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -40,14 +42,25 @@ import static org.eco.vip.orm.exception.enums.GlobalErrorCode.NOT_FOUND;
 @Slf4j
 public class GlobalExceptionHandler {
 
+    /**
+     * 认证失败
+     */
+    @ExceptionHandler(NotLoginException.class)
+    public CommonResult<Void> handleNotLoginException(NotLoginException loginException, HttpServletRequest request) {
+        log.warn("[handleNotLoginException]", loginException);
+        String uri = request.getRequestURI();
+        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", uri, loginException.getMessage());
+        return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
+    }
+
     /**
      * 业务异常
      */
     @ExceptionHandler(BusinessException.class)
-    public CommonResult<Void> handleServiceException(BusinessException e) {
-        log.error(e.getMessage());
-        Integer code = e.getCode();
-        return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, e.getMessage()) : CommonResult.fail(e.getMessage());
+    public CommonResult<Void> handleBusinessException(BusinessException exception, HttpServletRequest request) {
+        log.warn("[handleBusinessException]", exception);
+        Integer code = exception.getCode();
+        return ObjectUtil.isNotNull(code) ? CommonResult.fail(code, exception.getMessage()) : CommonResult.fail(exception.getMessage());
     }
 
     /**
@@ -76,7 +89,7 @@ public class GlobalExceptionHandler {
      * 处理 Validator 校验不通过产生的异常
      */
     @ExceptionHandler(value = ConstraintViolationException.class)
-    public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception) {
+    public CommonResult<?> constraintViolationExceptionHandler(ConstraintViolationException exception, HttpServletRequest request) {
         log.warn("[constraintViolationExceptionHandler]", exception);
         ConstraintViolation<?> constraintViolation = exception.getConstraintViolations().iterator().next();
         return CommonResult.fail(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", constraintViolation.getMessage()));
@@ -86,7 +99,7 @@ public class GlobalExceptionHandler {
      * 处理 Dubbo Consumer 本地参数校验时,抛出的 ValidationException 异常
      */
     @ExceptionHandler(value = ValidationException.class)
-    public CommonResult<?> validationException(ValidationException exception) {
+    public CommonResult<?> validationException(ValidationException exception, HttpServletRequest request) {
         log.warn("[validationException]", exception);
         // 无法拼接明细的错误信息,因为 Dubbo Consumer 抛出 ValidationException 异常时,是直接的字符串信息,且人类不可读
         return CommonResult.fail(BAD_REQUEST);
@@ -96,22 +109,11 @@ public class GlobalExceptionHandler {
      * 处理 SpringMVC 请求地址不存在
      */
     @ExceptionHandler(NoResourceFoundException.class)
-    private CommonResult<?> noResourceFoundExceptionHandler(HttpServletRequest req, NoResourceFoundException throwable) {
+    private CommonResult<?> noResourceFoundExceptionHandler(NoResourceFoundException throwable, HttpServletRequest request) {
         log.warn("[noResourceFoundExceptionHandler]", throwable);
         return CommonResult.fail(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", throwable.getResourcePath()));
     }
 
-    /**
-     * 认证失败
-     */
-    @ExceptionHandler(NotLoginException.class)
-    public CommonResult<Void> handleNotLoginException(NotLoginException loginException, HttpServletRequest request) {
-        log.warn("[handleNotLoginException]", loginException);
-        String uri = request.getRequestURI();
-        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", uri, loginException.getMessage());
-        return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
-    }
-
     /**
      * 系统异常
      */
@@ -123,15 +125,19 @@ public class GlobalExceptionHandler {
         log.error("请求地址'{}',发生系统异常.{}", requestUri, message);
         return CommonResult.fail(message);
     }
+
     /**
-     * 系统异常
+     * Mybatis系统异常 通用处理
      */
-    @ExceptionHandler(Exception.class)
-    public CommonResult<Void> handleRuntimeException(RuntimeException runtimeException, HttpServletRequest request) {
-        log.warn("[handleRuntimeException]", runtimeException);
+    @ExceptionHandler(MyBatisSystemException.class)
+    public CommonResult<Void> handleCannotFindDataSourceException(MyBatisSystemException exception, HttpServletRequest request) {
         String requestUri = request.getRequestURI();
-        String message = ExceptionUtil.getRootCauseMessage(runtimeException);
-        log.error("请求地址'{}',发生未知异常.{}", requestUri, message);
+        String message = ExceptionUtil.getRootCauseMessage(exception);
+        if (StrUtils.contains("CannotFindDataSourceException", message)) {
+            log.error("请求地址'{}', 未找到数据源", requestUri);
+            return CommonResult.fail("未找到数据源,请联系管理员确认");
+        }
+        log.error("请求地址'{}', Mybatis系统异常", requestUri, exception);
         return CommonResult.fail(message);
     }
 }

+ 9 - 3
eco-common/com-security/src/main/java/org/eco/vip/security/utils/LoginHelper.java

@@ -95,9 +95,9 @@ public class LoginHelper {
      *
      * @return 对应的扩展数据
      */
-    private static Object getExtra() {
+    private static Object getExtra(String key) {
         try {
-            return StpUtil.getExtra(LoginHelper.USER_KEY);
+            return StpUtil.getExtra(key);
         } catch (Exception e) {
             return null;
         }
@@ -107,6 +107,12 @@ public class LoginHelper {
      * 获取用户id
      */
     public static String getUserId() {
-        return Convert.toStr(getExtra());
+        return Convert.toStr(getExtra(LoginHelper.USER_KEY));
+    }
+    /**
+     * 获取租户id
+     */
+    public static String getTenantId() {
+        return Convert.toStr(getExtra(LoginHelper.TENANT_KEY));
     }
 }

+ 1 - 1
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/org/OrgService.java

@@ -80,7 +80,7 @@ public class OrgService extends BaseService<OrgMapper, Org> implements IOrgServi
     @Override
     public boolean insert(OrgBO orgBO) {
         OrgVO orgVO = selectById(orgBO.getParentId());
-        if (ObjUtils.isNotNull(orgVO) && StrUtils.equals(Constants.NORMAL, orgVO.getStatus())) {
+        if (ObjUtils.isNotNull(orgVO) && !StrUtils.equals(Constants.NORMAL, orgVO.getStatus())) {
             throw new BusinessException("父组织停用,不允许新增");
         }
         Org org = MapstructUtils.convert(orgBO, Org.class);