Abstract NacosRuntimeException as unchecked exception super class

This commit is contained in:
KomachiSion 2020-05-26 15:39:24 +08:00
parent 5b9780e2fc
commit 3887a5e0b9
4 changed files with 93 additions and 20 deletions

View File

@ -14,14 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.api.exception; package com.alibaba.nacos.api.exception.runtime;
/** /**
* Nacos deserialization exception. * Nacos deserialization exception.
* *
* @author yangyi * @author yangyi
*/ */
public class NacosDeserializationException extends RuntimeException { public class NacosDeserializationException extends NacosRuntimeException {
public static final int ERROR_CODE = 101;
private static final long serialVersionUID = -2742350751684273728L; private static final long serialVersionUID = -2742350751684273728L;
@ -32,19 +34,24 @@ public class NacosDeserializationException extends RuntimeException {
private Class<?> targetClass; private Class<?> targetClass;
public NacosDeserializationException() { public NacosDeserializationException() {
super(ERROR_CODE);
} }
public NacosDeserializationException(Class<?> targetClass) { public NacosDeserializationException(Class<?> targetClass) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName())); super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()));
this.targetClass = targetClass; this.targetClass = targetClass;
} }
public NacosDeserializationException(Throwable throwable) { public NacosDeserializationException(Throwable throwable) {
super(DEFAULT_MSG, throwable); super(ERROR_CODE, DEFAULT_MSG, throwable);
} }
public NacosDeserializationException(Class<?> targetClass, Throwable throwable) { public NacosDeserializationException(Class<?> targetClass, Throwable throwable) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()), throwable); super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()), throwable);
this.targetClass = targetClass; this.targetClass = targetClass;
} }
public Class<?> getTargetClass() {
return targetClass;
}
} }

View File

@ -0,0 +1,59 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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
*
* http://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 com.alibaba.nacos.api.exception.runtime;
/**
* Nacos runtime exception.
*
* @author yangyi
*/
public class NacosRuntimeException extends RuntimeException {
private static final long serialVersionUID = 3513491993982293262L;
public static final String ERROR_MESSAGE_FORMAT = "errCode: %d, errMsg: %s ";
private int errCode;
public NacosRuntimeException(int errCode) {
super();
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, String errMsg) {
super(String.format(ERROR_MESSAGE_FORMAT, errCode, errMsg));
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, Throwable throwable) {
super(throwable);
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, String errMsg, Throwable throwable) {
super(String.format(ERROR_MESSAGE_FORMAT, errCode, errMsg), throwable);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
}

View File

@ -14,14 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package com.alibaba.nacos.api.exception; package com.alibaba.nacos.api.exception.runtime;
/** /**
* Nacos serialization exception. * Nacos serialization exception.
* *
* @author yangyi * @author yangyi
*/ */
public class NacosSerializationException extends RuntimeException { public class NacosSerializationException extends NacosRuntimeException {
public static final int ERROR_CODE = 100;
private static final long serialVersionUID = -4308536346316915612L; private static final long serialVersionUID = -4308536346316915612L;
@ -32,19 +34,24 @@ public class NacosSerializationException extends RuntimeException {
private Class<?> serializedClass; private Class<?> serializedClass;
public NacosSerializationException() { public NacosSerializationException() {
super(ERROR_CODE);
} }
public NacosSerializationException(Class<?> serializedClass) { public NacosSerializationException(Class<?> serializedClass) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName())); super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()));
this.serializedClass = serializedClass; this.serializedClass = serializedClass;
} }
public NacosSerializationException(Throwable throwable) { public NacosSerializationException(Throwable throwable) {
super(DEFAULT_MSG, throwable); super(ERROR_CODE, DEFAULT_MSG, throwable);
} }
public NacosSerializationException(Class<?> serializedClass, Throwable throwable) { public NacosSerializationException(Class<?> serializedClass, Throwable throwable) {
super(String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()), throwable); super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()), throwable);
this.serializedClass = serializedClass; this.serializedClass = serializedClass;
} }
public Class<?> getSerializedClass() {
return serializedClass;
}
} }

View File

@ -16,8 +16,8 @@
package com.alibaba.nacos.common.utils; package com.alibaba.nacos.common.utils;
import com.alibaba.nacos.api.exception.NacosDeserializationException; import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
import com.alibaba.nacos.api.exception.NacosSerializationException; import com.alibaba.nacos.api.exception.runtime.NacosSerializationException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
@ -43,7 +43,7 @@ public final class JacksonUtils {
try { try {
return mapper.writeValueAsString(obj); return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass()); throw new NacosSerializationException(obj.getClass(), e);
} }
} }
@ -51,7 +51,7 @@ public final class JacksonUtils {
try { try {
return ByteUtils.toBytes(mapper.writeValueAsString(obj)); return ByteUtils.toBytes(mapper.writeValueAsString(obj));
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass()); throw new NacosSerializationException(obj.getClass(), e);
} }
} }
@ -59,7 +59,7 @@ public final class JacksonUtils {
try { try {
return toObj(StringUtils.newString4UTF8(json), cls); return toObj(StringUtils.newString4UTF8(json), cls);
} catch (Exception e) { } catch (Exception e) {
throw new NacosDeserializationException(cls); throw new NacosDeserializationException(cls, e);
} }
} }
@ -67,7 +67,7 @@ public final class JacksonUtils {
try { try {
return toObj(StringUtils.newString4UTF8(json), cls); return toObj(StringUtils.newString4UTF8(json), cls);
} catch (Exception e) { } catch (Exception e) {
throw new NacosDeserializationException(); throw new NacosDeserializationException(e);
} }
} }
@ -75,7 +75,7 @@ public final class JacksonUtils {
try { try {
return mapper.readValue(json, typeReference); return mapper.readValue(json, typeReference);
} catch (IOException e) { } catch (IOException e) {
throw new NacosDeserializationException(typeReference.getClass()); throw new NacosDeserializationException(typeReference.getClass(), e);
} }
} }
@ -83,7 +83,7 @@ public final class JacksonUtils {
try { try {
return mapper.readValue(json, cls); return mapper.readValue(json, cls);
} catch (IOException e) { } catch (IOException e) {
throw new NacosDeserializationException(cls); throw new NacosDeserializationException(cls, e);
} }
} }
@ -91,7 +91,7 @@ public final class JacksonUtils {
try { try {
return mapper.readValue(json, mapper.constructType(type)); return mapper.readValue(json, mapper.constructType(type));
} catch (IOException e) { } catch (IOException e) {
throw new NacosDeserializationException(); throw new NacosDeserializationException(e);
} }
} }
@ -99,7 +99,7 @@ public final class JacksonUtils {
try { try {
return mapper.readTree(json); return mapper.readTree(json);
} catch (IOException e) { } catch (IOException e) {
throw new NacosDeserializationException(); throw new NacosDeserializationException(e);
} }
} }