Gaokun Wang 3 долоо хоног өмнө
parent
commit
b38716026d

+ 21 - 15
eco-common/com-orm/src/main/java/org/eco/vip/orm/listener/EntityInsertListener.java

@@ -7,8 +7,10 @@ package org.eco.vip.orm.listener;
 
 
 import cn.hutool.core.util.ObjectUtil;
+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;
@@ -25,22 +27,26 @@ public class EntityInsertListener implements InsertListener {
 
     @Override
     public void onInsert(Object entity) {
-        if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
-            LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
-            String loginUserId = null;
-            if (ObjUtils.isNotNull(loginUserStorage)) {
-                loginUserId = loginUserStorage.getUserId();
+        try {
+            if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
+                LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
+                String loginUserId = null;
+                if (ObjUtils.isNotNull(loginUserStorage)) {
+                    loginUserId = loginUserStorage.getUserId();
+                }
+                Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
+                        ? baseEntity.getCreateTime() : new Date();
+                if (ObjectUtil.isNull(baseEntity.getCreateBy())) {
+                    baseEntity.setCreateBy(loginUserId);
+                }
+                baseEntity.setCreateTime(createTime);
+                if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
+                    baseEntity.setUpdateBy(loginUserId);
+                }
+                baseEntity.setUpdateTime(createTime);
             }
-            Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
-                    ? baseEntity.getCreateTime() : new Date();
-            if (ObjectUtil.isNull(baseEntity.getCreateBy())) {
-                baseEntity.setCreateBy(loginUserId);
-            }
-            baseEntity.setCreateTime(createTime);
-            if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
-                baseEntity.setUpdateBy(loginUserId);
-            }
-            baseEntity.setUpdateTime(createTime);
+        } catch (Exception e) {
+            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局插入数据监听器注入异常 => {}", e.getMessage());
         }
     }
 }

+ 15 - 9
eco-common/com-orm/src/main/java/org/eco/vip/orm/listener/EntityUpdateListener.java

@@ -7,8 +7,10 @@ package org.eco.vip.orm.listener;
 
 
 import cn.hutool.core.util.ObjectUtil;
+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;
@@ -24,16 +26,20 @@ import java.util.Date;
 public class EntityUpdateListener implements UpdateListener {
     @Override
     public void onUpdate(Object entity) {
-        if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
-            LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
-            String loginUserId = null;
-            if (ObjUtils.isNotNull(loginUserStorage)) {
-                loginUserId = loginUserStorage.getUserId();
+        try {
+            if (ObjectUtil.isNotNull(entity) && (entity instanceof BaseEntity baseEntity)) {
+                LoginUserStorage loginUserStorage = LoginHelper.getLoginUser();
+                String loginUserId = null;
+                if (ObjUtils.isNotNull(loginUserStorage)) {
+                    loginUserId = loginUserStorage.getUserId();
+                }
+                if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
+                    baseEntity.setUpdateBy(loginUserId);
+                }
+                baseEntity.setUpdateTime(new Date());
             }
-            if (ObjectUtil.isNull(baseEntity.getUpdateBy())) {
-                baseEntity.setUpdateBy(loginUserId);
-            }
-            baseEntity.setUpdateTime(new Date());
+        } catch (BusinessException e) {
+            throw new BusinessException(HttpStatus.HTTP_UNAUTHORIZED, "全局更新数据监听器注入异常 => {} ", e.getMessage());
         }
     }
 }

+ 23 - 0
eco-common/com-security/src/main/java/org/eco/vip/security/handler/GlobalExceptionHandler.java

@@ -111,4 +111,27 @@ public class GlobalExceptionHandler {
         log.error("请求地址'{}',认证失败'{}',无法访问系统资源", uri, loginException.getMessage());
         return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
     }
+
+    /**
+     * 系统异常
+     */
+    @ExceptionHandler(Exception.class)
+    public CommonResult<Void> handleExceptionHandler(HttpServletRequest request, Exception exception) {
+        log.warn("[handleExceptionHandler]", exception);
+        String requestUri = request.getRequestURI();
+        String message = ExceptionUtil.getRootCauseMessage(exception);
+        log.error("请求地址'{}',发生系统异常.{}", requestUri, message);
+        return CommonResult.fail(message);
+    }
+    /**
+     * 系统异常
+     */
+    @ExceptionHandler(Exception.class)
+    public CommonResult<Void> handleRuntimeException(RuntimeException runtimeException, HttpServletRequest request) {
+        log.warn("[handleRuntimeException]", runtimeException);
+        String requestUri = request.getRequestURI();
+        String message = ExceptionUtil.getRootCauseMessage(runtimeException);
+        log.error("请求地址'{}',发生未知异常.{}", requestUri, message);
+        return CommonResult.fail(message);
+    }
 }