一、前言
关于taobao的tddl-common包(5.1.0)的com.taobao.tddl.common.utils.ExceptionErrorCodeUtils异常错误编码工具类,对异常堆栈exception信息获取错误编码getErrorCode、在异常exception内容中追加错误编码appendErrorCode、将异常Throwable堆栈转为字符串(exceptionToString)及,详见源码说明。
二、源码说明
package com.taobao.tddl.common.utils;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.PrintStream;@b@@b@public class ExceptionErrorCodeUtils@b@{@b@ public static final String separator = "-!@#-";@b@ public static final int Duplicate_entry = 10000;@b@ public static final int Null_Pointer_exception = 10001;@b@ public static final int Wrong_PassWD_Or_UserName = 10002;@b@ public static final int UNKNOWN_EXCEPTION = 10003;@b@ public static final int WRITE_NOT_ALLOW_EXECUTE_ON_MUTI_SERVERS = 10004;@b@ public static final int Read_only = 20001;@b@ public static final int Communication_link_failure = 30000;@b@ public static final int Connect_timeout = 30001;@b@@b@ public static Integer getErrorCode(String exception)@b@ {@b@ if ((exception == null) || (exception.isEmpty()))@b@ return null;@b@@b@ String[] errcodeWithException = exception.split("-!@#-");@b@ if (errcodeWithException.length > 2)@b@ throw new IllegalArgumentException("can't understand this exception: " + exception);@b@@b@ if (errcodeWithException.length < 2)@b@ return null;@b@ try@b@ {@b@ return Integer.valueOf(errcodeWithException[0]);@b@ } catch (NumberFormatException e) {@b@ throw new IllegalArgumentException("error exception can't be understand ." + exception, e);@b@ }@b@ }@b@@b@ public static String appendErrorCode(Integer errorCode, String exception) {@b@ StringBuilder sb = new StringBuilder();@b@ sb.append(errorCode).append("-!@#-").append(exception);@b@ return sb.toString();@b@ }@b@@b@ public static String exceptionToString(Throwable ex) {@b@ ByteArrayOutputStream out = new ByteArrayOutputStream(1024);@b@ PrintStream ps = new PrintStream(out);@b@ ex.printStackTrace(ps);@b@ return new String(out.toByteArray());@b@ }@b@@b@ public static String appendErrorCode(Integer errorCode, Throwable ex) {@b@ return appendErrorCode(errorCode, exceptionToString(ex));@b@ }@b@}