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
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 bygetStatus(), headers usinggetHeaders(), while the body can be retrieved throughgetErrorResponse()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 aPredicateis accepted, you can use one of the provided static methods likeisStatusCode(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 orRoutebased error handling, refer to theResponseFunctionclass.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ClientException(ClientRequest request, HttpClientResponse response, ErrorResponse errorResponse)Create a newClientExceptionwith the given HTTP request and response details.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Function<Mono<T>,Publisher<T>>emptyOnStatus(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.Optional<ErrorResponse>getErrorResponse()Return the HTTP response body in the form of a DiscordErrorResponse, if present.HttpHeadersgetHeaders()Return theHttpHeadersfrom the error response.ClientRequestgetRequest()Return theClientRequestencapsulating a Discord API request.HttpClientResponsegetResponse()Return theHttpClientResponseencapsulating a low-level Discord API response.HttpResponseStatusgetStatus()Return theHttpResponseStatuswith information related to the HTTP error.static Predicate<RetryContext<?>>isRetryContextStatusCode(int code)Predicatehelper to further classify aClientException, while creating aRetryfactory, depending on the underlying HTTP status code.static Predicate<RetryContext<?>>isRetryContextStatusCode(Integer... codes)Predicatehelper to further classify aClientException, while creating aRetryfactory, depending on the underlying HTTP status code.static Predicate<Throwable>isStatusCode(int code)Predicatehelper to further classify aClientExceptiondepending on the underlying HTTP status code.static Predicate<Throwable>isStatusCode(Integer... codes)Predicatehelper to further classify aClientExceptiondepending 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 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 Detail
-
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 Detail
-
getRequest
public ClientRequest getRequest()
Return theClientRequestencapsulating a Discord API request.- Returns:
- the request that caused this exception
-
getResponse
public HttpClientResponse getResponse()
Return theHttpClientResponseencapsulating a low-level Discord API response.- Returns:
- the low-level response that caused this exception
-
getStatus
public HttpResponseStatus 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
public HttpHeaders getHeaders()
Return theHttpHeadersfrom the error response. To get request headers refer togetRequest()and thenClientRequest.getHeaders().- Returns:
- the HTTP response headers
-
getErrorResponse
public Optional<ErrorResponse> 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
public static Predicate<Throwable> isStatusCode(int code)
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
public static Predicate<Throwable> isStatusCode(Integer... codes)
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
-
isRetryContextStatusCode
public static Predicate<RetryContext<?>> isRetryContextStatusCode(int code)
Predicatehelper to further classify aClientException, while creating aRetryfactory, depending on the underlying HTTP status code. ARetryfactory can be created through methods likeRetry.onlyIf(Predicate)where this method can be used as argument.- Parameters:
code- the status code for which thisPredicateshould returntrue- Returns:
- a
Predicatethat returnstrueif the givenRetryContextexception is aClientExceptioncontaining the given HTTP status code
-
isRetryContextStatusCode
public static Predicate<RetryContext<?>> isRetryContextStatusCode(Integer... codes)
Predicatehelper to further classify aClientException, while creating aRetryfactory, depending on the underlying HTTP status code. ARetryfactory can be created through methods likeRetry.onlyIf(Predicate)where this method can be used as argument.- Parameters:
codes- the status codes for which thisPredicateshould returntrue- Returns:
- a
Predicatethat returnstrueif the givenThrowableis aClientExceptioncontaining the given HTTP status code
-
emptyOnStatus
public static <T> Function<Mono<T>,Publisher<T>> emptyOnStatus(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.- 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
public static <T> Function<Mono<T>,Publisher<T>> 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. 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
-
-