X-Git-Url: https://juplo.de/gitweb/?p=facebook-utils;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Fclient%2FGraphApiErrorHandler.java;h=6b1ab971af2f98a51b663c4dde39154a403ed173;hp=2b73e4918741fd66d29967f50565dd8696344d2c;hb=24361a39e7ec511fd621c0fee59263e70ab0c2c9;hpb=5f2b31dc7f443ab9eb088d3150e2d4903cb40159 diff --git a/src/main/java/de/juplo/facebook/client/GraphApiErrorHandler.java b/src/main/java/de/juplo/facebook/client/GraphApiErrorHandler.java index 2b73e49..6b1ab97 100644 --- a/src/main/java/de/juplo/facebook/client/GraphApiErrorHandler.java +++ b/src/main/java/de/juplo/facebook/client/GraphApiErrorHandler.java @@ -4,6 +4,9 @@ import de.juplo.facebook.exceptions.GraphApiException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.Charset; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; @@ -16,12 +19,12 @@ import org.springframework.web.client.ResponseErrorHandler; * Error-Handler for error-messages from the Facebook Graph-API. *

* This error-handler handels responses withe the HTTP-status code - * {@code 4xx}. It tries to extract and parse the error-message + * {@code 400 BAD REQUEST}. It tries to extract and parse the error-message * from the HTTP-body. Successfully extracted and parsed messages are mapped * to a hierarchy of exceptions, that reflects the hierarchy of the error- * codes and -types. *

- * If the HTTP-status-code of the response is not {@code 4xx} or + * If the HTTP-status-code of the response is not {@code 400 BAD REQUEST} or * the HTTP-body could not be extracted or parsed, this error-handler * delegates the handling to its parent. * @@ -31,6 +34,9 @@ import org.springframework.web.client.ResponseErrorHandler; */ public class GraphApiErrorHandler implements ResponseErrorHandler { + private final static Logger LOG = + LoggerFactory.getLogger(GraphApiErrorHandler.class); + private final ResponseErrorHandler parent; @@ -51,10 +57,10 @@ public class GraphApiErrorHandler implements ResponseErrorHandler @Override public void handleError(final ClientHttpResponse response) throws IOException { - if (!HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())) + if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode())) { - // Let the parent-error-handler handle all errors, that are no client - // errors (4xx). + // We will only handle 400 BAD REQUEST + LOG.debug("ignoring response with status-code {}.", response.getStatusCode()); parent.handleError(response); return; } @@ -63,6 +69,7 @@ public class GraphApiErrorHandler implements ResponseErrorHandler if (response.getBody() == null) { // There is no body to interpret in the HTTP-message + LOG.warn("Could not convert the response into an exception, because there is no message-body."); parent.handleError(response); return; } @@ -73,12 +80,16 @@ public class GraphApiErrorHandler implements ResponseErrorHandler try { error = GraphApiException.create(body); + if (LOG.isInfoEnabled()) + LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8"))); } catch (Exception e) { // The body of the HTTP-message could not be parsed. // Let the parent error-handler try to handle the response. + LOG.warn("Could not convert the response into an exception, because the body is unparsable: {}", body); + // To do so, we have to wrap the original response to fill in // the buffered body, if needed ClientHttpResponse buffered = new ClientHttpResponse()