import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.reactive.JettyClientHttpConnector;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
.builder()
.clientConnector(new JettyClientHttpConnector())
.baseUrl(server.url("/").toString())
+ .filter(errorHandlingFilter())
.build();
}
.get()
.uri("/egal")
.retrieve()
- .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
.bodyToMono(String.class);
StepVerifier
result = webClient.get()
.uri("/greeting?name=Spring")
.retrieve()
- .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
.bodyToMono(String.class);
StepVerifier.create(result)
result = webClient.get()
.uri("/greeting?name=Spring")
.retrieve()
- .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
.bodyToMono(String.class);
StepVerifier.create(result)
public static ExchangeFilterFunction errorHandlingFilter()
{
- return ExchangeFilterFunction.ofResponseProcessor(clientResponse ->
+ return ExchangeFilterFunction.ofResponseProcessor(response ->
{
- if (clientResponse.statusCode() != null && (clientResponse.statusCode()
- .is5xxServerError() || clientResponse.statusCode().is4xxClientError()))
+ if (response.statusCode() != null && (response.statusCode()
+ .is5xxServerError() || response.statusCode().is4xxClientError()))
{
- return clientResponse.bodyToMono(String.class)
+ return response.bodyToMono(String.class)
.flatMap(errorBody ->
{
- return Mono.error(new Exception(errorBody));
+ return Mono.error(GraphApiException.create(response.statusCode(), response.headers().asHttpHeaders(), errorBody.getBytes()));
});
}
else
{
- return Mono.just(clientResponse);
+ return Mono.just(response);
}
});
}