forked from plusone/plusone-commons
refactor(exception)!: 修改 SysException 和 BizException
- 将 `SysException` 和 `BizException` 的构造方法设为 `protected`,供子类的构造方法调用 - 创建对应的工厂方法用于直接创建 `SysException` 和 `BizException` 实例
This commit is contained in:
@@ -33,21 +33,13 @@ public class BizException extends RuntimeException {
|
|||||||
|
|
||||||
private static final String DEFAULT_MSG = "业务异常";
|
private static final String DEFAULT_MSG = "业务异常";
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用默认 message 构造新的业务异常。
|
|
||||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
|
||||||
*/
|
|
||||||
public BizException() {
|
|
||||||
super(DEFAULT_MSG);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用指定的 {@code message} 构造新的业务异常。
|
* 使用指定的 {@code message} 构造新的业务异常。
|
||||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||||
*
|
*
|
||||||
* @param message 异常信息
|
* @param message 异常信息
|
||||||
*/
|
*/
|
||||||
public BizException(String message) {
|
protected BizException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +49,7 @@ public class BizException extends RuntimeException {
|
|||||||
*
|
*
|
||||||
* @param cause 包装的异常
|
* @param cause 包装的异常
|
||||||
*/
|
*/
|
||||||
public BizException(Throwable cause) {
|
protected BizException(Throwable cause) {
|
||||||
super(cause);
|
super(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,8 +59,27 @@ public class BizException extends RuntimeException {
|
|||||||
* @param message 异常信息
|
* @param message 异常信息
|
||||||
* @param cause 包装的异常
|
* @param cause 包装的异常
|
||||||
*/
|
*/
|
||||||
public BizException(String message, Throwable cause) {
|
protected BizException(String message, Throwable cause) {
|
||||||
super(message, cause);
|
super(message, cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BizException of() {
|
||||||
|
return new BizException(DEFAULT_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BizException of(String message) {
|
||||||
|
return new BizException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BizException of(String errorMessageFormat, Object... errorMessageArgs) {
|
||||||
|
return new BizException(String.format(errorMessageFormat, errorMessageArgs));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BizException of(Throwable cause) {
|
||||||
|
return new BizException(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BizException of(String message, Throwable cause) {
|
||||||
|
return new BizException(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class NoAvailableMacFoundException extends SysException {
|
|||||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||||
*/
|
*/
|
||||||
public NoAvailableMacFoundException() {
|
public NoAvailableMacFoundException() {
|
||||||
super();
|
super("无法找到可访问的 Mac 地址");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -30,21 +30,13 @@ public class SysException extends RuntimeException {
|
|||||||
|
|
||||||
private static final String DEFAULT_MSG = "系统异常";
|
private static final String DEFAULT_MSG = "系统异常";
|
||||||
|
|
||||||
/**
|
|
||||||
* 使用默认 message 构造新的系统异常。
|
|
||||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
|
||||||
*/
|
|
||||||
public SysException() {
|
|
||||||
super(DEFAULT_MSG);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用指定的 {@code message} 构造新的系统异常。
|
* 使用指定的 {@code message} 构造新的系统异常。
|
||||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||||
*
|
*
|
||||||
* @param message 异常信息
|
* @param message 异常信息
|
||||||
*/
|
*/
|
||||||
public SysException(String message) {
|
protected SysException(String message) {
|
||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +46,7 @@ public class SysException extends RuntimeException {
|
|||||||
*
|
*
|
||||||
* @param cause 包装的异常
|
* @param cause 包装的异常
|
||||||
*/
|
*/
|
||||||
public SysException(Throwable cause) {
|
protected SysException(Throwable cause) {
|
||||||
super(cause);
|
super(cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +56,27 @@ public class SysException extends RuntimeException {
|
|||||||
* @param message 异常信息
|
* @param message 异常信息
|
||||||
* @param cause 包装的异常
|
* @param cause 包装的异常
|
||||||
*/
|
*/
|
||||||
public SysException(String message, Throwable cause) {
|
protected SysException(String message, Throwable cause) {
|
||||||
super(message, cause);
|
super(message, cause);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static SysException of() {
|
||||||
|
return new SysException(DEFAULT_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SysException of(String message) {
|
||||||
|
return new SysException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SysException of(String errorMessageFormat, Object... errorMessageArgs) {
|
||||||
|
return new SysException(String.format(errorMessageFormat, errorMessageArgs));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SysException of(Throwable cause) {
|
||||||
|
return new SysException(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SysException of(String message, Throwable cause) {
|
||||||
|
return new SysException(message, cause);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -381,7 +381,7 @@ class CustomUnifiedResponseFactoryTests {
|
|||||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", user));
|
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", user));
|
||||||
|
|
||||||
// Throwable
|
// Throwable
|
||||||
BizException bizException = new BizException("业务异常");
|
BizException bizException = BizException.of("业务异常");
|
||||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, bizException));
|
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, bizException));
|
||||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, (Throwable) null));
|
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, (Throwable) null));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ class UnifiedResponseTests {
|
|||||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", user));
|
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", user));
|
||||||
|
|
||||||
// Throwable
|
// Throwable
|
||||||
BizException bizException = new BizException("业务异常");
|
BizException bizException = BizException.of("业务异常");
|
||||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, bizException));
|
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, bizException));
|
||||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, (Throwable) null));
|
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, (Throwable) null));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user