@@ -650,38 +650,39 @@ <T, P extends Publisher<T>> RequestHeadersSpec<?> body(P publisher,
650
650
interface ResponseSpec {
651
651
652
652
/**
653
- * Register a custom error function that gets invoked when the given {@link HttpStatus}
654
- * predicate applies. Whatever exception is returned from the function (possibly using
655
- * {@link ClientResponse#createException()}) will also be returned as error signal
656
- * from {@link #bodyToMono(Class)} and {@link #bodyToFlux(Class)}.
657
- * <p>By default, an error handler is registered that returns a
658
- * {@link WebClientResponseException} when the response status code is 4xx or 5xx.
659
- * To override this default (and return a non-error response from {@code bodyOn*}), register
660
- * an exception function that returns an {@linkplain Mono#empty() empty} mono.
661
- * <p><strong>NOTE:</strong> if the response is expected to have content,
662
- * the exceptionFunction should consume it. If not, the content will be
663
- * automatically drained to ensure resources are released.
664
- * @param statusPredicate a predicate that indicates whether {@code exceptionFunction}
665
- * applies
666
- * @param exceptionFunction the function that returns the exception
653
+ * Provide a function to map specific error status codes to an error
654
+ * signal to to be propagated downstream instead of the response.
655
+ * <p>By default, if there are not matching status handlers, responses
656
+ * with status codes >= 400 are mapped to
657
+ * {@link WebClientResponseException} which is created with
658
+ * {@link ClientResponse#createException()}.
659
+ * <p>To suppress the treatment of a status code as an error and process
660
+ * it as a normal response, return {@code Mono.empty()} from the function.
661
+ * The response will then propagate downstream for processing.
662
+ * <p>To ignore an error response, handle it earlier with a
663
+ * {@link ExchangeFilterFunction filter}, or add {@code onErrorResume}
664
+ * downstream, for example:
665
+ * <pre class="code">
666
+ * webClient.get()
667
+ * .uri("http://abc.com/account/123")
668
+ * .retrieve()
669
+ * .bodyToMono(Account.class)
670
+ * .onErrorResume(WebClientResponseException.class,
671
+ * ex -> ex.getRawStatusCode() == 404 ? Mono.empty() : Mono.error(ex));
672
+ * </pre>
673
+ * @param statusPredicate to match responses with
674
+ * @param exceptionFunction to map the response to an error signal
667
675
* @return this builder
668
676
* @see ClientResponse#createException()
669
677
*/
670
678
ResponseSpec onStatus (Predicate <HttpStatus > statusPredicate ,
671
679
Function <ClientResponse , Mono <? extends Throwable >> exceptionFunction );
672
680
673
681
/**
674
- * Register a custom error function that gets invoked when the given raw status code
675
- * predicate applies. The exception returned from the function will be returned from
676
- * {@link #bodyToMono(Class)} and {@link #bodyToFlux(Class)}.
677
- * <p>By default, an error handler is registered that throws a
678
- * {@link WebClientResponseException} when the response status code is 4xx or 5xx.
679
- * @param statusCodePredicate a predicate of the raw status code that indicates
680
- * whether {@code exceptionFunction} applies.
681
- * <p><strong>NOTE:</strong> if the response is expected to have content,
682
- * the exceptionFunction should consume it. If not, the content will be
683
- * automatically drained to ensure resources are released.
684
- * @param exceptionFunction the function that returns the exception
682
+ * Variant of {@link #onStatus(Predicate, Function)} that works with
683
+ * raw status code values. This is useful for custom status codes.
684
+ * @param statusCodePredicate to match responses with
685
+ * @param exceptionFunction to map the response to an error signal
685
686
* @return this builder
686
687
* @since 5.1.9
687
688
*/
0 commit comments