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
ConstructorsConstructorDescriptionClientException(ClientRequest request, HttpClientResponse response, ErrorResponse errorResponse) Create a newClientExceptionwith 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 theHttpHeadersfrom the error response.Return theClientRequestencapsulating a Discord API request.Return theHttpClientResponseencapsulating a low-level Discord API response.Return theHttpResponseStatuswith information related to the HTTP error.isStatusCode(int code) Predicatehelper to further classify aClientExceptiondepending on the underlying HTTP status code.isStatusCode(Integer... codes) Predicatehelper to further classify aClientExceptiondepending 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 newClientExceptionwith the given HTTP request and response details.- Parameters:
request- the originalClientRequestthat caused this exceptionresponse- the failingHttpClientResponseerrorResponse- the response body converted to anErrorResponse, ornullif not available
-
-
Method Details
-
getRequest
Return theClientRequestencapsulating a Discord API request.- Returns:
- the request that caused this exception
-
getResponse
Return theHttpClientResponseencapsulating a low-level Discord API response.- Returns:
- the low-level response that caused this exception
-
getStatus
Return theHttpResponseStatuswith 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 theHttpHeadersfrom 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.ErrorResponseis 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
Predicatehelper to further classify aClientExceptiondepending on the underlying HTTP status code.- Parameters:
code- the status code for which thisPredicateshould returntrue- Returns:
- a
Predicatethat returnstrueif the givenThrowableis aClientExceptioncontaining the given HTTP status code
-
isStatusCode
Predicatehelper to further classify aClientExceptiondepending on the underlying HTTP status code.- Parameters:
codes- the status codes for which thisPredicateshould returntrue- Returns:
- a
Predicatethat returnstrueif the givenThrowableis aClientExceptioncontaining 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
-