Skip to content

Commit

Permalink
replace lang3 in api (#3126)
Browse files Browse the repository at this point in the history
* replace lang3

* style:remove this
  • Loading branch information
lin-mt authored Jun 23, 2020
1 parent 9d34193 commit 1c84253
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 310 deletions.
255 changes: 132 additions & 123 deletions api/src/main/java/com/alibaba/nacos/api/exception/NacosException.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,136 +13,145 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.api.exception;

import com.alibaba.nacos.api.common.Constants;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.nacos.api.utils.StringUtils;

/**
* Nacos Exception
* Nacos Exception.
*
* @author Nacos
*/
public class NacosException extends Exception {

/**
* serialVersionUID
*/
private static final long serialVersionUID = -3913902031489277776L;

private int errCode;

private String errMsg;

private Throwable causeThrowable;

public NacosException() {
}

public NacosException(int errCode, String errMsg) {
super(errMsg);
this.errCode = errCode;
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() {
return errCode;
}

public String getErrMsg() {
if (!StringUtils.isBlank(this.errMsg)) {
return errMsg;
}
if (this.causeThrowable != null) {
return causeThrowable.getMessage();
}
return Constants.NULL;
}

public void setErrCode(int errCode) {
this.errCode = errCode;
}

public void setErrMsg(String 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
public String toString() {
return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg();
}

/**
* client error code
* -400 -503 throw exception to user
*/
/**
* invalid param(参数错误)
*/
public static final int CLIENT_INVALID_PARAM = -400;
/**
* over client threshold(超过server端的限流阈值)
*/
public static final int CLIENT_OVER_THRESHOLD = -503;

/**
* server error code
* 400 403 throw exception to user
* 500 502 503 change ip and retry
*/

/**
* invalid param(参数错误)
*/
public static final int INVALID_PARAM = 400;
/**
* no right(鉴权失败)
*/
public static final int NO_RIGHT = 403;
/**
* not found
*/
public static final int NOT_FOUND = 404;
/**
* conflict(写并发冲突)
*/
public static final int CONFLICT = 409;
/**
* server error(server异常,如超时)
*/
public static final int SERVER_ERROR = 500;
/**
* bad gateway(路由异常,如nginx后面的Server挂掉)
*/
public static final int BAD_GATEWAY = 502;
/**
* over threshold(超过server端的限流阈值)
*/
public static final int OVER_THRESHOLD = 503;

public static final int RESOURCE_NOT_FOUND = -404;

/**
* serialVersionUID.
*/
private static final long serialVersionUID = -3913902031489277776L;

private int errCode;

private String errMsg;

private Throwable causeThrowable;

public NacosException() {
}

public NacosException(final int errCode, final String errMsg) {
super(errMsg);
this.errCode = errCode;
this.errMsg = errMsg;
}

public NacosException(final int errCode, final Throwable throwable) {
super(throwable);
this.errCode = errCode;
this.setCauseThrowable(throwable);
}

public NacosException(final int errCode, final String errMsg, final Throwable throwable) {
super(errMsg, throwable);
this.errCode = errCode;
this.errMsg = errMsg;
this.setCauseThrowable(throwable);
}

public int getErrCode() {
return this.errCode;
}

public String getErrMsg() {
if (!StringUtils.isBlank(this.errMsg)) {
return this.errMsg;
}
if (this.causeThrowable != null) {
return this.causeThrowable.getMessage();
}
return Constants.NULL;
}

public void setErrCode(final int errCode) {
this.errCode = errCode;
}

public void setErrMsg(final String errMsg) {
this.errMsg = errMsg;
}

public void setCauseThrowable(final Throwable throwable) {
this.causeThrowable = this.getCauseThrowable(throwable);
}

private Throwable getCauseThrowable(final Throwable t) {
if (t.getCause() == null) {
return t;
}
return this.getCauseThrowable(t.getCause());
}

@Override
public String toString() {
return "ErrCode:" + getErrCode() + ", ErrMsg:" + getErrMsg();
}

/*
* client error code.
* -400 -503 throw exception to user.
*/

/**
* invalid param(参数错误).
*/
public static final int CLIENT_INVALID_PARAM = -400;

/**
* over client threshold(超过server端的限流阈值).
*/
public static final int CLIENT_OVER_THRESHOLD = -503;

/*
* server error code.
* 400 403 throw exception to user
* 500 502 503 change ip and retry
*/

/**
* invalid param(参数错误).
*/
public static final int INVALID_PARAM = 400;

/**
* no right(鉴权失败).
*/
public static final int NO_RIGHT = 403;

/**
* not found.
*/
public static final int NOT_FOUND = 404;

/**
* conflict(写并发冲突).
*/
public static final int CONFLICT = 409;

/**
* server error(server异常,如超时).
*/
public static final int SERVER_ERROR = 500;

/**
* bad gateway(路由异常,如nginx后面的Server挂掉).
*/
public static final int BAD_GATEWAY = 502;

/**
* over threshold(超过server端的限流阈值).
*/
public static final int OVER_THRESHOLD = 503;

public static final int RESOURCE_NOT_FOUND = -404;
}
Loading

0 comments on commit 1c84253

Please sign in to comment.