From b05c51b4f7dc4bfe423a7ea306bf82f34eb0faf9 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Thu, 21 Nov 2019 08:48:45 +0100 Subject: [PATCH] Fixed bug / refined GraphApiErrorResponseErrorHandler 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. --- .../GraphApiErrorResponseErrorHandler.java | 21 ++++++------- ...orResponseErrorHandlerIntegrationTest.java | 30 ++++++++++++++----- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java b/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java index e2f52ff..9df86a9 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandler.java @@ -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 - 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 diff --git a/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java b/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java index 6593375..55025e7 100644 --- a/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java +++ b/src/test/java/de/juplo/facebook/errors/GraphApiErrorResponseErrorHandlerIntegrationTest.java @@ -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.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; @@ -32,13 +33,7 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest { 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); @@ -74,10 +69,31 @@ public class GraphApiErrorResponseErrorHandlerIntegrationTest 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); + fail("The parent handler should have raised an exception!"); + } + catch(HttpServerErrorException e) + { + LOG.debug("Expexted error: {}", e.toString()); } catch(Exception e) { -- 2.20.1