新增 BaseRuntimeException 作为基础运行时异常,原 BaseException 作为基础检查型异常。

feature/security
ZhouXY108 2023-10-18 10:54:12 +08:00
parent eba31a93f3
commit 94d34faffd
3 changed files with 82 additions and 1 deletions

View File

@ -27,7 +27,7 @@ import java.util.Objects;
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a> * @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/ */
public abstract class BaseException public abstract class BaseException
extends RuntimeException extends Exception
implements IWithCode<String> { implements IWithCode<String> {
private static final long serialVersionUID = -2546365325001947203L; private static final long serialVersionUID = -2546365325001947203L;

View File

@ -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 <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
public abstract class BaseRuntimeException
extends RuntimeException
implements IWithCode<String> {
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;
}
}

View File

@ -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();
}
}