错误码统一进行配置,不作为异常的字段。异常通过 getType 标识不同错误类型。

dev
ZhouXY108 2024-08-27 19:00:38 +08:00
parent a59cadc537
commit 6d76e9d524
6 changed files with 118 additions and 39 deletions

View File

@ -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 <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public abstract class BaseException
extends Exception
implements IWithCode<String> {
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;
}
}

View File

@ -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 <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public abstract class BaseRuntimeException
extends RuntimeException
implements IWithCode<String> {
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;
}
}

View File

@ -0,0 +1,36 @@
package xyz.zhouxy.plusone.commons.exception;
/**
*
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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 <E extends Throwable & IWithCode<?>> 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());
}
<E extends Throwable & IWithCode<?>> ErrorResult(E e) {
super(e.getCode(), e.getMessage());
}
}
/**