소스 검색

代码优化

24282 1 년 전
부모
커밋
2598cc88f5
1개의 변경된 파일33개의 추가작업 그리고 3개의 파일을 삭제
  1. 33 3
      ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java

+ 33 - 3
ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java

@@ -1,7 +1,9 @@
 package com.ruoyi.framework.web.exception;
 
+import cn.hutool.core.util.StrUtil;
 import com.ruoyi.common.constant.HttpStatus;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.exception.DemoModeException;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.StringUtils;
@@ -9,14 +11,18 @@ import org.apache.catalina.connector.ClientAbortException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.dao.DuplicateKeyException;
+import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.security.access.AccessDeniedException;
 import org.springframework.validation.BindException;
 import org.springframework.web.HttpRequestMethodNotSupportedException;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Objects;
 
 /**
  * 全局异常处理器
@@ -91,7 +97,6 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(BindException.class)
     public AjaxResult handleBindException(BindException e) {
-        log.error(e.getMessage(), e);
         String message = e.getAllErrors().get(0).getDefaultMessage();
         return AjaxResult.error(message);
     }
@@ -101,11 +106,10 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
-        log.error(e.getMessage(), e);
         String message = e.getBindingResult().getFieldError().getDefaultMessage();
         return AjaxResult.error(message);
     }
-    
+
     /**
      * 管道破裂异常
      */
@@ -121,4 +125,30 @@ public class GlobalExceptionHandler {
     public AjaxResult handleDemoModeException(DemoModeException e) {
         return AjaxResult.error("演示模式,不允许操作");
     }
+
+    /**
+     * 请求参数类型不匹配
+     */
+    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
+    public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
+        return AjaxResult.error(String.format("请求参数类型不匹配,参数[%s]要求类型为:'%s',但输入值为:'%s'",
+                e.getName(), Objects.requireNonNull(e.getRequiredType()).getName(), e.getValue()));
+    }
+
+    /**
+     * 请求参数异常
+     */
+    @ExceptionHandler(HttpMessageNotReadableException.class)
+    public R handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
+        String message = e.getMessage();
+        String errorMsg = "未知JSON异常";
+        if (StrUtil.isNotBlank(message)) {
+            if (message.startsWith("Required request body is missing:")) {
+                errorMsg = "请求JSON为空";
+            } else if (message.startsWith("JSON parse error:")) {
+                errorMsg = "JSON格式错误:" + message.replace("JSON parse error:", "");
+            }
+        }
+        return R.fail(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, errorMsg);
+    }
 }