public class ClientException extends RuntimeException
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.| Constructor and Description |
|---|
ClientException(ClientRequest request,
HttpClientResponse response,
ErrorResponse errorResponse)
Create a new
ClientException with the given HTTP request and response details. |
| Modifier and Type | Method and Description |
|---|---|
static <T> Function<Mono<T>,Publisher<T>> |
emptyOnStatus(int code)
Transformation function that can be used within an operator such as
Mono.transform(Function) or
Mono.compose(Function) to turn an error sequence matching the given HTTP status code, into an empty
sequence, effectively suppressing the original error. |
ErrorResponse |
getErrorResponse()
Return the HTTP response body in the form of a Discord
ErrorResponse, if present. |
HttpHeaders |
getHeaders()
Return the
HttpHeaders from the error response. |
ClientRequest |
getRequest()
Return the
ClientRequest encapsulating a Discord API request. |
HttpResponseStatus |
getStatus()
Return the
HttpResponseStatus with information related to the HTTP error. |
static Predicate<RetryContext<?>> |
isRetryContextStatusCode(int code)
Predicate helper to further classify a ClientException, while creating a Retry
factory, depending on the underlying HTTP status code. |
static Predicate<RetryContext<?>> |
isRetryContextStatusCode(Integer... codes)
Predicate helper to further classify a ClientException, while creating a Retry
factory, depending on the underlying HTTP status code. |
static Predicate<Throwable> |
isStatusCode(int code)
Predicate helper to further classify a ClientException depending on the underlying HTTP status
code. |
static Predicate<Throwable> |
isStatusCode(Integer... codes)
Predicate helper to further classify a ClientException depending on the underlying HTTP status
code. |
static <T> Function<Mono<T>,Publisher<T>> |
retryOnceOnStatus(int code)
Transformation function that can be used within an operator such as
Mono.transform(Function) or
Mono.compose(Function) to apply a retrying strategy in case of an error matching the given HTTP status
code. |
String |
toString() |
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTracepublic ClientException(ClientRequest request, HttpClientResponse response, @Nullable ErrorResponse errorResponse)
ClientException with the given HTTP request and response details.request - the original ClientRequest that caused this exceptionresponse - the failing HttpClientResponseerrorResponse - the response body converted to an ErrorResponse, or null if not availablepublic ClientRequest getRequest()
ClientRequest encapsulating a Discord API request.public HttpResponseStatus getStatus()
HttpResponseStatus with information related to the HTTP error. The actual status code can be
obtained through HttpResponseStatus.code().public HttpHeaders getHeaders()
HttpHeaders from the error response. To get request headers refer to
getRequest() and then ClientRequest.getHeaders().@Nullable public ErrorResponse getErrorResponse()
ErrorResponse, if present. ErrorResponse
is a common object that contains an internal status code and messages, and could be used to further clarify
the source of the API error.public static Predicate<Throwable> isStatusCode(int code)
Predicate helper to further classify a ClientException depending on the underlying HTTP status
code.code - the status code for which this Predicate should return truePredicate that returns true if the given Throwable is a ClientException
containing the given HTTP status code.public static Predicate<Throwable> isStatusCode(Integer... codes)
Predicate helper to further classify a ClientException depending on the underlying HTTP status
code.codes - the status codes for which this Predicate should return truePredicate that returns true if the given Throwable is a ClientException
containing the given HTTP status code.public static Predicate<RetryContext<?>> isRetryContextStatusCode(int code)
Predicate helper to further classify a ClientException, while creating a Retry
factory, depending on the underlying HTTP status code. A Retry factory can be created through methods
like Retry.onlyIf(Predicate) where this method can be used as argument.code - the status code for which this Predicate should return truePredicate that returns true if the given RetryContext exception is a
ClientException containing the given HTTP status code.public static Predicate<RetryContext<?>> isRetryContextStatusCode(Integer... codes)
Predicate helper to further classify a ClientException, while creating a Retry
factory, depending on the underlying HTTP status code. A Retry factory can be created through methods
like Retry.onlyIf(Predicate) where this method can be used as argument.codes - the status codes for which this Predicate should return truePredicate that returns true if the given Throwable is a ClientException
containing the given HTTP status code.public static <T> Function<Mono<T>,Publisher<T>> emptyOnStatus(int code)
Mono.transform(Function) or
Mono.compose(Function) to turn an error sequence matching the given HTTP status code, into an empty
sequence, effectively suppressing the original error.T - the type of the responsecode - the status code that should be transformed into empty sequencespublic static <T> Function<Mono<T>,Publisher<T>> retryOnceOnStatus(int code)
Mono.transform(Function) or
Mono.compose(Function) to apply a retrying strategy in case of an error matching the given HTTP status
code. The provided retrying strategy will wait 1 second, and then retry once.T - the type of the responsecode - the status code that should be retried