用Java根据错误值解析出错误类型和错误码

public class ErrorParser {
	public static void main(String[] args) {
		int ret = encodeDepError(Constants.ELEM_POOL, ErrorCode.STATUS
				| ErrorCode.LACK | ErrorCode.PROBED_FAILED);
		String retString = decodeDepError(ret);
		System.out.println(retString);
	}

	public static int encodeDepError(int type, int errorCode) {
		return -(type << 16 | errorCode);
	}

	public static String decodeDepError(int error) {
		int type;
		int errorCode;
		int positiveError = -error;
		type = positiveError >> 16;
		errorCode = positiveError & 0xFFFF;
		System.out.println("type = " + type);
		System.out.println("errorCode = " + errorCode);
		return "";
	}
}

interface ErrorCode {
	public static final int DUP = 1 << 0;
	public static final int STATUS = 1 << 1;
	public static final int LACK = 1 << 2;
	public static final int PROBED_FAILED = 1 << 3;
}

interface Constants {
		public static final int ELEM_CLUSTER = 0;
		public static final int ELEM_HOST = 1;
		public static final int ELEM_MON = 2;
		public static final int ELEM_DISK = 3;
		public static final int ELEM_OSD = 4;
		public static final int ELEM_MDS = 5;
		public static final int ELEM_POOL = 6;
		public static final int ELEM_ACCS = 7;
		public static final int ELEM_NTYPES = 8;
}

输出为:

type =6

errorCode = 14



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值