Quellcode durchsuchen

添加异常和全局返回结果

Gaokun Wang vor 1 Monat
Ursprung
Commit
6f9f440d57

+ 89 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/BusinessException.java

@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.exception;
+
+
+import cn.hutool.core.util.StrUtil;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import org.eco.vip.orm.exception.enums.BusinessErrorCode;
+
+import java.text.MessageFormat;
+
+/**
+ * @description 业务逻辑异常
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 13:55
+ */
+@Data
+@EqualsAndHashCode(callSuper = true)
+public final class BusinessException extends RuntimeException {
+    /**
+     * 业务错误码
+     *
+     * @see BusinessErrorCode
+     */
+    private Integer code;
+
+    /**
+     * 错误参数
+     */
+    private Object[] msgParams;
+
+    /**
+     * 错误提示
+     */
+    private String message;
+
+    /**
+     * 空构造方法,避免反序列化问题
+     */
+    public BusinessException() {
+    }
+
+    public BusinessException(String message) {
+        this.message = message;
+    }
+
+
+    public BusinessException(ErrorCode errorCode) {
+        this.code = errorCode.getCode();
+        this.message = errorCode.getMsg();
+    }
+
+    public BusinessException(Integer code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public BusinessException(Integer code, String msg, Object... msgParams) {
+        this.message = msg;
+        this.code = code;
+        this.msgParams = msgParams;
+    }
+
+    public BusinessException(String msg, Object... msgParams) {
+        this.message = msg;
+        this.msgParams = msgParams;
+    }
+
+    @Override
+    public String getMessage() {
+        if (StrUtil.isNotBlank(message)) {
+            if (msgParams != null && msgParams.length > 0) {
+                return MessageFormat.format(message, msgParams);
+            }
+        }
+        // 如果不传参数,直接返回
+        return message;
+    }
+
+    public BusinessException setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+}

+ 32 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/ErrorCode.java

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.exception;
+
+
+import lombok.Data;
+
+/**
+ * @description ErrorCode
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 13:48
+ */
+@Data
+public class ErrorCode {
+    /**
+     * 错误码
+     */
+    private final Integer code;
+    /**
+     * 错误提示
+     */
+    private final String msg;
+
+    public ErrorCode(Integer code, String message) {
+        this.code = code;
+        this.msg = message;
+    }
+}

+ 65 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/ServerException.java

@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.exception;
+
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import org.eco.vip.orm.exception.enums.GlobalErrorCode;
+
+/**
+ * @description ServerException 服务器异常
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 14:03
+ */
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class ServerException extends RuntimeException {
+
+    /**
+     * 全局错误码
+     *
+     * @see GlobalErrorCode
+     */
+    private Integer code;
+    /**
+     * 错误提示
+     */
+    private String message;
+
+    /**
+     * 空构造方法,避免反序列化问题
+     */
+    public ServerException() {
+    }
+
+    public ServerException(ErrorCode errorCode) {
+        this.code = errorCode.getCode();
+        this.message = errorCode.getMsg();
+    }
+
+    public ServerException(Integer code, String message) {
+        this.code = code;
+        this.message = message;
+    }
+
+    public ServerException setCode(Integer code) {
+        this.code = code;
+        return this;
+    }
+
+    @Override
+    public String getMessage() {
+        return message;
+    }
+
+    public ServerException setMessage(String message) {
+        this.message = message;
+        return this;
+    }
+}

+ 16 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/enums/BusinessErrorCode.java

@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.exception.enums;
+
+
+/**
+ * @description BusinessErrorCode
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 13:56
+ */
+public class BusinessErrorCode {
+}

+ 41 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/enums/GlobalErrorCode.java

@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.exception.enums;
+
+
+import org.eco.vip.orm.exception.ErrorCode;
+
+/**
+ * @description 全局错误码枚举
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 13:53
+ */
+public interface GlobalErrorCode {
+    ErrorCode SUCCESS = new ErrorCode(200, "成功");
+
+    // ========== 客户端错误段 ==========
+    ErrorCode BAD_REQUEST = new ErrorCode(400, "请求参数不正确");
+    ErrorCode UNAUTHORIZED = new ErrorCode(401, "账号未登录");
+    ErrorCode FORBIDDEN = new ErrorCode(403, "没有该操作权限");
+    ErrorCode NOT_FOUND = new ErrorCode(404, "请求未找到");
+    ErrorCode METHOD_NOT_ALLOWED = new ErrorCode(405, "请求方法不正确");
+    // 并发请求,不允许
+    ErrorCode LOCKED = new ErrorCode(423, "请求失败,请稍后重试");
+    ErrorCode TOO_MANY_REQUESTS = new ErrorCode(429, "请求过于频繁,请稍后重试");
+
+    // ========== 服务端错误段 ==========
+
+    ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统异常");
+    ErrorCode NOT_IMPLEMENTED = new ErrorCode(501, "功能未实现/未开启");
+
+    // ========== 自定义错误段 ==========
+    ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试");
+    // 重复请求
+    ErrorCode DEMO_DENY = new ErrorCode(901, "演示模式,禁止写操作");
+
+    ErrorCode UNKNOWN = new ErrorCode(999, "未知错误");
+}

+ 129 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/pojo/CommonResult.java

@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.orm.pojo;
+
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.eco.vip.orm.exception.BusinessException;
+import org.eco.vip.orm.exception.ErrorCode;
+import org.eco.vip.orm.exception.enums.GlobalErrorCode;
+import org.springframework.util.Assert;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * @description CommonResult
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 13:47
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Builder
+@Data
+public class CommonResult<T> implements Serializable {
+    /**
+     * 错误码
+     *
+     * @see ErrorCode#getCode()
+     */
+    private Integer code;
+    /**
+     * 返回数据
+     */
+    private T result;
+    /**
+     * 错误提示,用户可阅读
+     *
+     * @see ErrorCode#getMsg() ()
+     */
+    private String message;
+
+    /**
+     * 将传入的 result 对象,转换成另外一个泛型结果的对象
+     * 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
+     *
+     * @param result 传入的 result 对象
+     * @param <T>    返回的泛型
+     * @return 新的 CommonResult 对象
+     */
+    public static <T> CommonResult<T> error(CommonResult<?> result) {
+        return error(result.getCode(), result.getMessage());
+    }
+
+    public static <T> CommonResult<T> error(Integer code, String message) {
+        Assert.isTrue(!GlobalErrorCode.SUCCESS.getCode().equals(code), "code 必须是错误的!");
+        CommonResult<T> result = new CommonResult<>();
+        result.code = code;
+        result.message = message;
+        return result;
+    }
+
+    public static <T> CommonResult<T> error(ErrorCode errorCode) {
+        return error(errorCode.getCode(), errorCode.getMsg());
+    }
+
+    public static <T> CommonResult<T> success(T data) {
+        CommonResult<T> result = new CommonResult<>();
+        result.code = GlobalErrorCode.SUCCESS.getCode();
+        result.result = data;
+        result.message = "";
+        return result;
+    }
+
+    public static <T> CommonResult<T> success() {
+        CommonResult<T> result = new CommonResult<>();
+        result.code = GlobalErrorCode.SUCCESS.getCode();
+        result.message = "";
+        return result;
+    }
+
+    public static boolean isSuccess(Integer code) {
+        return Objects.equals(code, GlobalErrorCode.SUCCESS.getCode());
+    }
+
+    @JsonIgnore
+    public boolean isSuccess() {
+        return isSuccess(code);
+    }
+
+    @JsonIgnore
+    public boolean isError() {
+        return !isSuccess();
+    }
+
+    // ========= 和 Exception 异常体系集成 =========
+
+    /**
+     * 判断是否有异常。如果有,则抛出 {@link BusinessException} 异常
+     */
+    public void checkError() throws BusinessException {
+        if (isSuccess()) {
+            return;
+        }
+        // 业务异常
+        throw new BusinessException(code, message);
+    }
+
+    /**
+     * 判断是否有异常。如果有,则抛出 {@link BusinessException} 异常
+     * 如果没有,则返回 {@link #result} 数据
+     */
+    @JsonIgnore
+    public T getCheckedData() {
+        checkError();
+        return result;
+    }
+
+    public static <T> CommonResult<T> error(BusinessException businessException) {
+        return error(businessException.getCode(), businessException.getMessage());
+    }
+}