Fixed bug / refined GraphApiErrorResponseErrorHandler
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 07:48:45 +0000 (08:48 +0100)
committerKai Moritz <kai@jupl.de>
Fri, 22 Nov 2019 06:39:44 +0000 (07:39 +0100)
If the response is not a client-error (4xx) or can not be parsed as a
Graph-API error, the handler should behave identical to its parent.

src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java
src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java

index e2f52ff..9df86a9 100644 (file)
@@ -55,25 +55,26 @@ public class GraphApiErrorResponseErrorHandler implements ResponseErrorHandler
   }
 
 
   }
 
 
-  @Override
-  public boolean hasError(ClientHttpResponse response) throws IOException
+  private boolean hasGraphApiError(ClientHttpResponse response) throws IOException
   {
     return HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series());
   }
 
   @Override
   {
     return HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series());
   }
 
   @Override
-  public void handleError(final ClientHttpResponse response) throws IOException
+  public boolean hasError(ClientHttpResponse response) throws IOException
   {
   {
-    GraphApiErrorResponseErrorHandler.handleError(parent, response);
+    return hasGraphApiError(response) || parent.hasError(response);
   }
 
   }
 
-  public static void handleError(
-      final ResponseErrorHandler parent,
-      final ClientHttpResponse response
-      )
-      throws
-        IOException
+  @Override
+  public void handleError(final ClientHttpResponse response) throws IOException
   {
   {
+    if (!hasGraphApiError(response))
+    {
+      parent.handleError(response);
+      return;
+    }
+
     if (response.getBody() == null)
     {
       // There is no body to interpret in the HTTP-message
     if (response.getBody() == null)
     {
       // There is no body to interpret in the HTTP-message
index 6593375..55025e7 100644 (file)
@@ -10,6 +10,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.client.HttpClientErrorException;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpServerErrorException;
 import org.springframework.web.client.RestTemplate;
 
 
 import org.springframework.web.client.RestTemplate;
 
 
@@ -32,13 +33,7 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
   {
     LOG.info("testNoError");
 
   {
     LOG.info("testNoError");
 
-    requestFactory.setBody("{\n" +
-        "  \"error\": {\n" +
-        "    \"message\": \"(#200) The user hasn't authorized the application to perform this action\",\n" +
-        "    \"type\": \"OAuthException\",\n" +
-        "    \"code\": 200\n" +
-        "  }\n" +
-        "}");
+    requestFactory.setBody("{ \"message\": \"Hello World!\" }");
 
 
     requestFactory.setStatus(HttpStatus.CONTINUE);
 
 
     requestFactory.setStatus(HttpStatus.CONTINUE);
@@ -74,10 +69,31 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest
       fail("Unexpected error: " + e);
     }
 
       fail("Unexpected error: " + e);
     }
 
+    requestFactory.setStatus(HttpStatus.BAD_REQUEST);
+    try
+    {
+      clientTemplate.getForObject("ANY", SOME.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpClientErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
+    }
+    catch(Exception e)
+    {
+      LOG.debug("{}", e.toString());
+      fail("Unexpected error: " + e);
+    }
+
     requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
     try
     {
       clientTemplate.getForObject("ANY", SOME.class);
     requestFactory.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
     try
     {
       clientTemplate.getForObject("ANY", SOME.class);
+      fail("The parent handler should have raised an exception!");
+    }
+    catch(HttpServerErrorException e)
+    {
+      LOG.debug("Expexted error: {}", e.toString());
     }
     catch(Exception e)
     {
     }
     catch(Exception e)
     {