浏览代码

扩展starter-api增加通用的ParameterException异常类与框架处理逻辑

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 3 年之前
父节点
当前提交
b1b4c9c3d7

+ 10 - 0
core-models/src/main/java/com/qmth/boot/core/exception/CodeNameException.java

@@ -0,0 +1,10 @@
+package com.qmth.boot.core.exception;
+
+public interface CodeNameException {
+
+    Integer getCode();
+
+    String getName();
+
+    String getMessage();
+}

+ 59 - 0
core-models/src/main/java/com/qmth/boot/core/exception/ParameterException.java

@@ -0,0 +1,59 @@
+package com.qmth.boot.core.exception;
+
+/**
+ * 通用请求参数异常,在API层按401状态码处理
+ */
+public class ParameterException extends RuntimeException implements CodeNameException {
+
+    private static final long serialVersionUID = -1045285828688368482L;
+
+    private Integer code;
+
+    private String name;
+
+    /**
+     * 有明确的文字提示
+     *
+     * @param message
+     */
+    public ParameterException(String message) {
+        super(message);
+    }
+
+    /**
+     * 有明确的文字提示与触发来源
+     *
+     * @param message
+     * @param cause
+     */
+    public ParameterException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    /**
+     * 带所有参数的构造方法
+     *
+     * @param code    - 数字标识,范围在100~999
+     * @param name    - 异常详细类型
+     * @param message - 文字提示
+     * @param cause   - 触发来源
+     */
+    public ParameterException(Integer code, String name, String message, Throwable cause) {
+        super(message, cause);
+        if (code != null) {
+            if (code < 100 || code > 999) {
+                throw new IllegalArgumentException("ParameterException.code should between 100~999");
+            }
+        }
+        this.code = code;
+        this.name = name;
+    }
+
+    public Integer getCode() {
+        return code;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

+ 1 - 1
core-models/src/main/java/com/qmth/boot/core/exception/StatusException.java

@@ -3,7 +3,7 @@ package com.qmth.boot.core.exception;
 /**
  * 有状态标识的运行时异常,在API层按500状态码处理
  */
-public class StatusException extends RuntimeException {
+public class StatusException extends RuntimeException implements CodeNameException {
 
     private static final long serialVersionUID = -2411329525159341065L;
 

+ 2 - 2
starter-api/src/main/java/com/qmth/boot/api/exception/DefaultExceptionEnum.java

@@ -1,6 +1,6 @@
 package com.qmth.boot.api.exception;
 
-import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.core.exception.CodeNameException;
 import org.springframework.http.HttpStatus;
 
 /**
@@ -34,7 +34,7 @@ public enum DefaultExceptionEnum {
         return new ApiException(status, code, name, null);
     }
 
-    public ApiException exception(StatusException ex) {
+    public ApiException exception(CodeNameException ex) {
         return new ApiException(status, ex.getCode() != null ? status.value() * 1000 + ex.getCode() : code,
                 ex.getName() != null ? ex.getName() : name, ex.getMessage());
     }

+ 7 - 0
starter-api/src/main/java/com/qmth/boot/api/exception/GlobalExceptionHandler.java

@@ -1,5 +1,6 @@
 package com.qmth.boot.api.exception;
 
+import com.qmth.boot.core.exception.ParameterException;
 import com.qmth.boot.core.exception.ReentrantException;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.UnauthorizedException;
@@ -27,6 +28,12 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
         return new ResponseEntity<>(e.responseEntity(), new HttpHeaders(), e.getStatus());
     }
 
+    @ExceptionHandler(ParameterException.class)
+    public ResponseEntity<Object> handleParameterException(ParameterException e) {
+        //log.error(e.getMessage(), e);
+        return handleApiException(DefaultExceptionEnum.REQUEST_PARAMETER_INVALID.exception(e));
+    }
+
     @ExceptionHandler(UnauthorizedException.class)
     public ResponseEntity<Object> handleUnauthorizedException(UnauthorizedException e) {
         //log.error(e.getMessage(), e);