Package net.dv8tion.jda.api.exceptions
Class ErrorHandler
java.lang.Object
net.dv8tion.jda.api.exceptions.ErrorHandler
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:
- 
Constructor SummaryConstructorsConstructorDescriptionCreate an ErrorHandler withRestAction.getDefaultFailure()as base consumer.ErrorHandler(Consumer<? super Throwable> base) Create an ErrorHandler with the specified consumer as base consumer.
- 
Method SummaryModifier and TypeMethodDescriptionvoid<T> ErrorHandlerHandle specific throwable types.<T> ErrorHandlerHandle specific throwable types.handle(Collection<Class<?>> clazz, Predicate<? super Throwable> condition, Consumer<? super Throwable> handler) Handle specific throwable types.handle(Collection<ErrorResponse> errorResponses, Consumer<? super ErrorResponseException> handler) Handle specificErrorResponses.Handle specific conditions.handle(ErrorResponse response, Consumer<? super ErrorResponseException> handler) Handle specificErrorResponses.Ignore exceptions of the specified types.ignore(Collection<ErrorResponse> errorResponses) Ignore the specified set of error responses.Ignore exceptions on specific conditions.ignore(ErrorResponse ignored, ErrorResponse... errorResponses) Ignore the specified set of error responses.
- 
Constructor Details- 
ErrorHandlerpublic ErrorHandler()Create an ErrorHandler withRestAction.getDefaultFailure()as base consumer.
 If none of the provided ignore/handle cases apply, the base consumer is applied instead.
- 
ErrorHandlerCreate 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 Details- 
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
 
- 
ignoreIgnore 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
 
- 
ignoreIgnore 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:
 
- 
ignoreIgnore 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:
 
- 
handle@Nonnull public ErrorHandler handle(@Nonnull ErrorResponse response, @Nonnull Consumer<? super ErrorResponseException> handler) Handle specificErrorResponses.
 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- ErrorResponseto 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 specificErrorResponses.
 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- ErrorResponsesto 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 usingClass.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 usingClass.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 usingClass.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
 
- 
accept
 
-