Class ExceptionUtil
This class contains static utility methods for handling and manipulating exceptions;
the only one you're likely to call being
getStackTraceWithRevisions(Throwable, ClassLoader, int, String)
,
which extracts CVS revision information from classes to produce an annotated (and highlighted)
stack trace.
When passing in ClassLoader
s to these methods, you may want to try one of
- this.getClass().getClassLoader()
- Thread.currentThread().getContextClassLoader()
- Author:
- knoxg
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Allow stack trace elements to be highlighted, as bold HTMLstatic final int
Allow stack trace elements to be highlighted, with span'ed CSS elementsstatic final int
Perform no stack trace element highlightingstatic final int
Allow stack trace elements to be highlighted, as textstatic final int
The number of characters of the git revision to include in non-CSS stack traces (from the left side of the String) -
Method Summary
Modifier and TypeMethodDescriptionstatic String
escapeHtml
(String string) Returns the HTML-escaped form of a string.static String
getClassRevision2
(ClassLoader loader, String className) An alternate implementation ofgetClassRevision(ClassLoader, String)
, which searches the raw bytecode of the class, rather than using Java reflection.static int
Return the number of elements in the current thread's stack trace (i.e.static String
Converts an exception's stack trace to a string.getStackTraceSummary
(Throwable throwable) Returns a list of all the messages contained within a exception (following the causal chain of the supplied exception).static String
getStackTraceWithRevisions
(Throwable e, ClassLoader loader, int highlight, String highlightPrefix) Converts an exception's stack trace to a string.
-
Field Details
-
HIGHLIGHT_NONE
Perform no stack trace element highlighting- See Also:
-
HIGHLIGHT_TEXT
Allow stack trace elements to be highlighted, as text- See Also:
-
HIGHLIGHT_HTML
Allow stack trace elements to be highlighted, as bold HTML- See Also:
-
HIGHLIGHT_HTML_WITH_CSS
Allow stack trace elements to be highlighted, with span'ed CSS elements- See Also:
-
NUM_CHARACTERS_GIT_REVISION
The number of characters of the git revision to include in non-CSS stack traces (from the left side of the String)- See Also:
-
-
Method Details
-
getStackTrace
Converts an exception's stack trace to a string. If the exception passed to this function is null, returns the empty string.- Parameters:
e
- exception- Returns:
- string representation of the exception's stack trace
-
getStackTraceWithRevisions
public static String getStackTraceWithRevisions(Throwable e, ClassLoader loader, int highlight, String highlightPrefix) Converts an exception's stack trace to a string. Each stack trace element is annotated to include the CVS revision Id, if it contains a public static String element containing this information.Stack trace elements whose classes begin with the specified highlightPrefix are also marked, to allow easier debugging. Highlights used can be text (which will insert the string "=>" before relevent stack trace elements), or HTML (which will render the stack trace element between <b> and </b> tags.
If HTML highlighting is enabled, then the exception message is also HTML-escaped.
- Parameters:
e
- exceptionloader
- ClassLoader used to read stack trace element revision informationhighlight
- One of the HIGHLIGHT_* constants in this classhighlightPrefix
- A prefix used to determine which stack trace elements are rendered as being 'important'. (e.g. "com.randomnoun.common.
"). Multiple prefixes can be specified, if separated by commas.- Returns:
- string representation of the exception's stack trace
-
getStackDepth
Return the number of elements in the current thread's stack trace (i.e. the height of the call stack).- Returns:
- the height of the call stack
-
getClassRevision2
public static String getClassRevision2(ClassLoader loader, String className) throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException An alternate implementation ofgetClassRevision(ClassLoader, String)
, which searches the raw bytecode of the class, rather than using Java reflection. May be a bit more robust.- Parameters:
loader
- Classloader to useclassName
- Class to load- Returns:
- The CVS revision string, in the form "ver 1.234".
- Throws:
ClassNotFoundException
SecurityException
NoSuchFieldException
IllegalArgumentException
IllegalAccessException
-
getStackTraceSummary
Returns a list of all the messages contained within a exception (following the causal chain of the supplied exception). This may be more useful to and end-user, since it should not contain any references to Java class names.- Parameters:
throwable
- an exception- Returns:
- a List of Strings
-
escapeHtml
Returns the HTML-escaped form of a string. Any&
,<
,>
, and"
characters are converted to&
,<
,>
, and"
respectively.- Parameters:
string
- the string to convert- Returns:
- the HTML-escaped form of the string
-