Class ErrorHandler

  • All Implemented Interfaces:
    java.util.function.Consumer<java.lang.Throwable>

    public class ErrorHandler
    extends java.lang.Object
    implements java.util.function.Consumer<java.lang.Throwable>
    Utility class to simplify error handling with RestActions and ErrorResponses.

    Example

    
     // Send message to user and delete it 30 seconds later, handles blocked messages in context channel.
     public void sendMessage(TextChannel context, User user, String content) {
         user.openPrivateChannel()
             .flatMap(channel -> channel.sendMessage(content))
             .delay(Duration.ofSeconds(30))
             .flatMap(Message::delete) // delete after 30 seconds
             .queue(null, new ErrorHandler()
                 .ignore(ErrorResponse.UNKNOWN_MESSAGE) // if delete fails that's fine
                 .handle(
                     ErrorResponse.CANNOT_SEND_TO_USER,  // Fallback handling for blocked messages
                     (e) -> context.sendMessage("Failed to send message, you block private messages!").queue()));
     }
     
    Since:
    4.2.0
    See Also:
    ErrorResponse, ErrorResponseException, RestAction.queue(Consumer, Consumer)
    • Constructor Summary

      Constructors 
      Constructor Description
      ErrorHandler()
      Create an ErrorHandler with RestAction.getDefaultFailure() as base consumer.
      ErrorHandler​(java.util.function.Consumer<? super java.lang.Throwable> base)
      Create an ErrorHandler with the specified consumer as base consumer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void accept​(java.lang.Throwable t)  
      <T> ErrorHandler handle​(java.lang.Class<T> clazz, java.util.function.Consumer<? super T> handler)
      Handle specific throwable types.
      <T> ErrorHandler handle​(java.lang.Class<T> clazz, java.util.function.Predicate<? super T> condition, java.util.function.Consumer<? super T> handler)
      Handle specific throwable types.
      ErrorHandler handle​(java.util.Collection<java.lang.Class<?>> clazz, java.util.function.Predicate<? super java.lang.Throwable> condition, java.util.function.Consumer<? super java.lang.Throwable> handler)
      Handle specific throwable types.
      ErrorHandler handle​(java.util.Collection<ErrorResponse> errorResponses, java.util.function.Consumer<? super ErrorResponseException> handler)
      Handle specific ErrorResponses.
      ErrorHandler handle​(java.util.function.Predicate<? super java.lang.Throwable> condition, java.util.function.Consumer<? super java.lang.Throwable> handler)
      Handle specific conditions.
      ErrorHandler handle​(ErrorResponse response, java.util.function.Consumer<? super ErrorResponseException> handler)
      Handle specific ErrorResponses.
      ErrorHandler ignore​(java.lang.Class<?> clazz, java.lang.Class<?>... classes)
      Ignore exceptions of the specified types.
      ErrorHandler ignore​(java.util.Collection<ErrorResponse> errorResponses)
      Ignore the specified set of error responses.
      ErrorHandler ignore​(java.util.function.Predicate<? super java.lang.Throwable> condition)
      Ignore exceptions on specific conditions.
      ErrorHandler ignore​(ErrorResponse ignored, ErrorResponse... errorResponses)
      Ignore the specified set of error responses.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.function.Consumer

        andThen
    • Constructor Detail

      • ErrorHandler

        public ErrorHandler()
        Create an ErrorHandler with RestAction.getDefaultFailure() as base consumer.
        If none of the provided ignore/handle cases apply, the base consumer is applied instead.
      • ErrorHandler

        public ErrorHandler​(@Nonnull
                            java.util.function.Consumer<? super java.lang.Throwable> base)
        Create an ErrorHandler with the specified consumer as base consumer.
        If none of the provided ignore/handle cases apply, the base consumer is applied instead.
        Parameters:
        base - The base Consumer
    • Method Detail

      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   ErrorResponse ignored,
                                   @Nonnull
                                   ErrorResponse... errorResponses)
        Ignore the specified set of error responses.

        Example

        
         // Creates a message with the provided content and deletes it 30 seconds later
         public static void selfDestruct(MessageChannel channel, String content) {
             channel.sendMessage(content)
                 .delay(Duration.ofSeconds(30))
                 .flatMap(Message::delete)
                 .queue(null, new ErrorHandler().ignore(UNKNOWN_MESSAGE));
         }
         
        Parameters:
        ignored - Ignored error response
        errorResponses - Additional error responses to ignore
        Returns:
        This ErrorHandler with the applied ignore cases
        Throws:
        java.lang.IllegalArgumentException - If provided with null
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   java.util.Collection<ErrorResponse> errorResponses)
        Ignore the specified set of error responses.

        Example

        
         // Creates a message with the provided content and deletes it 30 seconds later
         public static void selfDestruct(User user, String content) {
             user.openPrivateChannel()
                 .flatMap(channel -> channel.sendMessage(content))
                 .delay(Duration.ofSeconds(30))
                 .flatMap(Message::delete)
                 .queue(null, new ErrorHandler().ignore(EnumSet.of(UNKNOWN_MESSAGE, CANNOT_SEND_TO_USER)));
         }
         
        Parameters:
        errorResponses - The error responses to ignore
        Returns:
        This ErrorHandler with the applied ignore cases
        Throws:
        java.lang.IllegalArgumentException - If provided with null
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   java.lang.Class<?> clazz,
                                   @Nonnull
                                   java.lang.Class<?>... classes)
        Ignore exceptions of the specified types.

        Example

        
         // Ignore SocketTimeoutException
         public static void ban(Guild guild, String userId) {
             guild.ban(userId).queue(null, new ErrorHandler().ignore(SocketTimeoutException.class);
         }
         
        Parameters:
        clazz - The class to ignore
        classes - Additional classes to ignore
        Returns:
        This ErrorHandler with the applied ignore case
        Throws:
        java.lang.IllegalArgumentException - If provided with null
        See Also:
        SocketTimeoutException
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   java.util.function.Predicate<? super java.lang.Throwable> condition)
        Ignore exceptions on specific conditions.

        Example

        
         // Ignore all exceptions except for ErrorResponseException
         public static void ban(Guild guild, String userId) {
             guild.ban(userId).queue(null, new ErrorHandler().ignore((ex) -> !(ex instanceof ErrorResponseException));
         }
         
        Parameters:
        condition - The condition to check
        Returns:
        This ErrorHandler with the applied ignore case
        Throws:
        java.lang.IllegalArgumentException - If provided with null
        See Also:
        ErrorResponseException
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   ErrorResponse response,
                                   @Nonnull
                                   java.util.function.Consumer<? super ErrorResponseException> handler)
        Handle specific ErrorResponses.
        This will apply the specified handler to use instead of the base consumer if one of the provided ErrorResponses happens.

        Example

        
         public static void sendMessage(TextChannel context, User user, String content) {
             user.openPrivateChannel()
                 .flatMap(channel -> channel.sendMessage(content))
                 .queue(null, new ErrorHandler()
                     .handle(ErrorResponse.CANNOT_SEND_TO_USER,
                         (ex) -> context.sendMessage("Cannot send direct message, please enable direct messages from server members!").queue()));
         }
         
        Parameters:
        response - The first ErrorResponse to match
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
        Throws:
        java.lang.IllegalArgumentException - If provided with null
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   java.util.Collection<ErrorResponse> errorResponses,
                                   @Nonnull
                                   java.util.function.Consumer<? super ErrorResponseException> handler)
        Handle specific ErrorResponses.
        This will apply the specified handler to use instead of the base consumer if one of the provided ErrorResponses happens.

        Example

        
         public static void sendMessage(TextChannel context, User user, String content) {
             user.openPrivateChannel()
                 .flatMap(channel -> channel.sendMessage(content))
                 .queue(null, new ErrorHandler()
                     .handle(EnumSet.of(ErrorResponse.CANNOT_SEND_TO_USER),
                         (ex) -> context.sendMessage("Cannot send direct message, please enable direct messages from server members!").queue()));
         }
         
        Parameters:
        errorResponses - The ErrorResponses to match
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
        Throws:
        java.lang.IllegalArgumentException - If provided with null
      • handle

        @Nonnull
        public <T> ErrorHandler handle​(@Nonnull
                                       java.lang.Class<T> clazz,
                                       @Nonnull
                                       java.util.function.Consumer<? super T> handler)
        Handle specific throwable types.
        This will apply the specified handler if the throwable is of the specified type. The check is done using Class.isInstance(Object).

        Example

        
         public static void logErrorResponse(RestAction<?> action) {
             action.queue(null, new ErrorHandler()
                 .handle(ErrorResponseException.class,
                     (ex) -> System.out.println(ex.getErrorResponse())));
         }
         
        Type Parameters:
        T - The type
        Parameters:
        clazz - The throwable type
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
      • handle

        @Nonnull
        public <T> ErrorHandler handle​(@Nonnull
                                       java.lang.Class<T> clazz,
                                       @Nonnull
                                       java.util.function.Predicate<? super T> condition,
                                       @Nonnull
                                       java.util.function.Consumer<? super T> handler)
        Handle specific throwable types.
        This will apply the specified handler if the throwable is of the specified type. The check is done using Class.isInstance(Object).

        Example

        
         public static void logErrorResponse(RestAction<?> action) {
             action.queue(null, new ErrorHandler()
                 .handle(ErrorResponseException.class,
                     ErrorResponseException::isServerError,
                     (ex) -> System.out.println(ex.getErrorCode() + ": " + ex.getMeaning())));
         }
         
        Type Parameters:
        T - The type
        Parameters:
        clazz - The throwable type
        condition - Additional condition that must apply to use this handler
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   java.util.Collection<java.lang.Class<?>> clazz,
                                   @Nullable
                                   java.util.function.Predicate<? super java.lang.Throwable> condition,
                                   @Nonnull
                                   java.util.function.Consumer<? super java.lang.Throwable> handler)
        Handle specific throwable types.
        This will apply the specified handler if the throwable is of the specified type. The check is done using Class.isInstance(Object).

        Example

        
         public static void logErrorResponse(RestAction<?> action) {
             action.queue(null, new ErrorHandler()
                 .handle(Arrays.asList(Throwable.class),
                     (ex) -> ex instanceof Error,
                     (ex) -> ex.printStackTrace()));
         }
         
        Parameters:
        clazz - The throwable types
        condition - Additional condition that must apply to use this handler, or null to apply no additional condition
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   java.util.function.Predicate<? super java.lang.Throwable> condition,
                                   @Nonnull
                                   java.util.function.Consumer<? super java.lang.Throwable> handler)
        Handle specific conditions.

        Example

        
         public static void logErrorResponse(RestAction<?> action) {
             action.queue(null, new ErrorHandler()
                 .handle(
                     (ex) -> !(ex instanceof ErrorResponseException),
                     Throwable::printStackTrace));
         }
         
        Parameters:
        condition - Condition that must apply to use this handler
        handler - The alternative handler
        Returns:
        This ErrorHandler with the applied handler
      • accept

        public void accept​(java.lang.Throwable t)
        Specified by:
        accept in interface java.util.function.Consumer<java.lang.Throwable>