- java.lang.Object
-
- org.tentackle.common.ExceptionHelper
-
public final class ExceptionHelper extends Object
Helper methods dealing with exceptions.- Author:
- harald
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExceptionHelper.Handler<T extends Throwable>Exception handler.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringconcatenateMessages(Throwable headOfChain)Concatenates the messages of an exception chain.static ThrowableextractException(boolean first, Throwable headOfChain, Class<? extends Throwable>... types)Extracts any of the given exception types from an exception chain.static <T extends Throwable>
TextractException(Class<T> type, boolean first, Throwable headOfChain)Extracts the given exception type from an exception chain.static TentackleRuntimeExceptionextractTemporaryException(boolean first, Throwable ex)Extracts the first/lastTentackleRuntimeExceptionmarked as temporay in the throwable chain.static StackTraceElement[]filterStackTrace(StackTraceElement[] trace)Filters a given stacktrace skipping all noisy elements without relevant information for the developer.static StringgetMessage(Throwable ex)Determines the first exception in the chain with a valid message.static StringgetStackTraceAsString(Throwable cause)Returns the stacktrace of a given exception as a string.static booleanhandleException(boolean first, Throwable headOfChain, ExceptionHelper.Handler<? extends Throwable>... handlers)Extracts any of the given exception types from an exception chain.static booleanisClassValuableForStackTrace(String className)Checks if the class is a valuable information within a stracktrace.
-
-
-
Method Detail
-
extractException
public static <T extends Throwable> T extractException(Class<T> type, boolean first, Throwable headOfChain)
Extracts the given exception type from an exception chain.- Type Parameters:
T- the exception type to search for- Parameters:
type- the exception class to search forfirst- true if finds the first, else the last in chainheadOfChain- the head of chained exceptions, may be null- Returns:
- the exception, null if none
-
extractException
public static Throwable extractException(boolean first, Throwable headOfChain, Class<? extends Throwable>... types)
Extracts any of the given exception types from an exception chain.- Parameters:
first- true if finds the first, else the last in chainheadOfChain- the head of chained exceptions, may be nulltypes- the exception classes to search for- Returns:
- the exception, null if none
-
extractTemporaryException
public static TentackleRuntimeException extractTemporaryException(boolean first, Throwable ex)
Extracts the first/lastTentackleRuntimeExceptionmarked as temporay in the throwable chain.- Parameters:
first- true if finds the first, else the last in chainex- the head of chained exceptions, may be null- Returns:
- the temporary exception, null if not found
-
getMessage
public static String getMessage(Throwable ex)
Determines the first exception in the chain with a valid message.- Parameters:
ex- the head of chained exceptions, may be null- Returns:
- the message, null if none
-
handleException
public static boolean handleException(boolean first, Throwable headOfChain, ExceptionHelper.Handler<? extends Throwable>... handlers)Extracts any of the given exception types from an exception chain.- Parameters:
first- true if handle the first, else the last in chainheadOfChain- the head of chained exceptions, may be nullhandlers- the exception handlers- Returns:
- true if handled, false if no matching exception found in chain
-
concatenateMessages
public static String concatenateMessages(Throwable headOfChain)
Concatenates the messages of an exception chain.- Parameters:
headOfChain- the head of chained exceptions, may be null- Returns:
- the message string
-
getStackTraceAsString
public static String getStackTraceAsString(Throwable cause)
Returns the stacktrace of a given exception as a string.- Parameters:
cause- the exception- Returns:
- the stacktrace as a printable string
-
isClassValuableForStackTrace
public static boolean isClassValuableForStackTrace(String className)
Checks if the class is a valuable information within a stracktrace.- Parameters:
className- the classname- Returns:
- true if valuable, else noise
-
filterStackTrace
public static StackTraceElement[] filterStackTrace(StackTraceElement[] trace)
Filters a given stacktrace skipping all noisy elements without relevant information for the developer.- Parameters:
trace- the stacktrace- Returns:
- the reduced stacktrace
-
-