liulin
2024-07-30 ce04dfcdd664df7e791a63800cab2cd2d12e878c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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();
    }
}