diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java new file mode 100644 index 0000000..5f9ae68 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java @@ -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; + +/** + * 解析失败异常 + * + *

+ * 解析失败的不一定是客户传的参数,也可能是其它来源的数据解析失败 + * 如果表示用户传参造成的解析失败,可使用 RequestParamsException(Throwable cause), + * 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出 + *

+ * + * @author ZhouXY + * @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; + } + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/business/BizException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/BizException.java new file mode 100644 index 0000000..eee5262 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/BizException.java @@ -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 + * + *

+ * 业务异常 + *

+ * + *

+ * NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。 + *

+ * @author ZhouXY + * @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); + } + +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java new file mode 100644 index 0000000..8b8495c --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java @@ -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 + * + *

+ * 用户输入内容非法 + *

+ * + *

+ * NOTE: 属业务异常 + *

+ * @author ZhouXY + * @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; + } + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/business/RequestParamsException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/RequestParamsException.java new file mode 100644 index 0000000..8f4f383 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/RequestParamsException.java @@ -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 + * + *

+ * 用户请求参数错误 + *

+ * + * @author ZhouXY + * @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); + } + +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/system/DataOperationResultException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/DataOperationResultException.java new file mode 100644 index 0000000..99db690 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/DataOperationResultException.java @@ -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 + * + *

+ * 当数据操作的结果不符合预期时抛出。 + *

+ * + *

+ * 比如当一个 insert 或 update 操作时,预计影响数据库中的一行数据,但结果却影响了零条数据或多条数据, + * 当出现这种始料未及的诡异情况时,抛出 {@link DataOperationResultException} 并回滚事务。 + * 后续需要排查原因。 + *

+ * @author ZhouXY + * @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); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/NoAvailableMacFoundException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/NoAvailableMacFoundException.java similarity index 76% rename from src/main/java/xyz/zhouxy/plusone/commons/exception/NoAvailableMacFoundException.java rename to src/main/java/xyz/zhouxy/plusone/commons/exception/system/NoAvailableMacFoundException.java index bcc6621..7897b46 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/NoAvailableMacFoundException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/NoAvailableMacFoundException.java @@ -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 + * + *

+ * 在无法找到可访问的 Mac 地址时抛出 + *

+ * + * @author ZhouXY + * @since 0.1.0 + */ +public class NoAvailableMacFoundException extends SysException { private static final long serialVersionUID = 152827098461071551L; public NoAvailableMacFoundException() { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/SysException.java similarity index 52% rename from src/main/java/xyz/zhouxy/plusone/commons/exception/InvalidInputException.java rename to src/main/java/xyz/zhouxy/plusone/commons/exception/system/SysException.java index 62059da..877d43a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/system/SysException.java @@ -14,29 +14,35 @@ * limitations under the License. */ -package xyz.zhouxy.plusone.commons.exception; +package xyz.zhouxy.plusone.commons.exception.system; /** - * 输入参数无效异常 - * - * @author ZhouXY + * 系统异常 + * + *

+ * 通常表示应用代码存在问题,或因环境问题,引发异常。 + *

+ * + * @author ZhouXY + * @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); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/IdWorker.java b/src/main/java/xyz/zhouxy/plusone/commons/util/IdWorker.java index 6ed543b..e806cb2 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/IdWorker.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/IdWorker.java @@ -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 { diff --git a/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java index ec6d9d8..59af919 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java @@ -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 {