refactor(exception)!: 重构多类型异常接口
- 在 IMultiTypesException 接口中添加泛型参数 TCode,用于指定异常类型代码的类型 - 在 IExceptionType 接口中添加 getDescription 方法,用于获取异常类型的描述信息
This commit is contained in:
parent
ce56f297f3
commit
f4e3684a3f
@ -77,7 +77,7 @@ System.out.println(result); // Output: Return string
|
|||||||
```java
|
```java
|
||||||
public final class LoginException
|
public final class LoginException
|
||||||
extends RuntimeException
|
extends RuntimeException
|
||||||
implements IMultiTypesException<LoginException, LoginException.Type> {
|
implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||||
private final Type type;
|
private final Type type;
|
||||||
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||||
super(message);
|
super(message);
|
||||||
@ -103,7 +103,7 @@ public final class LoginException
|
|||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
public enum Type implements IExceptionType<LoginException> {
|
public enum Type implements IExceptionType<LoginException, String> {
|
||||||
DEFAULT("00", "当前会话未登录"),
|
DEFAULT("00", "当前会话未登录"),
|
||||||
NOT_TOKEN("10", "未提供token"),
|
NOT_TOKEN("10", "未提供token"),
|
||||||
INVALID_TOKEN("20", "token无效"),
|
INVALID_TOKEN("20", "token无效"),
|
||||||
|
@ -15,8 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package xyz.zhouxy.plusone.commons.exception;
|
package xyz.zhouxy.plusone.commons.exception;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,7 +38,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
|||||||
* <pre>
|
* <pre>
|
||||||
* public final class LoginException
|
* public final class LoginException
|
||||||
* extends RuntimeException
|
* extends RuntimeException
|
||||||
* implements IMultiTypesException<LoginException, LoginException.Type> {
|
* implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||||
* private final Type type;
|
* private final Type type;
|
||||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||||
* super(message);
|
* super(message);
|
||||||
@ -61,7 +64,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
|||||||
*
|
*
|
||||||
* // ...
|
* // ...
|
||||||
*
|
*
|
||||||
* public enum Type implements IExceptionType<LoginException> {
|
* public enum Type implements IExceptionType<LoginException, String> {
|
||||||
* DEFAULT("00", "当前会话未登录"),
|
* DEFAULT("00", "当前会话未登录"),
|
||||||
* NOT_TOKEN("10", "未提供token"),
|
* NOT_TOKEN("10", "未提供token"),
|
||||||
* INVALID_TOKEN("20", "token无效"),
|
* INVALID_TOKEN("20", "token无效"),
|
||||||
@ -125,7 +128,8 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
|||||||
*/
|
*/
|
||||||
public interface IMultiTypesException<
|
public interface IMultiTypesException<
|
||||||
X extends Exception,
|
X extends Exception,
|
||||||
T extends IMultiTypesException.IExceptionType<X>> {
|
TCode extends Serializable,
|
||||||
|
T extends IMultiTypesException.IExceptionType<X, TCode>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常类型
|
* 异常类型
|
||||||
@ -140,20 +144,25 @@ public interface IMultiTypesException<
|
|||||||
*
|
*
|
||||||
* @return 异常类型编码
|
* @return 异常类型编码
|
||||||
*/
|
*/
|
||||||
default @Nonnull String getTypeCode() {
|
default @Nonnull TCode getTypeCode() {
|
||||||
return getType().getCode();
|
return getType().getCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常类型
|
* 异常类型
|
||||||
*/
|
*/
|
||||||
public static interface IExceptionType<X extends Exception>
|
public static interface IExceptionType<X extends Exception, TCode extends Serializable>
|
||||||
extends IWithCode<String>, IExceptionFactory<X> {
|
extends IWithCode<TCode>, IExceptionFactory<X> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认异常信息
|
* 默认异常信息
|
||||||
*/
|
*/
|
||||||
String getDefaultMessage();
|
String getDefaultMessage();
|
||||||
|
|
||||||
|
@Virtual
|
||||||
|
default String getDescription() {
|
||||||
|
return getDefaultMessage();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
|
|||||||
*/
|
*/
|
||||||
public final class ParsingFailureException
|
public final class ParsingFailureException
|
||||||
extends RuntimeException
|
extends RuntimeException
|
||||||
implements IMultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
|
implements IMultiTypesException<ParsingFailureException, String, ParsingFailureException.Type> {
|
||||||
|
|
||||||
private final Type type;
|
private final Type type;
|
||||||
|
|
||||||
@ -170,7 +170,7 @@ public final class ParsingFailureException
|
|||||||
/** XML 解析失败 */
|
/** XML 解析失败 */
|
||||||
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
|
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
|
||||||
|
|
||||||
public enum Type implements IExceptionType<ParsingFailureException> {
|
public enum Type implements IExceptionType<ParsingFailureException, String> {
|
||||||
DEFAULT("00", "解析失败"),
|
DEFAULT("00", "解析失败"),
|
||||||
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||||
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||||
|
@ -35,7 +35,7 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
|
|||||||
*/
|
*/
|
||||||
public final class InvalidInputException
|
public final class InvalidInputException
|
||||||
extends RequestParamsException
|
extends RequestParamsException
|
||||||
implements IMultiTypesException<InvalidInputException, InvalidInputException.Type> {
|
implements IMultiTypesException<InvalidInputException, String, InvalidInputException.Type> {
|
||||||
|
|
||||||
private final Type type;
|
private final Type type;
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ public final class InvalidInputException
|
|||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type implements IExceptionType<InvalidInputException> {
|
public enum Type implements IExceptionType<InvalidInputException, String> {
|
||||||
DEFAULT("00", "用户输入内容非法"),
|
DEFAULT("00", "用户输入内容非法"),
|
||||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||||
|
@ -31,7 +31,7 @@
|
|||||||
* <pre>
|
* <pre>
|
||||||
* public final class LoginException
|
* public final class LoginException
|
||||||
* extends RuntimeException
|
* extends RuntimeException
|
||||||
* implements IMultiTypesException<LoginException, LoginException.Type> {
|
* implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||||
* private final Type type;
|
* private final Type type;
|
||||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||||
* super(message);
|
* super(message);
|
||||||
@ -57,7 +57,7 @@
|
|||||||
*
|
*
|
||||||
* // ...
|
* // ...
|
||||||
*
|
*
|
||||||
* public enum Type implements IExceptionType<LoginException> {
|
* public enum Type implements IExceptionType<LoginException, String> {
|
||||||
* DEFAULT("00", "当前会话未登录"),
|
* DEFAULT("00", "当前会话未登录"),
|
||||||
* NOT_TOKEN("10", "未提供token"),
|
* NOT_TOKEN("10", "未提供token"),
|
||||||
* INVALID_TOKEN("20", "token无效"),
|
* INVALID_TOKEN("20", "token无效"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user