xujunwei 3 лет назад
Родитель
Сommit
4df10544c5

+ 15 - 29
framework-boot/src/main/java/com/mrxu/framework/boot/handle/FeignProviderExceptionHandler.java

@@ -8,7 +8,6 @@ import com.mrxu.framework.common.util.BusinessException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.validation.BindingResult;
-import org.springframework.validation.FieldError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.MissingServletRequestParameterException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -37,38 +36,25 @@ public class FeignProviderExceptionHandler {
     @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
     @ExceptionHandler(Exception.class)
     public Object handleException(Exception e) {
+        if(e instanceof BusinessException) {
+            BusinessException error = (BusinessException)e;
+            log.warn(e.getMessage());
+            return rendError(true,new BaseCode(error.getCode(),error.getMsg()));
+        }
+        else if(e instanceof MethodArgumentNotValidException) {
+            MethodArgumentNotValidException error = (MethodArgumentNotValidException)e;
+            BindingResult result = error.getBindingResult();
+            log.warn("请求参数错误:{}",result.getFieldError().getDefaultMessage());
+            return rendError(true,BaseCode.ERR_PARAMS_VALID);
+        }
+        else if(e instanceof MissingServletRequestParameterException) {
+            log.warn("请求缺少参数:{}",e.getMessage());
+            return rendError(true,BaseCode.ERR_PARAMS_MISS);
+        }
         log.error("系统错误:{}",e.getMessage(), e);
         return rendError(false,BaseCode.ERROR);
     }
 
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    @ExceptionHandler(BusinessException.class)
-    public Object handleLogicalException(BusinessException e) {
-        log.warn(e.getMessage());
-        return rendError(true,new BaseCode(e.getCode(),e.getMsg()));
-    }
-
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public Object handleValidException(MethodArgumentNotValidException e) {
-        BindingResult result = e.getBindingResult();
-        FieldError error = result.getFieldError();
-        log.warn("请求参数错误:{}",error.getDefaultMessage());
-        return rendError(true,BaseCode.ERR_PARAMS_VALID);
-    }
-
-    /**
-     * 缺少必要参数
-     * @param e
-     * @return
-     */
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    @ExceptionHandler(MissingServletRequestParameterException.class)
-    public Object handleValidException(MissingServletRequestParameterException e) {
-        log.warn("请求缺少参数:{}",e.getMessage());
-        return rendError(true,BaseCode.ERR_PARAMS_MISS);
-    }
-
     public Object rendError(boolean isBusinessException,BaseCode code) {
         HttpServletRequest request = ServletUtils.getRequest();
         FeignProviderException exception = new FeignProviderException(isBusinessException,code);

+ 15 - 28
framework-boot/src/main/java/com/mrxu/framework/boot/handle/WebExceptionHandler.java

@@ -8,14 +8,12 @@ import com.mrxu.framework.common.util.StrFunc;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.context.annotation.Import;
-import org.springframework.http.HttpStatus;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.FieldError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.MissingServletRequestParameterException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.servlet.ModelAndView;
 
@@ -42,36 +40,25 @@ public class WebExceptionHandler {
 
     @ExceptionHandler(Exception.class)
     public Object handleException(Exception e) {
+        if(e instanceof BusinessException) {
+            BusinessException error = (BusinessException)e;
+            logger.warn(e.getMessage());
+            return rendError(new BaseCode(error.getCode(),error.getMsg()));
+        }
+        else if(e instanceof MethodArgumentNotValidException) {
+            BindingResult result = ((MethodArgumentNotValidException)e).getBindingResult();
+            FieldError error = result.getFieldError();
+            logger.warn("请求参数错误:{}",error.getDefaultMessage());
+            return rendError(BaseCode.ERR_PARAMS_VALID);
+        }
+        else if(e instanceof MissingServletRequestParameterException) {
+            logger.warn("请求缺少参数:{}",e.getMessage());
+            return rendError(BaseCode.ERR_PARAMS_MISS);
+        }
         logger.error("系统错误:{}",e.getMessage(), e);
         return rendError(BaseCode.ERROR);
     }
 
-    @ExceptionHandler(BusinessException.class)
-    public Object handleLogicalException(BusinessException e) {
-        logger.warn(e.getMessage());
-        return rendError(new BaseCode(e.getCode(),e.getMsg()));
-    }
-
-    @ResponseStatus(HttpStatus.BAD_REQUEST)
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public Object handleValidException(MethodArgumentNotValidException e) {
-        BindingResult result = e.getBindingResult();
-        FieldError error = result.getFieldError();
-        logger.warn("请求参数错误:{}",error.getDefaultMessage());
-        return rendError(BaseCode.ERR_PARAMS_VALID);
-    }
-
-    /**
-     * 缺少必要参数
-     * @param e
-     * @return
-     */
-    @ExceptionHandler(MissingServletRequestParameterException.class)
-    public Object handleValidException(MissingServletRequestParameterException e) {
-        logger.warn("请求缺少参数:{}",e.getMessage());
-        return rendError(BaseCode.ERR_PARAMS_MISS);
-    }
-
     public Object rendError(BaseCode code) {
         HttpServletRequest request = ServletUtils.getRequest();
         if(ServletUtils.isAjaxRequest(request)) {

+ 6 - 0
framework-common/src/main/java/com/mrxu/framework/common/util/MrxuAssert.java

@@ -18,6 +18,12 @@ public class MrxuAssert {
         }
     }
 
+    public static void isTrue(boolean expression, BaseCode baseCode) {
+        if (!expression) {
+            throw new BusinessException(baseCode);
+        }
+    }
+
     public static void isNotEmpty(Object str, String msg) {
         isFalse((str == null) || ("".equals(str.toString())), msg);
     }