Procházet zdrojové kódy

异常拦截 - 抛出具体信息,便于定位原因。
生产环境需要调回来,后续再优化

yucheng před 3 roky
rodič
revize
c0f4c98218

+ 16 - 2
dmp-common/src/main/java/com/persagy/dmp/common/handler/CommonExceptionHandler.java

@@ -1,5 +1,6 @@
 package com.persagy.dmp.common.handler;
 
+import cn.hutool.core.exceptions.ExceptionUtil;
 import com.persagy.dmp.common.constant.ResponseCode;
 import com.persagy.dmp.common.exception.BusinessException;
 import com.persagy.dmp.common.model.response.CommonResult;
@@ -15,6 +16,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.servlet.NoHandlerFoundException;
 
 import javax.validation.ValidationException;
+import java.sql.SQLException;
 
 /**
  * @description: 通用异常处理类
@@ -31,7 +33,7 @@ public class CommonExceptionHandler {
     @ExceptionHandler(NullPointerException.class)
     public CommonResult handleBusinessRuntimeException(NullPointerException e) {
         log.error(e.getMessage(), e);
-        return ResultHelper.failure(ResponseCode.B0001.getCode(), e.getMessage());
+        return ResultHelper.failure(ResponseCode.B0001.getCode(), ExceptionUtil.getRootCauseMessage(e));
     }
 
     /**
@@ -44,6 +46,17 @@ public class CommonExceptionHandler {
     }
 
     /**
+     * SQL异常
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(SQLException.class)
+    public CommonResult handleSQLException(SQLException e) {
+        log.error(e.getMessage(), e);
+        return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getMessage());
+    }
+
+    /**
      * 入参校验异常
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)
@@ -106,6 +119,7 @@ public class CommonExceptionHandler {
     @ExceptionHandler(Exception.class)
     public CommonResult handleException(Exception e) {
         log.error(e.getMessage(), e);
-        return ResultHelper.failure(ResponseCode.Z9999.getCode(), ResponseCode.Z9999.getDesc());
+        // 20210819 返回实际的异常信息
+        return ResultHelper.failure(ResponseCode.Z9999.getCode(), ExceptionUtil.getRootCauseMessage(e));
     }
 }