Class ExceptionLogger


  • public class ExceptionLogger
    extends java.lang.Object
    This class contains some helpers to log exceptions.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.function.Function<java.lang.Throwable,​T> get​(java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
      Returns a function that can be used in the CompletableFuture.exceptionally(Function) method.
      static <T> java.util.function.Function<java.lang.Throwable,​T> get​(java.util.function.Predicate<java.lang.Throwable> logFilter, java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
      Returns a function that can be used in the CompletableFuture.exceptionally(Function) method.
      static java.util.function.Consumer<java.lang.Throwable> getConsumer​(java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
      Returns a consumer that can for example be used in the TextChannel.typeContinuously(Consumer) method.
      static java.util.function.Consumer<java.lang.Throwable> getConsumer​(java.util.function.Predicate<java.lang.Throwable> logFilter, java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
      Returns a consumer that can for example be used in the TextChannel.typeContinuously(Consumer) method.
      static java.lang.Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
      Returns an uncaught exception handler that can be used in the Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler) and Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler) methods.
      static java.lang.Throwable unwrapThrowable​(java.lang.Throwable throwable)
      Unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions and returns the actual cause, or the given argument itself if it is not one of the listed exceptions types.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getConsumer

        @SafeVarargs
        public static java.util.function.Consumer<java.lang.Throwable> getConsumer​(java.util.function.Predicate<java.lang.Throwable> logFilter,
                                                                                   java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
        Returns a consumer that can for example be used in the TextChannel.typeContinuously(Consumer) method. It unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions first, and then adds a fresh CompletionException as wrapper with the stacktrace of the caller of this method and logs it afterwards. The rewrapped exception is only logged if the given logFilter predicate allows the exception and the class of it is not in the ignoredThrowableTypes.
        Parameters:
        logFilter - The predicate the unwrapped exception is tested against.
        ignoredThrowableTypes - The throwable types that should never be logged.
        Returns:
        A consumer which logs the given throwable.
      • getConsumer

        @SafeVarargs
        public static java.util.function.Consumer<java.lang.Throwable> getConsumer​(java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
        Returns a consumer that can for example be used in the TextChannel.typeContinuously(Consumer) method. It unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions first, and then adds a fresh CompletionException as wrapper with the stacktrace of the caller of this method and logs it afterwards. The rewrapped exception is only logged if it is not in the ignoredThrowableTypes.
        Parameters:
        ignoredThrowableTypes - The throwable types that should never be logged.
        Returns:
        A consumer which logs the given throwable.
      • get

        @SafeVarargs
        public static <T> java.util.function.Function<java.lang.Throwable,​T> get​(java.util.function.Predicate<java.lang.Throwable> logFilter,
                                                                                       java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
        Returns a function that can be used in the CompletableFuture.exceptionally(Function) method. It unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions first, and then adds a fresh CompletionException as wrapper with the stacktrace of the caller of this method and logs it afterwards. The rewrapped exception is only logged if the given logFilter predicate allows the exception and the class of it is not in the ignoredThrowableTypes.
        Type Parameters:
        T - The return type of the function.
        Parameters:
        logFilter - The predicate the unwrapped exception is tested against.
        ignoredThrowableTypes - The throwable types that should never be logged.
        Returns:
        A function which logs the given throwable and returns null.
      • get

        @SafeVarargs
        public static <T> java.util.function.Function<java.lang.Throwable,​T> get​(java.lang.Class<? extends java.lang.Throwable>... ignoredThrowableTypes)
        Returns a function that can be used in the CompletableFuture.exceptionally(Function) method. It unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions first, and then adds a fresh CompletionException as wrapper with the stacktrace of the caller of this method and logs it afterwards. The rewrapped exception is only logged if it is not in the ignoredThrowableTypes.
        Type Parameters:
        T - The return type of the function.
        Parameters:
        ignoredThrowableTypes - The throwable types that should never be logged.
        Returns:
        A function which logs the given throwable and returns null.
      • getUncaughtExceptionHandler

        public static java.lang.Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
        Returns an uncaught exception handler that can be used in the Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler) and Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler) methods. It unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions first and logs it afterwards, including the thread name.
        Returns:
        An Thread.UncaughtExceptionHandler which logs the given throwable.
      • unwrapThrowable

        public static java.lang.Throwable unwrapThrowable​(java.lang.Throwable throwable)
        Unwraps CompletionExceptions, InvocationTargetExceptions and ExecutionExceptions and returns the actual cause, or the given argument itself if it is not one of the listed exceptions types. If the bottom-most exception to unwrap does not have a cause, it is returned itself instead.
        Parameters:
        throwable - The throwable to unwrap.
        Returns:
        The unwrapped throwable.