forked from plusone/plusone-commons
新增通用异常,调整异常结构。
This commit is contained in:
parent
5932bbd53f
commit
443116a5a2
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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 java.time.format.DateTimeParseException;
|
||||
|
||||
/**
|
||||
* 解析失败异常
|
||||
*
|
||||
* <p>
|
||||
* 解析失败的不一定是客户传的参数,也可能是其它来源的数据解析失败
|
||||
* 如果表示用户传参造成的解析失败,可使用 RequestParamsException(Throwable cause),
|
||||
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
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);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private ParsingFailureException(Type type, Throwable cause) {
|
||||
super(cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private ParsingFailureException(Type type, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type) {
|
||||
return new ParsingFailureException(type);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type, String msg) {
|
||||
return new ParsingFailureException(type, msg);
|
||||
}
|
||||
|
||||
public static ParsingFailureException of(Type type, Throwable e) {
|
||||
return new ParsingFailureException(type, e);
|
||||
}
|
||||
|
||||
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 Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
DEFAULT("4010500", "解析失败"),
|
||||
NUMBER_PARSING_FAILURE("4010501", "数字转换失败"),
|
||||
DATE_TIME_PARSING_FAILURE("4010502", "时间解析失败"),
|
||||
JSON_PARSING_FAILURE("4010503", "JSON 解析失败"),
|
||||
XML_PARSING_FAILURE("4010504", "XML 解析失败"),
|
||||
;
|
||||
|
||||
final String code;
|
||||
final String defaultMsg;
|
||||
|
||||
Type(String code, String defaultMsg) {
|
||||
this.code = code;
|
||||
this.defaultMsg = defaultMsg;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDefaultMsg() {
|
||||
return defaultMsg;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.business;
|
||||
|
||||
/**
|
||||
* BizException
|
||||
*
|
||||
* <p>
|
||||
* 业务异常
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。</b>
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
|
||||
private static final String DEFAULT_MSG = "业务异常";
|
||||
|
||||
public BizException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public BizException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public BizException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public BizException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.business;
|
||||
|
||||
/**
|
||||
* InvalidInputException
|
||||
*
|
||||
* <p>
|
||||
* 用户输入内容非法
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* <b>NOTE: 属业务异常</b>
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public final class InvalidInputException extends RequestParamsException {
|
||||
|
||||
private final Type type;
|
||||
|
||||
private InvalidInputException(Type type) {
|
||||
super(type.getDefaultMsg());
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private InvalidInputException(Type type, String msg) {
|
||||
super(msg);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private InvalidInputException(Type type, Throwable cause) {
|
||||
super(cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private InvalidInputException(Type type, String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
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 Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
DEFAULT("00", "用户输入内容非法"),
|
||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||
PICTURE_CONTAINS_ILLEGAL_INFORMATION("03", "图片包含违禁信息"),
|
||||
INFRINGE_COPYRIGHT("04", "文件侵犯版权"),
|
||||
;
|
||||
|
||||
final String code;
|
||||
final String defaultMsg;
|
||||
|
||||
Type(String code, String defaultMsg) {
|
||||
this.code = code;
|
||||
this.defaultMsg = defaultMsg;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getDefaultMsg() {
|
||||
return defaultMsg;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 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.business;
|
||||
|
||||
/**
|
||||
* RequestParamsException
|
||||
*
|
||||
* <p>
|
||||
* 用户请求参数错误
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class RequestParamsException extends BizException {
|
||||
|
||||
private static final String DEFAULT_MSG = "用户请求参数错误";
|
||||
|
||||
public RequestParamsException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public RequestParamsException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public RequestParamsException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public RequestParamsException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.system;
|
||||
|
||||
/**
|
||||
* DataOperationResultException
|
||||
*
|
||||
* <p>
|
||||
* 当数据操作的结果不符合预期时抛出。
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* 比如当一个 insert 或 update 操作时,预计影响数据库中的一行数据,但结果却影响了零条数据或多条数据,
|
||||
* 当出现这种始料未及的诡异情况时,抛出 {@link DataOperationResultException} 并回滚事务。
|
||||
* 后续需要排查原因。
|
||||
* </p>
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public final class DataOperationResultException extends SysException {
|
||||
|
||||
private static final String DEFAULT_MSG = "数据操作的结果不符合预期";
|
||||
|
||||
public DataOperationResultException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public DataOperationResultException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public DataOperationResultException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public DataOperationResultException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
@ -14,9 +14,19 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
package xyz.zhouxy.plusone.commons.exception.system;
|
||||
|
||||
public class NoAvailableMacFoundException extends Exception {
|
||||
/**
|
||||
* NoAvailableMacFoundException
|
||||
*
|
||||
* <p>
|
||||
* 在无法找到可访问的 Mac 地址时抛出
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class NoAvailableMacFoundException extends SysException {
|
||||
private static final long serialVersionUID = 152827098461071551L;
|
||||
|
||||
public NoAvailableMacFoundException() {
|
@ -14,29 +14,35 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
package xyz.zhouxy.plusone.commons.exception.system;
|
||||
|
||||
/**
|
||||
* 输入参数无效异常
|
||||
*
|
||||
* @author ZhouXY
|
||||
* 系统异常
|
||||
*
|
||||
* <p>
|
||||
* 通常表示应用代码存在问题,或因环境问题,引发异常。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public class InvalidInputException extends RuntimeException {
|
||||
private static final long serialVersionUID = 20241017111423L;
|
||||
public class SysException extends RuntimeException {
|
||||
|
||||
public InvalidInputException() {
|
||||
super();
|
||||
private static final String DEFAULT_MSG = "系统异常";
|
||||
|
||||
protected SysException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public InvalidInputException(String message) {
|
||||
super(message);
|
||||
public SysException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
public InvalidInputException(Throwable cause) {
|
||||
public SysException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public InvalidInputException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
public SysException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ import java.util.Enumeration;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.NoAvailableMacFoundException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
||||
|
||||
|
||||
public class IdWorker {
|
||||
@ -160,7 +160,7 @@ public class IdWorker {
|
||||
/**
|
||||
* use lowest 10 bit of available MAC as workerId
|
||||
* @return workerId
|
||||
* @throws SocketException
|
||||
* @throws SocketException
|
||||
* @throws NoAvailableMacFoundException when there is no available mac found
|
||||
*/
|
||||
private static long generateWorkerIdBaseOnMac() throws SocketException, NoAvailableMacFoundException {
|
||||
|
@ -21,7 +21,7 @@ import java.io.ObjectStreamClass;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.exception.NoAvailableMacFoundException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
|
||||
|
||||
@Slf4j
|
||||
class SerialTests {
|
||||
|
Loading…
x
Reference in New Issue
Block a user