diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java index 3b13195..7e15ed4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java @@ -27,7 +27,7 @@ import java.util.Objects; * @author ZhouXY */ public abstract class BaseException - extends RuntimeException + extends Exception implements IWithCode { private static final long serialVersionUID = -2546365325001947203L; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java new file mode 100644 index 0000000..3212d6a --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseRuntimeException.java @@ -0,0 +1,58 @@ +/* + * Copyright 2022-2023 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 xyz.zhouxy.plusone.commons.base.IWithCode; + +import javax.annotation.Nonnull; +import java.util.Objects; + +/** + * 带错误码的异常。 + * + * @author ZhouXY + */ +public abstract class BaseRuntimeException + extends RuntimeException + implements IWithCode { + + private static final long serialVersionUID = -6345888403567792664L; + + @Nonnull + private final String code; + + protected BaseRuntimeException(String code, String msg) { + super(msg); + this.code = Objects.requireNonNull(code); + } + + protected BaseRuntimeException(String code, Throwable cause) { + super(cause); + this.code = Objects.requireNonNull(code); + } + + protected BaseRuntimeException(String code, String msg, Throwable cause) { + super(msg, cause); + this.code = Objects.requireNonNull(code); + } + + @Nonnull + @Override + public final String getCode() { + return this.code; + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java new file mode 100644 index 0000000..c556af6 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/SerialTests.java @@ -0,0 +1,23 @@ +package xyz.zhouxy.plusone.commons; + +import java.io.ObjectStreamClass; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; +import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException; + +@Slf4j +class SerialTests { + + @Test + void testSerialVersionUID() { + long uid = getSerialVersionUID(BaseRuntimeException.class); + log.info("\n private static final long serialVersionUID = {}L;", uid); + } + + private long getSerialVersionUID(Class cl) { + ObjectStreamClass c = ObjectStreamClass.lookup(cl); + return c.getSerialVersionUID(); + } +}