瀏覽代碼

增加通用的找不到资源异常,并在api层按照404状态码进行特殊处理

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 3 年之前
父節點
當前提交
305567a6ca

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

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

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

@@ -16,7 +16,8 @@ public enum DefaultExceptionEnum {
     AUTHORIZATION_FAILE(HttpStatus.UNAUTHORIZED, 401002, "authorization faile"),
     COMMON_EXCEPTION(HttpStatus.INTERNAL_SERVER_ERROR, 500000, "internal server error"),
     REENTRANT_EXCEPTION(HttpStatus.SERVICE_UNAVAILABLE, 503001, "service unavailable"),
-    RATE_LIMITED(HttpStatus.SERVICE_UNAVAILABLE, 503002, "request rate limited");
+    RATE_LIMITED(HttpStatus.SERVICE_UNAVAILABLE, 503002, "request rate limited"),
+    RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, 404001, "resource not found");
 
     private HttpStatus status;
 

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

@@ -1,9 +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;
+import com.qmth.boot.core.exception.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpHeaders;
@@ -52,6 +49,12 @@ public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
         return handleApiException(DefaultExceptionEnum.COMMON_EXCEPTION.exception(e));
     }
 
+    @ExceptionHandler(NotFoundException.class)
+    public ResponseEntity<Object> handleNotFoundException(NotFoundException e) {
+        //log.error(e.getMessage(), e);
+        return handleApiException(DefaultExceptionEnum.RESOURCE_NOT_FOUND.exception(e));
+    }
+
     @ExceptionHandler(Exception.class)
     public ResponseEntity<Object> handleOtherException(Exception e) {
         log.error(e.getMessage(), e);