Class ErrorHandler

  • All Implemented Interfaces:
    Consumer<Throwable>

    public class ErrorHandler
    extends Object
    implements Consumer<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 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
                            Consumer<? super 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:
        IllegalArgumentException - If provided with null
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   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:
        IllegalArgumentException - If provided with null
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   Class<?> clazz,
                                   @Nonnull
                                   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:
        IllegalArgumentException - If provided with null
        See Also:
        SocketTimeoutException
      • ignore

        @Nonnull
        public ErrorHandler ignore​(@Nonnull
                                   Predicate<? super 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:
        IllegalArgumentException - If provided with null
        See Also:
        ErrorResponseException
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   ErrorResponse response,
                                   @Nonnull
                                   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:
        IllegalArgumentException - If provided with null
      • handle

        @Nonnull
        public ErrorHandler handle​(@Nonnull
                                   Collection<ErrorResponse> errorResponses,
                                   @Nonnull
                                   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:
        IllegalArgumentException - If provided with null
      • handle

        @Nonnull
        public <T> ErrorHandler handle​(@Nonnull
                                       Class<T> clazz,
                                       @Nonnull
                                       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
                                       Class<T> clazz,
                                       @Nonnull
                                       Predicate<? super T> condition,
                                       @Nonnull
                                       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
                                   Collection<Class<?>> clazz,
                                   @Nullable
                                   Predicate<? super Throwable> condition,
                                   @Nonnull
                                   Consumer<? super 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
                                   Predicate<? super Throwable> condition,
                                   @Nonnull
                                   Consumer<? super 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