Class ExceptionUtilsAksw


  • public class ExceptionUtilsAksw
    extends Object
    Utils for dealing with typical situtations w.r.t. Exceptions.
    Author:
    raven
    • Constructor Detail

      • ExceptionUtilsAksw

        public ExceptionUtilsAksw()
    • Method Detail

      • rethrowUnless

        @SafeVarargs
        public static <T extends Throwable> void rethrowUnless​(T t,
                                                               java.util.function.Predicate<? super T>... predicates)
        Utility function to rethrow an exception unless a condition is met; in that case it is silently swallowed.
         rethrowUnless(e, ExceptionUtils::isBrokenPipeException, Foo:isIoException)
         
        Type Parameters:
        T -
        Parameters:
        t -
        predicates -
      • isBrokenPipeException

        public static boolean isBrokenPipeException​(Throwable t)
      • isConnectionRefusedException

        public static boolean isConnectionRefusedException​(Throwable t)
      • isUnknownHostException

        public static boolean isUnknownHostException​(Throwable t)
      • isRootCauseInstanceOf

        public static java.util.function.Predicate<Throwable> isRootCauseInstanceOf​(Class<?> superClass)
      • isRootCauseInstanceOf

        public static boolean isRootCauseInstanceOf​(Throwable t,
                                                    Class<?> superClass)
      • rethrowIfNotBrokenPipe

        public static void rethrowIfNotBrokenPipe​(Throwable t)
      • forwardRootCauseUnless

        @SafeVarargs
        public static <T extends Throwable> void forwardRootCauseUnless​(T t,
                                                                        java.util.function.Consumer<? super Throwable> handler,
                                                                        java.util.function.Predicate<? super T>... predicates)
      • forwardRootCauseMessageUnless

        @SafeVarargs
        public static <T extends Throwable> void forwardRootCauseMessageUnless​(T t,
                                                                               java.util.function.Consumer<? super String> handler,
                                                                               java.util.function.Predicate<? super T>... predicates)
        Forward the root cause message of the throwable 't' to 'handler' unless any of the 'predicates' evaluates to true. Useful to e.g. silently suppress broken pipe exceptions in shell scripting pipes. The handler is typically something like logger::warn or logger::error.
      • unwrap

        @SafeVarargs
        public static Optional<Throwable> unwrap​(Throwable given,
                                                 Class<? extends Throwable>... priorities)
        Check the stack trace for whether it contains an instance of any of the given exceptions classes and return that instance. Arguments are checked in order.
      • isClosedChannelException

        public static boolean isClosedChannelException​(Throwable t)
      • rethrowUnlessRootCauseMatches

        @SafeVarargs
        public static void rethrowUnlessRootCauseMatches​(Throwable e,
                                                         java.util.function.Consumer<? super java.util.function.Predicate<? super Throwable>> firstMatchingConditionCallback,
                                                         java.util.function.Predicate<? super Throwable>... conditions)
      • rethrowUnlessRootCauseMatches

        @SafeVarargs
        public static void rethrowUnlessRootCauseMatches​(Throwable e,
                                                         java.util.function.Predicate<? super Throwable>... conditions)