From 3887a5e0b9c31d141befb73d1b0e5d4446b96510 Mon Sep 17 00:00:00 2001 From: KomachiSion <263976490@qq.com> Date: Tue, 26 May 2020 15:39:24 +0800 Subject: [PATCH] Abstract NacosRuntimeException as unchecked exception super class --- .../NacosDeserializationException.java | 17 ++++-- .../runtime/NacosRuntimeException.java | 59 +++++++++++++++++++ .../NacosSerializationException.java | 17 ++++-- .../nacos/common/utils/JacksonUtils.java | 20 +++---- 4 files changed, 93 insertions(+), 20 deletions(-) rename api/src/main/java/com/alibaba/nacos/api/exception/{ => runtime}/NacosDeserializationException.java (72%) create mode 100644 api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosRuntimeException.java rename api/src/main/java/com/alibaba/nacos/api/exception/{ => runtime}/NacosSerializationException.java (71%) diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosDeserializationException.java b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java similarity index 72% rename from api/src/main/java/com/alibaba/nacos/api/exception/NacosDeserializationException.java rename to api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java index 6e24b47dc..77c423e87 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosDeserializationException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosDeserializationException.java @@ -14,14 +14,16 @@ * limitations under the License. */ -package com.alibaba.nacos.api.exception; +package com.alibaba.nacos.api.exception.runtime; /** * Nacos deserialization exception. * * @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; @@ -32,19 +34,24 @@ public class NacosDeserializationException extends RuntimeException { private Class targetClass; public NacosDeserializationException() { + super(ERROR_CODE); } 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; } public NacosDeserializationException(Throwable throwable) { - super(DEFAULT_MSG, throwable); + super(ERROR_CODE, DEFAULT_MSG, 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; } + + public Class getTargetClass() { + return targetClass; + } } diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosRuntimeException.java b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosRuntimeException.java new file mode 100644 index 000000000..8f63c90d6 --- /dev/null +++ b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosRuntimeException.java @@ -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; + } +} diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosSerializationException.java b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosSerializationException.java similarity index 71% rename from api/src/main/java/com/alibaba/nacos/api/exception/NacosSerializationException.java rename to api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosSerializationException.java index 620450225..b783ab27c 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosSerializationException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/runtime/NacosSerializationException.java @@ -14,14 +14,16 @@ * limitations under the License. */ -package com.alibaba.nacos.api.exception; +package com.alibaba.nacos.api.exception.runtime; /** * Nacos serialization exception. * * @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; @@ -32,19 +34,24 @@ public class NacosSerializationException extends RuntimeException { private Class serializedClass; public NacosSerializationException() { + super(ERROR_CODE); } 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; } public NacosSerializationException(Throwable throwable) { - super(DEFAULT_MSG, throwable); + super(ERROR_CODE, DEFAULT_MSG, 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; } + + public Class getSerializedClass() { + return serializedClass; + } } diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index 33c358965..e3c6a293d 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -16,8 +16,8 @@ package com.alibaba.nacos.common.utils; -import com.alibaba.nacos.api.exception.NacosDeserializationException; -import com.alibaba.nacos.api.exception.NacosSerializationException; +import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException; +import com.alibaba.nacos.api.exception.runtime.NacosSerializationException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -43,7 +43,7 @@ public final class JacksonUtils { try { return mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { - throw new NacosSerializationException(obj.getClass()); + throw new NacosSerializationException(obj.getClass(), e); } } @@ -51,7 +51,7 @@ public final class JacksonUtils { try { return ByteUtils.toBytes(mapper.writeValueAsString(obj)); } catch (JsonProcessingException e) { - throw new NacosSerializationException(obj.getClass()); + throw new NacosSerializationException(obj.getClass(), e); } } @@ -59,7 +59,7 @@ public final class JacksonUtils { try { return toObj(StringUtils.newString4UTF8(json), cls); } catch (Exception e) { - throw new NacosDeserializationException(cls); + throw new NacosDeserializationException(cls, e); } } @@ -67,7 +67,7 @@ public final class JacksonUtils { try { return toObj(StringUtils.newString4UTF8(json), cls); } catch (Exception e) { - throw new NacosDeserializationException(); + throw new NacosDeserializationException(e); } } @@ -75,7 +75,7 @@ public final class JacksonUtils { try { return mapper.readValue(json, typeReference); } catch (IOException e) { - throw new NacosDeserializationException(typeReference.getClass()); + throw new NacosDeserializationException(typeReference.getClass(), e); } } @@ -83,7 +83,7 @@ public final class JacksonUtils { try { return mapper.readValue(json, cls); } catch (IOException e) { - throw new NacosDeserializationException(cls); + throw new NacosDeserializationException(cls, e); } } @@ -91,7 +91,7 @@ public final class JacksonUtils { try { return mapper.readValue(json, mapper.constructType(type)); } catch (IOException e) { - throw new NacosDeserializationException(); + throw new NacosDeserializationException(e); } } @@ -99,7 +99,7 @@ public final class JacksonUtils { try { return mapper.readTree(json); } catch (IOException e) { - throw new NacosDeserializationException(); + throw new NacosDeserializationException(e); } }