package gaf3.core.exception;

@SuppressWarnings("serial")
public class BusinessError extends RuntimeException{
	
	public final static int ERR_UNKNOW = -1;
	public final static int ERR_BADPARAM	= -2;
	public final static int ERR_NETWORK	= -3;
	public final static int ERR_BAD_PASSWORD	= -4;
	public final static int ERR_NOT_LOGIN	= -5;
	public final static int ERR_ACCESS_DENIED	= -6;
	public final static int ERR_USER_NOTEXIST	= -7;
	public final static int ERR_DATA_NOTEXIST	= -8;
	public final static int ERR_DATA_EXISTED	= -9;
	public final static int ERR_DATA_INVALID	= -10;
	public final static int ERR_VERIFYCODE_INVALID	= -11;
	public final static int ERR_SERVICE_FAULT	= -12;
	public final static int ERR_TOKEN_EXPIRED	= -13;
	public final static int ERR_TOKEN_OBSOLETE	= -14;
	public final static int ERR_TOKEN_INVALID	= -15;
	public final static int ERR_BUSINESS	= -100;
	
	protected int errcode;
	protected String errmsg;
	protected String details;
	
	public BusinessError(String errmsg){
		super(errmsg);
		this.errcode = ERR_UNKNOW;
		this.errmsg = errmsg;
	}
	public BusinessError(int errcode){
		super(getErrDesc(errcode));
		this.errcode = errcode;
	}
	public BusinessError(int errcode, Throwable cause){
		super(getErrDesc(errcode),cause);
		this.errcode = errcode;
		this.details = cause.getLocalizedMessage();
	}
	public BusinessError(int errcode, String errmsg){
		this(errcode, errmsg, (String)null);
	}
	public BusinessError(int errcode, String errmsg, String details){
		super(errmsg);//super(getErrDesc(errcode,details));
		this.errcode = errcode;
		this.errmsg = errmsg;
		this.details = details;
	}
	public BusinessError(int errcode, String errmsg, Throwable cause){
		super(getErrDesc(errcode,errmsg),cause);
		this.errcode = errcode;
		this.errmsg = errmsg;
		this.details = cause.getLocalizedMessage();
	}
	public BusinessError(String errmsg, Throwable cause){
		super(errmsg,cause);
		this.errcode = ERR_UNKNOW;
		this.errmsg = errmsg;
		this.details = cause.getLocalizedMessage();
	}

	public static String getErrDesc(int errcode){
		switch(errcode){
		case ERR_BADPARAM:
			return "参数无效";
		case ERR_NETWORK:
			return "网络错误";
		case ERR_BAD_PASSWORD:
			return "密码错误";
		case ERR_BUSINESS:
			return "业务错误";
		case ERR_NOT_LOGIN:
			return "用户未登录";
		case ERR_USER_NOTEXIST:
			return "用户不存在";
		case ERR_DATA_NOTEXIST:
			return "数据不存在";
		case ERR_DATA_EXISTED:
			return "数据已存在";
		case ERR_ACCESS_DENIED:
			return "访问被拒绝";
		case ERR_DATA_INVALID:
			return "数据无效";
		case ERR_VERIFYCODE_INVALID:
			return "验证码无效";
		case ERR_SERVICE_FAULT:
			return "服务发生故障";
		case ERR_TOKEN_EXPIRED:
			return "Token已失效";
		case ERR_TOKEN_OBSOLETE:
			return "Token已被替代";
		case ERR_TOKEN_INVALID:
			return "Token无效";
		}

		return "系统错误";
	}

	public static String getErrDesc(int errcode,String detail){
		if(errcode == ERR_UNKNOW && detail != null)
			return detail;
		if(detail == null)
			return getErrDesc(errcode);

		return String.format("%s:%s", getErrDesc(errcode),detail);
	}

	public int getErrcode() {
		return errcode;
	}

	public String getErrmsg() {
		if(this.errmsg == null) return this.getMessage();
		return this.errmsg;
	}

	public String getDetails() {
		return details;
	}
}
