package com.lunhan.xxx.common.util;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.PrintStream;
|
|
/**
|
* 异常工具类
|
* @author linliu
|
* @date 2018-12-28
|
*/
|
public final class ExceptionUtil {
|
private ExceptionUtil() {
|
throw new IllegalStateException("Utility class");
|
}
|
|
public static String getMsg(Throwable e) {
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
PrintStream pout = new PrintStream(out);
|
e.printStackTrace(pout);
|
String ret = new String(out.toByteArray());
|
pout.close();
|
return ret;
|
}
|
|
public static String getDetails(Throwable ex) {
|
return getMessage(ex, true);
|
}
|
|
|
private static String getMessage(Throwable ex, boolean includeDetail) {
|
if (ex != null) {
|
StringBuilder builder = new StringBuilder();
|
Throwable current = ex;
|
|
do {
|
builder.append(ex.getClass().getName());
|
if (!StringUtil.isNullOrEmpty(current.getMessage())) {
|
builder.append(String.format(" : %s%n", current.getMessage()));
|
}
|
|
if (includeDetail) {
|
builder.append(getStackTraceInfo(current));
|
}
|
|
current = current.getCause();
|
} while(current != null);
|
|
return builder.toString();
|
} else {
|
return "";
|
}
|
}
|
|
private static String getStackTraceInfo(Throwable ex) {
|
StringBuilder sb = new StringBuilder();
|
if (ex != null) {
|
StackTraceElement[] trace = ex.getStackTrace();
|
StackTraceElement[] var6 = trace;
|
int var5 = trace.length;
|
|
for(int var4 = 0; var4 < var5; ++var4) {
|
StackTraceElement s = var6[var4];
|
sb.append(String.format("\tat %s%n", s));
|
}
|
}
|
|
return sb.toString();
|
}
|
}
|