Package net.dv8tion.jda.api.exceptions
Class ErrorHandler
- java.lang.Object
-
- net.dv8tion.jda.api.exceptions.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 withRestActionsandErrorResponses.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 withRestAction.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 voidaccept(java.lang.Throwable t)<T> ErrorHandlerhandle(java.lang.Class<T> clazz, java.util.function.Consumer<? super T> handler)Handle specific throwable types.<T> ErrorHandlerhandle(java.lang.Class<T> clazz, java.util.function.Predicate<? super T> condition, java.util.function.Consumer<? super T> handler)Handle specific throwable types.ErrorHandlerhandle(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.ErrorHandlerhandle(java.util.Collection<ErrorResponse> errorResponses, java.util.function.Consumer<? super ErrorResponseException> handler)Handle specificErrorResponses.ErrorHandlerhandle(java.util.function.Predicate<? super java.lang.Throwable> condition, java.util.function.Consumer<? super java.lang.Throwable> handler)Handle specific conditions.ErrorHandlerhandle(ErrorResponse response, java.util.function.Consumer<? super ErrorResponseException> handler)Handle specificErrorResponses.ErrorHandlerignore(java.lang.Class<?> clazz, java.lang.Class<?>... classes)Ignore exceptions of the specified types.ErrorHandlerignore(java.util.Collection<ErrorResponse> errorResponses)Ignore the specified set of error responses.ErrorHandlerignore(java.util.function.Predicate<? super java.lang.Throwable> condition)Ignore exceptions on specific conditions.ErrorHandlerignore(ErrorResponse ignored, ErrorResponse... errorResponses)Ignore the specified set of error responses.
-
-
-
Constructor Detail
-
ErrorHandler
public ErrorHandler()
Create an ErrorHandler withRestAction.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 baseConsumer
-
-
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 responseerrorResponses- 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 ignoreclasses- 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 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 firstErrorResponseto matchhandler- 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 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- TheErrorResponsesto matchhandler- 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 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 typehandler- 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 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 typecondition- Additional condition that must apply to use this handlerhandler- 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 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 typescondition- Additional condition that must apply to use this handler, or null to apply no additional conditionhandler- 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 handlerhandler- The alternative handler- Returns:
- This ErrorHandler with the applied handler
-
accept
public void accept(java.lang.Throwable t)
- Specified by:
acceptin interfacejava.util.function.Consumer<java.lang.Throwable>
-
-