Class ExceptionTools


  • public class ExceptionTools
    extends java.lang.Object
    Contains common helper methods for dealing with exceptions.
    • Constructor Detail

      • ExceptionTools

        public ExceptionTools()
    • Method Detail

      • rethrowErrors

        public static void rethrowErrors​(java.lang.Throwable throwable)
        Sometimes it is necessary to catch Throwable instances for logging or reporting purposes. However, unless for specific known cases, Error instances should not be blocked from propagating, so rethrow them.
        Parameters:
        throwable - The Throwable to check, it is rethrown if it is an Error
      • wrapUnfriendlyExceptions

        public static FriendlyException wrapUnfriendlyExceptions​(java.lang.String message,
                                                                 FriendlyException.Severity severity,
                                                                 java.lang.Throwable throwable)
        If the exception is not a FriendlyException, wrap with a FriendlyException with the given message
        Parameters:
        message - Message of the new FriendlyException if needed
        severity - Severity of the new FriendlyException
        throwable - The exception to potentially wrap
        Returns:
        Original or wrapped exception
      • wrapUnfriendlyExceptions

        public static java.lang.RuntimeException wrapUnfriendlyExceptions​(java.lang.Throwable throwable)
        If the exception is not a FriendlyException, wrap with a RuntimeException
        Parameters:
        throwable - The exception to potentially wrap
        Returns:
        Original or wrapped exception
      • toRuntimeException

        public static java.lang.RuntimeException toRuntimeException​(java.lang.Exception e)
      • findDeepException

        public static <T extends java.lang.Throwable> T findDeepException​(java.lang.Throwable throwable,
                                                                          java.lang.Class<T> klass)
        Finds the first exception which is an instance of the specified class from the throwable cause chain.
        Type Parameters:
        T - The throwable class to scan for.
        Parameters:
        throwable - Throwable to scan.
        klass - The throwable class to scan for.
        Returns:
        The first exception in the cause chain (including itself) which is an instance of the specified class.
      • keepInterrupted

        public static void keepInterrupted​(java.lang.Throwable throwable)
        Makes sure thread is set to interrupted state when the throwable is an InterruptedException
        Parameters:
        throwable - Throwable to check
      • log

        public static void log​(org.slf4j.Logger log,
                               FriendlyException exception,
                               java.lang.Object context)
        Log a FriendlyException appropriately according to its severity.
        Parameters:
        log - Logger instance to log it to
        exception - The exception itself
        context - An object that is included in the log
      • throwWithDebugInfo

        public static java.lang.RuntimeException throwWithDebugInfo​(org.slf4j.Logger log,
                                                                    java.lang.Throwable cause,
                                                                    java.lang.String message,
                                                                    java.lang.String name,
                                                                    java.lang.String value)
      • encodeException

        public static void encodeException​(java.io.DataOutput output,
                                           FriendlyException exception)
                                    throws java.io.IOException
        Encode an exception to an output stream
        Parameters:
        output - Data output
        exception - Exception to encode
        Throws:
        java.io.IOException - On IO error
      • closeWithWarnings

        public static void closeWithWarnings​(java.lang.AutoCloseable closeable)
        Closes the specified closeable object. In case that throws an error, logs the error with WARN level, but does not rethrow.
        Parameters:
        closeable - Object to close.
      • decodeException

        public static FriendlyException decodeException​(java.io.DataInput input)
                                                 throws java.io.IOException
        Decode an exception from an input stream
        Parameters:
        input - Data input
        Returns:
        Decoded exception
        Throws:
        java.io.IOException - On IO error