Merge pull request #5 from alibaba/develop

merge
This commit is contained in:
caoyixiong 2019-06-25 21:47:24 -05:00 committed by GitHub
commit 781273faa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 12 deletions

View File

@ -42,7 +42,7 @@ public class ConfigFactory {
ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties); ConfigService vendorImpl = (ConfigService) constructor.newInstance(properties);
return vendorImpl; return vendorImpl;
} catch (Throwable e) { } catch (Throwable e) {
throw new NacosException(-400, e.getMessage()); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
} }
} }

View File

@ -15,6 +15,9 @@
*/ */
package com.alibaba.nacos.api.exception; package com.alibaba.nacos.api.exception;
import com.alibaba.nacos.api.common.Constants;
import org.apache.commons.lang3.StringUtils;
/** /**
* Nacos Exception * Nacos Exception
* *
@ -31,6 +34,8 @@ public class NacosException extends Exception {
private String errMsg; private String errMsg;
private Throwable causeThrowable;
public NacosException() { public NacosException() {
} }
@ -40,12 +45,31 @@ public class NacosException extends Exception {
this.errMsg = errMsg; this.errMsg = errMsg;
} }
public NacosException(int errCode, Throwable throwable) {
super(throwable);
this.errCode = errCode;
setCauseThrowable(throwable);
}
public NacosException(int errCode, String errMsg, Throwable throwable) {
super(errMsg, throwable);
this.errCode = errCode;
this.errMsg = errMsg;
setCauseThrowable(throwable);
}
public int getErrCode() { public int getErrCode() {
return errCode; return errCode;
} }
public String getErrMsg() { 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) { public void setErrCode(int errCode) {
@ -56,9 +80,20 @@ public class NacosException extends Exception {
this.errMsg = errMsg; 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 @Override
public String toString() { public String toString() {
return "ErrCode:" + errCode + ",ErrMsg:" + errMsg; return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg();
} }
/** /**

View File

@ -34,7 +34,7 @@ public class NamingFactory {
NamingService vendorImpl = (NamingService)constructor.newInstance(serverList); NamingService vendorImpl = (NamingService)constructor.newInstance(serverList);
return vendorImpl; return vendorImpl;
} catch (Throwable e) { } 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); NamingService vendorImpl = (NamingService)constructor.newInstance(properties);
return vendorImpl; return vendorImpl;
} catch (Throwable e) { } catch (Throwable e) {
throw new NacosException(-400, e.getMessage()); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
} }
} }
} }

View File

@ -34,7 +34,7 @@ public class NamingMaintainFactory {
NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList); NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(serverList);
return vendorImpl; return vendorImpl;
} catch (Throwable e) { } 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); NamingMaintainService vendorImpl = (NamingMaintainService)constructor.newInstance(properties);
return vendorImpl; return vendorImpl;
} catch (Throwable e) { } catch (Throwable e) {
throw new NacosException(-400, e.getMessage()); throw new NacosException(NacosException.CLIENT_INVALID_PARAM, e);
} }
} }

View File

@ -237,7 +237,7 @@ public class ClientWorker {
"[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(), "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(),
dataId, group, tenant); dataId, group, tenant);
LOGGER.error(message, e); LOGGER.error(message, e);
throw new NacosException(NacosException.SERVER_ERROR, e.getMessage()); throw new NacosException(NacosException.SERVER_ERROR, e);
} }
switch (result.code) { switch (result.code) {

View File

@ -51,7 +51,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
raftCore.signalPublish(key, value); raftCore.signalPublish(key, value);
} catch (Exception e) { } catch (Exception e) {
Loggers.RAFT.error("Raft put failed.", 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); raftCore.unlistenAll(key);
} catch (Exception e) { } catch (Exception e) {
Loggers.RAFT.error("Raft remove failed.", 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); raftCore.onPublish(datum, source);
} catch (Exception e) { } catch (Exception e) {
Loggers.RAFT.error("Raft onPut failed.", 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); raftCore.onDelete(datum.key, source);
} catch (Exception e) { } catch (Exception e) {
Loggers.RAFT.error("Raft onRemove failed.", 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);
} }
} }
} }