From 6d76e9d5245de769e8741727f619f825d78fc736 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 27 Aug 2024 19:00:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=B8=8D=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E5=BC=82=E5=B8=B8=E7=9A=84=E5=AD=97=E6=AE=B5=E3=80=82?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=80=9A=E8=BF=87=20getType=20=E6=A0=87?= =?UTF-8?q?=E8=AF=86=E4=B8=8D=E5=90=8C=E9=94=99=E8=AF=AF=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/exception/BaseException.java | 24 ++++++------- .../exception/BaseRuntimeException.java | 25 ++++++------- .../commons/exception/BizException.java | 36 +++++++++++++++++++ .../commons/exception/SysException.java | 31 ++++++++++++++++ .../exception/ThirdPartySystemException.java | 31 ++++++++++++++++ .../commons/model/dto/UnifiedResponse.java | 10 ------ 6 files changed, 118 insertions(+), 39 deletions(-) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/exception/BizException.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/exception/SysException.java create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/exception/ThirdPartySystemException.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java index f09195f..a8b91ad 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java @@ -16,8 +16,6 @@ package xyz.zhouxy.plusone.commons.exception; -import xyz.zhouxy.plusone.commons.base.IWithCode; - import javax.annotation.Nonnull; import java.util.Objects; @@ -27,32 +25,30 @@ import java.util.Objects; * @author ZhouXY */ public abstract class BaseException - extends Exception - implements IWithCode { + extends Exception { private static final long serialVersionUID = -2546365325001947203L; @Nonnull - private final String code; + private final String type; - protected BaseException(String code, String msg) { + protected BaseException(String type, String msg) { super(msg); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } - protected BaseException(String code, Throwable cause) { + protected BaseException(String type, Throwable cause) { super(cause); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } - protected BaseException(String code, String msg, Throwable cause) { + protected BaseException(String type, String msg, Throwable cause) { super(msg, cause); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } @Nonnull - @Override - public final String getCode() { - return this.code; + public final String getType() { + return this.type; } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java index 61fae62..6dbdf68 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java @@ -16,8 +16,6 @@ package xyz.zhouxy.plusone.commons.exception; -import xyz.zhouxy.plusone.commons.base.IWithCode; - import javax.annotation.Nonnull; import java.util.Objects; @@ -26,33 +24,30 @@ import java.util.Objects; * * @author ZhouXY */ -public abstract class BaseRuntimeException - extends RuntimeException - implements IWithCode { +public abstract class BaseRuntimeException extends RuntimeException { private static final long serialVersionUID = -6345888403567792664L; @Nonnull - private final String code; + private final String type; - protected BaseRuntimeException(String code, String msg) { + protected BaseRuntimeException(String type, String msg) { super(msg); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } - protected BaseRuntimeException(String code, Throwable cause) { + protected BaseRuntimeException(String type, Throwable cause) { super(cause); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } - protected BaseRuntimeException(String code, String msg, Throwable cause) { + protected BaseRuntimeException(String type, String msg, Throwable cause) { super(msg, cause); - this.code = Objects.requireNonNull(code); + this.type = Objects.requireNonNull(type); } @Nonnull - @Override - public final String getCode() { - return this.code; + public final String getType() { + return this.type; } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BizException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BizException.java new file mode 100644 index 0000000..e21c3ce --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BizException.java @@ -0,0 +1,36 @@ +package xyz.zhouxy.plusone.commons.exception; + +/** + * 业务异常 + * + * @author ZhouXY + */ +public class BizException extends BaseRuntimeException { + private static final long serialVersionUID = -5524759033245815405L; + + protected BizException(String type, String msg) { + super(type, msg); + } + + protected BizException(String type, Throwable cause) { + super(type, cause); + } + + protected BizException(String type, String msg, Throwable cause) { + super(type, msg, cause); + } + + private static final String DEFAULT = "0"; + + public static BizException of(String msg) { + return new BizException(DEFAULT, msg); + } + + public static BizException of(Throwable cause) { + return new BizException(DEFAULT, cause); + } + + public static BizException of(String msg, Throwable cause) { + return new BizException(DEFAULT, msg, cause); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/SysException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/SysException.java new file mode 100644 index 0000000..b859696 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/SysException.java @@ -0,0 +1,31 @@ +package xyz.zhouxy.plusone.commons.exception; + +public class SysException extends BaseRuntimeException { + private static final long serialVersionUID = 8821240827443168118L; + + protected SysException(String type, String msg) { + super(type, msg); + } + + protected SysException(String type, Throwable cause) { + super(type, cause); + } + + protected SysException(String type, String msg, Throwable cause) { + super(type, msg, cause); + } + + private static final String DEFAULT = "0"; + + public static SysException of(String msg) { + return new SysException(DEFAULT, msg); + } + + public static SysException of(Throwable cause) { + return new SysException(DEFAULT, cause); + } + + public static SysException of(String msg, Throwable cause) { + return new SysException(DEFAULT, msg, cause); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/ThirdPartySystemException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/ThirdPartySystemException.java new file mode 100644 index 0000000..a88227a --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/ThirdPartySystemException.java @@ -0,0 +1,31 @@ +package xyz.zhouxy.plusone.commons.exception; + +public class ThirdPartySystemException extends BaseRuntimeException { + private static final long serialVersionUID = 20240827113826L; + + protected ThirdPartySystemException(String type, String msg) { + super(type, msg); + } + + protected ThirdPartySystemException(String type, Throwable cause) { + super(type, cause); + } + + protected ThirdPartySystemException(String type, String msg, Throwable cause) { + super(type, msg, cause); + } + + private static final String DEFAULT = "0"; + + public static ThirdPartySystemException of(String msg) { + return new ThirdPartySystemException(DEFAULT, msg); + } + + public static ThirdPartySystemException of(Throwable cause) { + return new ThirdPartySystemException(DEFAULT, cause); + } + + public static ThirdPartySystemException of(String msg, Throwable cause) { + return new ThirdPartySystemException(DEFAULT, msg, cause); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java index 01965e7..76395d4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java @@ -24,8 +24,6 @@ import javax.annotation.Nullable; import com.google.common.base.Preconditions; -import xyz.zhouxy.plusone.commons.base.IWithCode; - /** * 统一结果,对返回给前端的数据进行封装。 * @@ -70,10 +68,6 @@ public abstract class UnifiedResponse { return new ErrorResult(status, e); } - public static > UnifiedResponse error(E e) { - return new ErrorResult(e); - } - public static UnifiedResponse of(Object status, @Nullable String message) { return new CustomResult(status, message); } @@ -195,10 +189,6 @@ public abstract class UnifiedResponse { ErrorResult(Object status, Throwable e) { super(status, Objects.requireNonNull(e).getMessage()); } - - > ErrorResult(E e) { - super(e.getCode(), e.getMessage()); - } } /**