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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package Torello.Java;

import Torello.Java.Shell.C;

/**
 * <B><CODE>'Exception Cause Chain'</CODE></B> helps convert exception messages whose
 * <CODE><B>Throwable.cause()</B></CODE> method returns a non-null <CODE>cause</CODE>, thereby
 * unrolling <I>(and printing)</I> this chain of exceptions into a readable <CODE>String</CODE>.
 * 
 * <BR /><BR /><EMBED CLASS="external-html" DATA-FILE-ID="EXCC">
 */
@Torello.HTML.Tools.JavaDoc.StaticFunctional
public class EXCC
{
    private EXCC() { }

    /**
     * Convenience Method.
     * <BR />Invokes: {@link #toString(Throwable)}
     * <BR />And-Then: {@link StrIndent#indentTabs(String, int)}
     */
    public static String toString(Throwable t, int indentation)
    { return StrIndent.indentTabs(toString(t), indentation); }

    /**
     * Prints the {@code Exception}-message and {@code Exception}-stack trace to an output
     * {@code String}, and invokes, recursively, this method with any cause-{@code Throwable's}
     * that are present in this {@code Throwable}.
     * 
     * @param t Any Java {@code Throwable}
     * 
     * @return This method doesn't actually print anything to the screen or terminal, it just
     * returns a {@code String} that you may print yourself - <I>or write to a
     * {@code class StringBuilder}, or any variation of text-output you wish.</I>
     */
    public static String toString(Throwable t)
    {
        StackTraceElement[] steArr      = t.getStackTrace();
        StringBuilder       sb          = new StringBuilder();
        Throwable           cause       = t.getCause();
        String              m           = t.getMessage();
        String              lm          = t.getLocalizedMessage();
        boolean             hasMessage  = (m != null) && (m.length() > 0);
        boolean             hasLMess    = (lm != null) && (lm.length() > 0);

        if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
        if (hasLMess)   lm  = StrIndent.indent(lm, 1, false, true);

        sb.append(C.BRED + "THROWN: " + t.getClass().getCanonicalName() + C.RESET + "\n");

        if (hasMessage)
        {
            sb.append(C.BCYAN + "Throwable.getMessage():\n" + C.RESET + m + "\n");

            if (hasLMess && (! m.equals(lm)))
                sb.append(C.BCYAN + "Throwable.getLocalizedMessage():\n" + C.RESET + lm + "\n");
        }

        else if (hasLMess)
            sb.append(C.BCYAN + "Throwable.getLocalizedMessage():\n" + C.RESET + lm + "\n");

        else
            sb.append(C.BYELLOW + "No Exception Messages Provided.\n" + C.RESET);

        sb.append(C.BCYAN + "StackTrace:\n" + C.RESET);

        int temp, maxLN = 0;

        for (StackTraceElement ste : steArr)

            if ((temp = ste.getLineNumber()) > maxLN)
                maxLN = temp;

        int base10 = 2 /* 2: colon + space */ + ((int) Math.ceil(Math.log10(maxLN)));

        for (int k=0; k < steArr.length; k++)

            sb.append(
                '\t' + StringParse.rightSpacePad(steArr[k].getLineNumber() + ":", base10) +
                steArr[k].getClassName() + "." +
                steArr[k].getMethodName() + "()\n"
            );

        return (cause == null)
            ? sb.toString()
            : sb.toString() + StrIndent.indentTabs(toString(cause), 1);
    }


    /**
     * Prints the {@code Exception}-message to an output {@code String}.  Invokes, recursively,
     * this method with any cause-{@code Throwable's} that are present in this {@code Throwable}.
     *
     * <BR /><BR />This method differs from {@link #toString(Throwable)}, in that it <I>does not
     * print the {@code StackTrace's} to the output {@code String}.</I>
     * 
     * @param t Any Java {@code Throwable}
     * 
     * @return This method doesn't actually print anything to the screen or terminal, it just
     * returns a {@code String} that you may print yourself - <I>or write to a
     * {@code class StorageBuffer,} or any variation of logging you wish.</I>
     */
    public static String toStringNoST(Throwable t)
    {
        StringBuilder       sb          = new StringBuilder();
        Throwable           cause       = t.getCause();
        String              m           = t.getMessage();
        String              lm          = t.getLocalizedMessage();
        boolean             hasMessage  = (m != null) && (m.length() > 0);
        boolean             hasLMess    = (lm != null) && (lm.length() > 0);

        if (hasMessage) m   = StrIndent.indent(m, 1, false, true);
        if (hasLMess)   lm  = StrIndent.indent(lm, 1, false, true);

        sb.append(C.BRED + "THROWN: " + t.getClass().getCanonicalName() + C.RESET + "\n");

        if (hasMessage)
        {
            sb.append(C.BCYAN + "Throwable.getMessage():\n" + C.RESET + m + "\n");

            if (hasLMess && (! m.equals(lm)))
                sb.append(C.BCYAN + "Throwable.getLocalizedMessage():\n" + C.RESET + lm + "\n");
        }

        else if (hasLMess)
            sb.append(C.BCYAN + "Throwable.getLocalizedMessage():\n" + C.RESET + lm + "\n");

        else
            sb.append(C.BYELLOW + "No Exception Messages Provided.\n" + C.RESET);

        return (cause == null)
            ? sb.toString()
            : sb.toString() + StrIndent.indentTabs(toStringNoST(cause), 1);
    }
}