View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
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  }