Class ClientException

  • All Implemented Interfaces:
    Serializable

    public class ClientException
    extends RuntimeException
    Exception that contains information about a failed request containing HTTP response data.

    The original request can be retrieved through getRequest(). HTTP response status can be retrieved by getStatus(), headers using getHeaders(), while the body can be retrieved through getErrorResponse() provided Discord has supplied a body along with the error.

    It is possible to modify the behavior of a reactive sequence that has failed with this error, using operators like Mono.onErrorResume(Predicate, Function), Mono.onErrorContinue(Predicate, BiConsumer) among others. In cases where a Predicate is accepted, you can use one of the provided static methods like isStatusCode(int) to further filter by HTTP status code.

    The following example would retry a request if it has failed with an HTTP 500 error:

     client.getEventDispatcher().on(MessageCreateEvent.class)
         .map(MessageCreateEvent::getMessage)
         .filter(msg -> msg.getContent().map("!ping"::equals).orElse(false))
         .flatMap(Message::getChannel)
         .flatMap(channel -> channel.createMessage("Pong!")
             .transform(ClientException.retryOnceOnStatus(500)))
         .subscribe();
     
    While the following one would transform a not found user into an empty sequence:
     client.getUserById(Snowflake.of(userLongId))
         .onErrorResume(ClientException.isStatusCode(404), error -> Mono.empty())
         .subscribe(user -> System.out.println("Found: " + user.getUsername()));
     
    For global or Route based error handling, refer to the ResponseFunction class.
    See Also:
    Serialized Form