Procházet zdrojové kódy

任务提交 基础异常类,和异常处理类

张均强 před 4 roky
rodič
revize
abebed4c28

+ 8 - 0
common/fire-common/pom.xml

@@ -30,6 +30,14 @@
             <version>1.0</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+        </dependency>
     </dependencies>
 
     <artifactId>fire-common</artifactId>

+ 55 - 0
common/fire-common/src/main/java/com/fire/common/exception/BaseException.java

@@ -0,0 +1,55 @@
+package com.fire.common.exception;
+
+
+import com.fire.dto.enums.Status;
+
+import static com.fire.dto.enums.Status.FAILURE;
+
+/**
+ * 自定义异常类(继承RuntimeException)
+ *
+ * @author ZJQ 2021年5月14日14:30:26
+ */
+public class BaseException extends RuntimeException {
+
+    private String status;
+
+    public BaseException(String message) {
+        super(message);
+        this.status = FAILURE.status();
+    }
+
+    public BaseException(Status status, Throwable cause) {
+        super(status.message(), cause);
+        this.status = status.status();
+    }
+
+    public BaseException(Status status) {
+        super(status.message());
+        this.status = status.status();
+    }
+
+    public BaseException(String status, String message) {
+        super(message);
+        this.status = status;
+    }
+
+    public BaseException(String status, String message, Throwable cause) {
+        super(message, cause);
+        this.status = status;
+    }
+
+    public BaseException(String message, Throwable cause) {
+        super(message, cause);
+        this.status = FAILURE.status();
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+}

+ 44 - 0
common/fire-common/src/main/java/com/fire/common/handler/GlobalExceptionHandler.java

@@ -0,0 +1,44 @@
+package com.fire.common.handler;
+
+
+import com.fire.common.exception.BaseException;
+import com.fire.dto.response.BaseResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletResponse;
+
+import static com.fire.dto.enums.Status.FAILURE;
+
+/**
+ * 异常处理类
+ *
+ * @author ZJQ 2021年5月14日14:32:00
+ */
+@ControllerAdvice("com.fire")
+@ResponseBody
+@Slf4j
+public class GlobalExceptionHandler {
+
+    /**
+     * 处理自定义基础异常
+     */
+    @ExceptionHandler(BaseException.class)
+    public BaseResponse resExceptionHandler(HttpServletResponse response, BaseException ex) {
+        response.setStatus(501);
+        return new BaseResponse(ex.getStatus(), ex.getMessage());
+    }
+
+    /**
+     * 处理未知异常
+     */
+    @ExceptionHandler(Exception.class)
+    public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
+        response.setStatus(500);
+        log.error(FAILURE.message(), ex);
+        return new BaseResponse(FAILURE.status(), ex.getMessage());
+    }
+
+}

+ 19 - 1
common/fire-dto/src/main/java/com/fire/dto/enums/Status.java

@@ -4,7 +4,25 @@ public enum Status {
 
     //系统
     SUCCESS("00", "成功"),
-    FAILURE("99", "失败");
+    FAILURE("99", "失败"),
+    AUTH_LOSS("01", "鉴权参数缺失"),
+    PACKAGE_LOSS("02", "话费包ID为空"),
+    USER_LOSS("03", "分发用户为空"),
+    PACKAGE_ERROR("04", "话费包ID错误"),
+    PHONE_NUM_ERROR("05", "手机号码格式错误"),
+    ORDER_ID_ERROR("06", "订单号为空或CP订单号重复"),
+    AUTH_ERROR("07", "鉴权信息错误"),
+    SIGN_FAIL("08", "签名验证失败"),
+    APP_ID_LOSE("09", "APPID已经失效"),
+    ORDER_NOT_EXISTS("10", "订单号不存在"),
+    DATE_ERROR("11", "日期参数错误"),
+    RISK_MANAGE("12", "超出运营商风险控制要求"),
+    OPERATOR_ERROR("13", "运营商类型错误"),
+    OVER_TIME("14", "TIMESTAMP超过时限"),
+    PROVINCE_MAINTAIN("30", "省份维护"),
+    PRODUCT_MAINTAIN("205", "产品维护"),
+    BALANCE_NOT_ENOUGH("301", "可用余额不足"),
+    PARAM_ERROR("306", "参数不合法");
 
     private String status;
 

+ 41 - 0
common/fire-dto/src/main/java/com/fire/dto/response/BaseResponse.java

@@ -0,0 +1,41 @@
+package com.fire.dto.response;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Getter;
+import lombok.Setter;
+
+import static com.fire.dto.enums.Status.SUCCESS;
+
+/**
+ * 基础无数据返回的响应
+ *
+ * @author ZJQ 2021年5月14日14:50:33
+ */
+@Getter
+@Setter
+public class BaseResponse {
+
+    @ApiModelProperty(value = "接口状态", example = "200")
+    private String status = SUCCESS.status();
+    @ApiModelProperty(value = "接口状态信息", example = "成功")
+    private String message = SUCCESS.message();
+
+    /**
+     * 空构造方法,适用场景,无数据返回成功
+     */
+    public BaseResponse() {
+    }
+
+    /**
+     * 带状态构造方法,适用场景,返回错误信息
+     *
+     * @param status  状态
+     * @param message 错误信息
+     */
+    public BaseResponse(String status, String message) {
+        this.status = status;
+        this.message = message;
+    }
+
+
+}

+ 4 - 11
common/fire-dto/src/main/java/com/fire/dto/response/BaseRestResponse.java

@@ -4,8 +4,6 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Getter;
 import lombok.Setter;
 
-import static com.fire.dto.enums.Status.SUCCESS;
-
 
 /**
  * rest接口标准返回类型
@@ -14,11 +12,8 @@ import static com.fire.dto.enums.Status.SUCCESS;
  */
 @Getter
 @Setter
-public class BaseRestResponse<T> {
-    @ApiModelProperty(value = "接口状态", example = "00")
-    private String status = SUCCESS.status();
-    @ApiModelProperty(value = "接口状态信息", example = "成功")
-    private String message = SUCCESS.message();
+public class BaseRestResponse<T> extends BaseResponse {
+
     @ApiModelProperty(value = "接口返回数据", example = "成功")
     private T data;
 
@@ -44,8 +39,7 @@ public class BaseRestResponse<T> {
      * @param message 错误信息
      */
     public BaseRestResponse(String status, String message) {
-        this.status = status;
-        this.message = message;
+        super(status, message);
     }
 
     /**
@@ -56,8 +50,7 @@ public class BaseRestResponse<T> {
      * @param data    泛型的数据
      */
     public BaseRestResponse(String status, String message, T data) {
-        this.status = status;
-        this.message = message;
+        super(status, message);
         this.data = data;
     }