WIP: WebClient - DIE LĂ–SUNG
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 19:34:38 +0000 (20:34 +0100)
committerKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 19:42:17 +0000 (20:42 +0100)
--
Guckst du hier: https://stackoverflow.com/a/48984852/247276

Die spannende Frage ist noch: was macht der anders...?

src/test/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunctionIntegrationTest.java

index 306f044..8d21cc5 100644 (file)
@@ -11,6 +11,7 @@ import org.junit.Before;
 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;
@@ -41,6 +42,7 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .builder()
             .clientConnector(new JettyClientHttpConnector())
             .baseUrl(server.url("/").toString())
+            .filter(errorHandlingFilter())
             .build();
        }
 
@@ -74,7 +76,6 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
             .get()
             .uri("/egal")
             .retrieve()
-            .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
             .bodyToMono(String.class);
 
     StepVerifier
@@ -105,7 +106,6 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
     result = webClient.get()
                                .uri("/greeting?name=Spring")
                                .retrieve()
-        .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
                                .bodyToMono(String.class);
 
                StepVerifier.create(result)
@@ -122,7 +122,6 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
                result = webClient.get()
                                .uri("/greeting?name=Spring")
                                .retrieve()
-        .onStatus(status -> status.is4xxClientError(), response -> GraphApiException.create(response))
                                .bodyToMono(String.class);
 
                StepVerifier.create(result)
@@ -133,20 +132,20 @@ public class GraphApiExchangeFilterFunctionIntegrationTest
 
   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);
       }
     });
   }