Package discord4j.rest.http.client
Class ClientException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
discord4j.rest.http.client.ClientException
- All Implemented Interfaces:
Serializable
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:
-
Constructor Summary
ConstructorDescriptionClientException
(ClientRequest request, HttpClientResponse response, ErrorResponse errorResponse) Create a newClientException
with the given HTTP request and response details. -
Method Summary
Modifier and TypeMethodDescriptionemptyOnStatus
(int code) Transformation function that can be used within an operator such asMono.transform(Function)
orMono.transformDeferred(Function)
to turn an error sequence matching the given HTTP status code, into an empty sequence, effectively suppressing the original error.Return the HTTP response body in the form of a DiscordErrorResponse
, if present.Return theHttpHeaders
from the error response.Return theClientRequest
encapsulating a Discord API request.Return theHttpClientResponse
encapsulating a low-level Discord API response.Return theHttpResponseStatus
with information related to the HTTP error.isStatusCode
(int code) Predicate
helper to further classify aClientException
depending on the underlying HTTP status code.isStatusCode
(Integer... codes) Predicate
helper to further classify aClientException
depending on the underlying HTTP status code.retryOnceOnStatus
(int code) Transformation function that can be used within an operator such asMono.transform(Function)
orMono.transformDeferred(Function)
to apply a retrying strategy in case of an error matching the given HTTP status code.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
ClientException
public ClientException(ClientRequest request, HttpClientResponse response, @Nullable ErrorResponse errorResponse) Create a newClientException
with the given HTTP request and response details.- Parameters:
request
- the originalClientRequest
that caused this exceptionresponse
- the failingHttpClientResponse
errorResponse
- the response body converted to anErrorResponse
, ornull
if not available
-
-
Method Details
-
getRequest
Return theClientRequest
encapsulating a Discord API request.- Returns:
- the request that caused this exception
-
getResponse
Return theHttpClientResponse
encapsulating a low-level Discord API response.- Returns:
- the low-level response that caused this exception
-
getStatus
Return theHttpResponseStatus
with information related to the HTTP error. The actual status code can be obtained throughHttpResponseStatus.code()
.- Returns:
- the HTTP error associated to this exception
-
getHeaders
Return theHttpHeaders
from the error response. To get request headers refer togetRequest()
and thenClientRequest.getHeaders()
.- Returns:
- the HTTP response headers
-
getErrorResponse
Return the HTTP response body in the form of a DiscordErrorResponse
, 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.- Returns:
- the Discord error response, if present
-
isStatusCode
Predicate
helper to further classify aClientException
depending on the underlying HTTP status code.- Parameters:
code
- the status code for which thisPredicate
should returntrue
- Returns:
- a
Predicate
that returnstrue
if the givenThrowable
is aClientException
containing the given HTTP status code
-
isStatusCode
Predicate
helper to further classify aClientException
depending on the underlying HTTP status code.- Parameters:
codes
- the status codes for which thisPredicate
should returntrue
- Returns:
- a
Predicate
that returnstrue
if the givenThrowable
is aClientException
containing the given HTTP status code
-
emptyOnStatus
Transformation function that can be used within an operator such asMono.transform(Function)
orMono.transformDeferred(Function)
to turn an error sequence matching the given HTTP status code, into an empty sequence, effectively suppressing the original error.- Type Parameters:
T
- the type of the response- Parameters:
code
- the status code that should be transformed into empty sequences- Returns:
- a transformation function that converts error sequences into empty sequences
-
retryOnceOnStatus
Transformation function that can be used within an operator such asMono.transform(Function)
orMono.transformDeferred(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.- Type Parameters:
T
- the type of the response- Parameters:
code
- the status code that should be retried- Returns:
- a transformation function that retries error sequences
-