Class ExceptionHelper


  • public final class ExceptionHelper
    extends Object
    Helper methods dealing with exceptions.
    Author:
    harald
    • 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 for
        first - true if finds the first, else the last in chain
        headOfChain - 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 chain
        headOfChain - the head of chained exceptions, may be null
        types - the exception classes to search for
        Returns:
        the exception, null if none
      • extractTemporaryException

        public static TentackleRuntimeException extractTemporaryException​(boolean first,
                                                                          Throwable ex)
        Extracts the first/last TentackleRuntimeException marked as temporay in the throwable chain.
        Parameters:
        first - true if finds the first, else the last in chain
        ex - 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 chain
        headOfChain - the head of chained exceptions, may be null
        handlers - 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