1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.ebay.carad.os.vitalsigns.util;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25
26 /***
27 * Simple utilities for Exceptions.
28 *
29 * @author Jeremy Kraybill
30 * @author Jeremy Thomerson
31 * @version $Id$
32 */
33 public abstract class ExceptionUtil {
34
35 /***
36 * Returns a nicely formatted, string-form stack trace of
37 * the given Throwable. Why doesn't java have this built in?
38 *
39 * @param throwable the Throwable item to convert
40 * @return a string-form stack trace
41 */
42 public static String stringValue(Throwable throwable) {
43 StringWriter writer = new StringWriter();
44 throwable.printStackTrace(new PrintWriter(writer));
45 return writer.toString();
46 }
47
48 /***
49 * Creates an entire dump of the throwables message, stack trace, and caused by.
50 * Helper method to create a pretty exception message that is actually meaningful in CF.
51 * CF only prints the <code>th.getMessage()</code>, so the message has to have everything
52 * packed into it.
53 *
54 * @param th The throwable to format.
55 * @return The pretty message.
56 */
57 public static final String fullDetailsMessage(Throwable th) {
58 return exceptionMessageImpl(th) +
59 "\n\nCAUSED BY:\n" +
60 (th != null ? exceptionMessageImpl(th.getCause()) : "NULL THROWABLE");
61 }
62
63 private static final String exceptionMessageImpl(Throwable th) {
64 return th == null ? "NULL" : "" +
65 "EXCEPTION: \n" +
66 "Message: " + th.getMessage() + "\n" +
67 "Stack Trace: \n" +
68 stringValue(th);
69 }
70 }