From 1decd04008b64c738ab48a5f74156e7ff8249d0f Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Fri, 21 Jun 2019 11:41:55 +0800 Subject: [PATCH 1/4] #1278 --- .../alibaba/nacos/api/config/ConfigFactory.java | 2 +- .../nacos/api/exception/NacosException.java | 15 +++++++++++++++ .../alibaba/nacos/api/naming/NamingFactory.java | 4 ++-- .../nacos/api/naming/NamingMaintainFactory.java | 4 ++-- .../nacos/client/config/impl/ClientWorker.java | 2 +- .../raft/RaftConsistencyServiceImpl.java | 8 ++++---- 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java index 52179b112..450c13853 100644 --- a/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/config/ConfigFactory.java @@ -42,7 +42,7 @@ public class ConfigFactory { ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties); return vendorImpl; } catch (Throwable e) { - throw new NacosException(-400, e.getMessage()); + throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index aab756e60..3e4d2d058 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -31,6 +31,8 @@ public class NacosException extends Exception { private String errMsg; + private Throwable throwable; + public NacosException() { } @@ -40,6 +42,19 @@ public class NacosException extends Exception { this.errMsg = errMsg; } + public NacosException(int errCode, Throwable throwable) { + super(throwable); + this.errCode = errCode; + this.throwable = throwable; + } + + public NacosException(int errCode, String errMsg, Throwable throwable) { + super(errMsg, throwable); + this.errCode = errCode; + this.errMsg = errMsg; + this.throwable = throwable; + } + public int getErrCode() { return errCode; } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java index 78c34d31d..745ccd275 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingFactory.java @@ -34,7 +34,7 @@ public class NamingFactory { NamingService vendorImpl = (NamingService)constructor.newInstance(serverList); return vendorImpl; } catch (Throwable e) { - throw new NacosException(-400, e.getMessage()); + throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } @@ -45,7 +45,7 @@ public class NamingFactory { NamingService vendorImpl = (NamingService)constructor.newInstance(properties); return vendorImpl; } catch (Throwable e) { - throw new NacosException(-400, e.getMessage()); + throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java index d2f3b8e61..44e607fb9 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/NamingMaintainFactory.java @@ -34,7 +34,7 @@ public class NamingMaintainFactory { NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList); return vendorImpl; } catch (Throwable e) { - throw new NacosException(-400, e.getMessage()); + throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } @@ -45,7 +45,7 @@ public class NamingMaintainFactory { NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(properties); return vendorImpl; } catch (Throwable e) { - throw new NacosException(-400, e.getMessage()); + throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e); } } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index a7665f051..e6b932de6 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -221,7 +221,7 @@ public class ClientWorker { "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(), dataId, group, tenant); LOGGER.error(message, e); - throw new NacosException(NacosException.SERVER_ERROR, e.getMessage()); + throw new NacosException(NacosException.SERVER_ERROR, e); } switch (result.code) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftConsistencyServiceImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftConsistencyServiceImpl.java index 0a3fbf610..c58838d20 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftConsistencyServiceImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftConsistencyServiceImpl.java @@ -51,7 +51,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService raftCore.signalPublish(key, value); } catch (Exception e) { Loggers.RAFT.error("Raft put failed.", e); - throw new NacosException(NacosException.SERVER_ERROR, "Raft put failed, key:" + key + ", value:" + value); + throw new NacosException(NacosException.SERVER_ERROR, "Raft put failed, key:" + key + ", value:" + value, e); } } @@ -69,7 +69,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService raftCore.unlistenAll(key); } catch (Exception e) { Loggers.RAFT.error("Raft remove failed.", e); - throw new NacosException(NacosException.SERVER_ERROR, "Raft remove failed, key:" + key); + throw new NacosException(NacosException.SERVER_ERROR, "Raft remove failed, key:" + key, e); } } @@ -98,7 +98,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService raftCore.onPublish(datum, source); } catch (Exception e) { Loggers.RAFT.error("Raft onPut failed.", e); - throw new NacosException(NacosException.SERVER_ERROR, "Raft onPut failed, datum:" + datum + ", source: " + source); + throw new NacosException(NacosException.SERVER_ERROR, "Raft onPut failed, datum:" + datum + ", source: " + source, e); } } @@ -107,7 +107,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService raftCore.onDelete(datum.key, source); } catch (Exception e) { Loggers.RAFT.error("Raft onRemove failed.", e); - throw new NacosException(NacosException.SERVER_ERROR, "Raft onRemove failed, datum:" + datum + ", source: " + source); + throw new NacosException(NacosException.SERVER_ERROR, "Raft onRemove failed, datum:" + datum + ", source: " + source, e); } } } From 4c875507504c1924ee866411d62778bee524587c Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Mon, 24 Jun 2019 13:55:50 +0800 Subject: [PATCH 2/4] update nacosException --- .../nacos/api/exception/NacosException.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index 3e4d2d058..13b93b8a6 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -15,6 +15,11 @@ */ package com.alibaba.nacos.api.exception; +import com.alibaba.nacos.api.common.Constants; +import org.apache.commons.lang3.StringUtils; + +import java.lang.reflect.InvocationTargetException; + /** * Nacos Exception * @@ -31,7 +36,7 @@ public class NacosException extends Exception { private String errMsg; - private Throwable throwable; + private Throwable causeThrowable; public NacosException() { } @@ -45,14 +50,14 @@ public class NacosException extends Exception { public NacosException(int errCode, Throwable throwable) { super(throwable); this.errCode = errCode; - this.throwable = throwable; + setCauseThrowable(throwable); } public NacosException(int errCode, String errMsg, Throwable throwable) { super(errMsg, throwable); this.errCode = errCode; this.errMsg = errMsg; - this.throwable = throwable; + setCauseThrowable(throwable); } public int getErrCode() { @@ -60,7 +65,13 @@ public class NacosException extends Exception { } public String getErrMsg() { - return errMsg; + if (!StringUtils.isBlank(this.errMsg)) { + return errMsg; + } + if (this.causeThrowable != null) { + return causeThrowable.getMessage(); + } + return Constants.NULL; } public void setErrCode(int errCode) { @@ -71,9 +82,20 @@ public class NacosException extends Exception { this.errMsg = errMsg; } + public void setCauseThrowable(Throwable throwable) { + this.causeThrowable = getCauseThrowable(throwable); + } + + private Throwable getCauseThrowable(Throwable t) { + if (t.getCause() == null) { + return t; + } + return getCauseThrowable(t.getCause()); + } + @Override public String toString() { - return "ErrCode:" + errCode + ",ErrMsg:" + errMsg; + return "ErrCode:" + getErrCode() + ",ErrMsg:" + getErrMsg(); } /** From 2ca8e0c1b33f5c7f50d3efae2a1b76e6e52e75df Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Mon, 24 Jun 2019 13:57:51 +0800 Subject: [PATCH 3/4] fix --- .../java/com/alibaba/nacos/api/exception/NacosException.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index 13b93b8a6..15739160f 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -18,8 +18,6 @@ package com.alibaba.nacos.api.exception; import com.alibaba.nacos.api.common.Constants; import org.apache.commons.lang3.StringUtils; -import java.lang.reflect.InvocationTargetException; - /** * Nacos Exception * From 6d6e4a184c15af3c0a427c424a24c9506b5940ba Mon Sep 17 00:00:00 2001 From: caoyixiong Date: Mon, 24 Jun 2019 14:00:49 +0800 Subject: [PATCH 4/4] format --- .../java/com/alibaba/nacos/api/exception/NacosException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java index 15739160f..f9fa85814 100644 --- a/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java +++ b/api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java @@ -93,7 +93,7 @@ public class NacosException extends Exception { @Override public String toString() { - return "ErrCode:" + getErrCode() + ",ErrMsg:" + getErrMsg(); + return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg(); } /**