X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FGraphApiExchangeFilterFunction.java;h=34ca5a71ef8d23cffd86f5ef442dc99d0dcfce1b;hb=refs%2Ftags%2Fwip-webclient;hp=0b583f4e394869fa9687abd8427d8ca1f315d58e;hpb=4c254dc37e6b29bd9aa872384ed3f6b60b1c20ac;p=facebook-errors diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java b/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java index 0b583f4..34ca5a7 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java @@ -6,6 +6,7 @@ import org.springframework.web.reactive.function.client.ClientRequest; import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.ExchangeFilterFunction; import org.springframework.web.reactive.function.client.ExchangeFunction; +import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; @@ -15,22 +16,34 @@ import reactor.core.publisher.Mono; */ public class GraphApiExchangeFilterFunction implements ExchangeFilterFunction { - public static GraphApiExchangeFilterFunction INSTANCE = - new GraphApiExchangeFilterFunction(); - - @Override public Mono filter(ClientRequest request, ExchangeFunction next) { return - next - .exchange(request) - .flatMap(response -> - { - return - HttpStatus.Series.CLIENT_ERROR.equals(response.statusCode().series()) - ? Mono.error(GraphApiException.create(response)) - : Mono.just(response); - }); + next + .exchange(request) + .flatMap(response -> + { + return + HttpStatus.Series.CLIENT_ERROR.equals(response.statusCode().series()) + ? response + .bodyToMono(String.class) + .map(errorBody -> GraphApiException.create( + response.statusCode(), + response.headers().asHttpHeaders(), + errorBody.getBytes())) + .map(e -> + e.getClass().equals(ErrorResponseParsingErrorException.class) + ? WebClientResponseException + .create( + e.getStatus().value(), + e.getStatus().getReasonPhrase(), + e.getHeaders(), + null, + null) + : e) + .flatMap(e -> Mono.error(e)) + : Mono.just(response); + }); } }