forked from plusone/plusone-commons
1. 优化多类型异常的创建方式;
2. 修改参数名,不使用缩写; 3. 完成异常的单元测试,
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*
|
||||
* <p>
|
||||
* 异常在不同场景下被抛出,可以用不同的枚举值,表示不同的异常类型。
|
||||
* 该枚举实现本接口,用于基于不同类型创建异常。
|
||||
*
|
||||
* <pre>
|
||||
* public final class LoginException extends RuntimeException {
|
||||
* private final Type type;
|
||||
* private LoginException(Type type, String message) {
|
||||
* super(message);
|
||||
* this.type = type;
|
||||
* }
|
||||
*
|
||||
* private LoginException(Type type, Throwable cause) {
|
||||
* super(cause);
|
||||
* this.type = type;
|
||||
* }
|
||||
*
|
||||
* private LoginException(Type type, String message, Throwable cause) {
|
||||
* super(message, cause);
|
||||
* this.type = type;
|
||||
* }
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements ExceptionType<LoginException> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
* TOKEN_TIMEOUT("30", "token已过期"),
|
||||
* BE_REPLACED("40", "token已被顶下线"),
|
||||
* KICK_OUT("50", "token已被踢下线"),
|
||||
* ;
|
||||
*
|
||||
* @Nonnull
|
||||
* private final String code;
|
||||
* @Nonnull
|
||||
* private final String defaultMessage;
|
||||
*
|
||||
* Type(String code, String defaultMessage) {
|
||||
* this.code = code;
|
||||
* this.defaultMessage = defaultMessage;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* @Nonnull
|
||||
* public String getCode() {
|
||||
* return code;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* public String getDefaultMessage() {
|
||||
* return defaultMessage;
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* @Nonnull
|
||||
* public LoginException create() {
|
||||
* return new LoginException(this, this.defaultMessage);
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* @Nonnull
|
||||
* public LoginException create(String message) {
|
||||
* return new LoginException(this, message);
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* @Nonnull
|
||||
* public LoginException create(Throwable cause) {
|
||||
* return new LoginException(this, cause);
|
||||
* }
|
||||
*
|
||||
* @Override
|
||||
* @Nonnull
|
||||
* public LoginException create(String message, Throwable cause) {
|
||||
* return new LoginException(this, message, cause);
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* 使用时,可以使用这种方式创建并抛出异常:
|
||||
* <pre>
|
||||
* throw LoginException.Type.TOKEN_TIMEOUT.create();
|
||||
* </pre>
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108}">ZhouXY</a>
|
||||
*/
|
||||
public interface ExceptionType<E extends Exception> extends IWithCode<String> {
|
||||
|
||||
String getDefaultMessage();
|
||||
|
||||
@Nonnull
|
||||
E create();
|
||||
|
||||
@Nonnull
|
||||
E create(String message);
|
||||
|
||||
@Nonnull
|
||||
E create(Throwable cause);
|
||||
|
||||
@Nonnull
|
||||
E create(String message, Throwable cause);
|
||||
|
||||
}
|
@@ -20,7 +20,6 @@ import java.time.format.DateTimeParseException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||
|
||||
/**
|
||||
@@ -42,13 +41,8 @@ public final class ParsingFailureException extends RuntimeException {
|
||||
|
||||
private final Type type;
|
||||
|
||||
private ParsingFailureException(Type type) {
|
||||
super(type.getDefaultMsg());
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private ParsingFailureException(Type type, String msg) {
|
||||
super(msg);
|
||||
private ParsingFailureException(Type type, String message) {
|
||||
super(message);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -57,64 +51,64 @@ public final class ParsingFailureException extends RuntimeException {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private ParsingFailureException(Type type, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
private ParsingFailureException(Type type, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ParsingFailureException() {
|
||||
this(Type.DEFAULT);
|
||||
this(Type.DEFAULT, Type.DEFAULT.getDefaultMessage());
|
||||
}
|
||||
|
||||
public ParsingFailureException(String msg) {
|
||||
this(Type.DEFAULT, msg);
|
||||
public ParsingFailureException(String message) {
|
||||
this(Type.DEFAULT, message);
|
||||
}
|
||||
|
||||
public ParsingFailureException(Throwable e) {
|
||||
this(Type.DEFAULT, e);
|
||||
public ParsingFailureException(Throwable cause) {
|
||||
this(Type.DEFAULT, cause);
|
||||
}
|
||||
|
||||
public ParsingFailureException(String msg, Throwable e) {
|
||||
this(Type.DEFAULT, msg, e);
|
||||
public ParsingFailureException(String message, Throwable cause) {
|
||||
this(Type.DEFAULT, message, cause);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type) {
|
||||
return new ParsingFailureException(type);
|
||||
public static ParsingFailureException of(DateTimeParseException cause) {
|
||||
if (cause == null) {
|
||||
return Type.DATE_TIME_PARSING_FAILURE.create();
|
||||
}
|
||||
return Type.DATE_TIME_PARSING_FAILURE.create(cause.getMessage(), cause);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type, String msg) {
|
||||
return new ParsingFailureException(type, msg);
|
||||
public static ParsingFailureException of(String message, DateTimeParseException cause) {
|
||||
return Type.DATE_TIME_PARSING_FAILURE.create(message, cause);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type, Throwable e) {
|
||||
return new ParsingFailureException(type, e);
|
||||
public static ParsingFailureException of(NumberFormatException cause) {
|
||||
if (cause == null) {
|
||||
return Type.NUMBER_PARSING_FAILURE.create();
|
||||
}
|
||||
return Type.NUMBER_PARSING_FAILURE.create(cause.getMessage(), cause);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type, String msg, Throwable e) {
|
||||
return new ParsingFailureException(type, msg, e);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(DateTimeParseException e) {
|
||||
return new ParsingFailureException(Type.DATE_TIME_PARSING_FAILURE, e.getMessage(), e);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(String msg, DateTimeParseException e) {
|
||||
return new ParsingFailureException(Type.DATE_TIME_PARSING_FAILURE, msg, e);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(NumberFormatException e) {
|
||||
return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, e.getMessage(), e);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(String msg, NumberFormatException e) {
|
||||
return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, msg, e);
|
||||
public static ParsingFailureException of(String message, NumberFormatException cause) {
|
||||
return Type.NUMBER_PARSING_FAILURE.create(message, cause);
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public enum Type implements IWithCode<String> {
|
||||
public String getCode() {
|
||||
return this.type.code;
|
||||
}
|
||||
|
||||
public static final Type DEFAULT = Type.DEFAULT;
|
||||
public static final Type NUMBER_PARSING_FAILURE = Type.NUMBER_PARSING_FAILURE;
|
||||
public static final Type DATE_TIME_PARSING_FAILURE = Type.DATE_TIME_PARSING_FAILURE;
|
||||
public static final Type JSON_PARSING_FAILURE = Type.JSON_PARSING_FAILURE;
|
||||
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
|
||||
|
||||
public enum Type implements ExceptionType<ParsingFailureException> {
|
||||
DEFAULT("00", "解析失败"),
|
||||
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||
@@ -123,13 +117,13 @@ public final class ParsingFailureException extends RuntimeException {
|
||||
;
|
||||
|
||||
@Nonnull
|
||||
final String code;
|
||||
private final String code;
|
||||
@Nonnull
|
||||
final String defaultMsg;
|
||||
private final String defaultMessage;
|
||||
|
||||
Type(String code, String defaultMsg) {
|
||||
Type(String code, String defaultMessage) {
|
||||
this.code = code;
|
||||
this.defaultMsg = defaultMsg;
|
||||
this.defaultMessage = defaultMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -138,8 +132,33 @@ public final class ParsingFailureException extends RuntimeException {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDefaultMsg() {
|
||||
return defaultMsg;
|
||||
@Override
|
||||
public String getDefaultMessage() {
|
||||
return defaultMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ParsingFailureException create() {
|
||||
return new ParsingFailureException(this, this.defaultMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ParsingFailureException create(String message) {
|
||||
return new ParsingFailureException(this, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ParsingFailureException create(Throwable cause) {
|
||||
return new ParsingFailureException(this, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ParsingFailureException create(String message, Throwable cause) {
|
||||
return new ParsingFailureException(this, message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -37,16 +37,16 @@ public class BizException extends RuntimeException {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public BizException(String msg) {
|
||||
super(msg);
|
||||
public BizException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BizException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public BizException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
public BizException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -18,7 +18,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
import xyz.zhouxy.plusone.commons.exception.ExceptionType;
|
||||
|
||||
/**
|
||||
* InvalidInputException
|
||||
@@ -38,12 +38,12 @@ public final class InvalidInputException extends RequestParamsException {
|
||||
private final Type type;
|
||||
|
||||
private InvalidInputException(Type type) {
|
||||
super(type.getDefaultMsg());
|
||||
super(type.getDefaultMessage());
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private InvalidInputException(Type type, String msg) {
|
||||
super(msg);
|
||||
private InvalidInputException(Type type, String message) {
|
||||
super(message);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public final class InvalidInputException extends RequestParamsException {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private InvalidInputException(Type type, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
private InvalidInputException(Type type, String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -61,47 +61,27 @@ public final class InvalidInputException extends RequestParamsException {
|
||||
this(Type.DEFAULT);
|
||||
}
|
||||
|
||||
public InvalidInputException(String msg) {
|
||||
this(Type.DEFAULT, msg);
|
||||
public InvalidInputException(String message) {
|
||||
this(Type.DEFAULT, message);
|
||||
}
|
||||
|
||||
public InvalidInputException(Throwable e) {
|
||||
this(Type.DEFAULT, e);
|
||||
public InvalidInputException(Throwable cause) {
|
||||
this(Type.DEFAULT, cause);
|
||||
}
|
||||
|
||||
public InvalidInputException(String msg, Throwable e) {
|
||||
this(Type.DEFAULT, msg, e);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(Type type) {
|
||||
return new InvalidInputException(type);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(Type type, String msg) {
|
||||
return new InvalidInputException(type, msg);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(Type type, Throwable e) {
|
||||
return new InvalidInputException(type, e);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(Type type, String msg, Throwable e) {
|
||||
return new InvalidInputException(type, msg, e);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(Throwable e) {
|
||||
return new InvalidInputException(Type.DEFAULT, e.getMessage(), e);
|
||||
}
|
||||
|
||||
public static InvalidInputException of(String msg, Throwable e) {
|
||||
return new InvalidInputException(Type.DEFAULT, msg, e);
|
||||
public InvalidInputException(String message, Throwable cause) {
|
||||
this(Type.DEFAULT, message, cause);
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public enum Type implements IWithCode<String> {
|
||||
public Object getCode() {
|
||||
return this.type.code;
|
||||
}
|
||||
|
||||
public enum Type implements ExceptionType<InvalidInputException> {
|
||||
DEFAULT("00", "用户输入内容非法"),
|
||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||
@@ -109,12 +89,14 @@ public final class InvalidInputException extends RequestParamsException {
|
||||
INFRINGE_COPYRIGHT("04", "文件侵犯版权"),
|
||||
;
|
||||
|
||||
@Nonnull
|
||||
final String code;
|
||||
final String defaultMsg;
|
||||
@Nonnull
|
||||
final String defaultMessage;
|
||||
|
||||
Type(String code, String defaultMsg) {
|
||||
this.code = code;
|
||||
this.defaultMsg = defaultMsg;
|
||||
this.defaultMessage = defaultMsg;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,8 +105,33 @@ public final class InvalidInputException extends RequestParamsException {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDefaultMsg() {
|
||||
return defaultMsg;
|
||||
@Override
|
||||
public String getDefaultMessage() {
|
||||
return defaultMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InvalidInputException create() {
|
||||
return new InvalidInputException(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InvalidInputException create(String message) {
|
||||
return new InvalidInputException(this, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InvalidInputException create(Throwable cause) {
|
||||
return new InvalidInputException(this, cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InvalidInputException create(String message, Throwable cause) {
|
||||
return new InvalidInputException(this, message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -34,16 +34,16 @@ public class RequestParamsException extends BizException {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public RequestParamsException(String msg) {
|
||||
super(msg);
|
||||
public RequestParamsException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public RequestParamsException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public RequestParamsException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
public RequestParamsException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,15 +39,15 @@ public final class DataOperationResultException extends SysException {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public DataOperationResultException(String msg) {
|
||||
super(msg);
|
||||
public DataOperationResultException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DataOperationResultException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public DataOperationResultException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
public DataOperationResultException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
@@ -33,15 +33,15 @@ public class NoAvailableMacFoundException extends SysException {
|
||||
super();
|
||||
}
|
||||
|
||||
public NoAvailableMacFoundException(String msg) {
|
||||
super(msg);
|
||||
public NoAvailableMacFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public NoAvailableMacFoundException(Throwable e) {
|
||||
super(e);
|
||||
public NoAvailableMacFoundException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public NoAvailableMacFoundException(String msg, Throwable e) {
|
||||
super(msg, e);
|
||||
public NoAvailableMacFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
@@ -30,19 +30,19 @@ public class SysException extends RuntimeException {
|
||||
|
||||
private static final String DEFAULT_MSG = "系统异常";
|
||||
|
||||
protected SysException() {
|
||||
public SysException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public SysException(String msg) {
|
||||
super(msg);
|
||||
public SysException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SysException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public SysException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
public SysException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user