1 package de.juplo.facebook.errors;
4 import java.io.ByteArrayInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.nio.charset.Charset;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.http.HttpHeaders;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.http.client.ClientHttpResponse;
13 import org.springframework.util.FileCopyUtils;
14 import org.springframework.web.client.ResponseErrorHandler;
19 * Error-Handler for error-messages from the Facebook Graph-API.
21 * This error-handler handels responses withe the HTTP-status code
22 * {@code 400 BAD REQUEST}. It tries to extract and parse the error-message
23 * from the HTTP-body. Successfully extracted and parsed messages are mapped
24 * to a hierarchy of exceptions, that reflects the hierarchy of the error-
27 * If the HTTP-status-code of the response is not {@code 400 BAD REQUEST} or
28 * the HTTP-body could not be extracted or parsed, this error-handler
29 * delegates the handling to its parent.
31 * @see <a href="https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#errors">Graph-API Documentation</a>
32 * @see <a href="http://fbdevwiki.com/wiki/Error_codes">Inofficial Wiki For Facebook-Developers</a>
35 public class GraphApiErrorHandler implements ResponseErrorHandler
37 private final static Logger LOG =
38 LoggerFactory.getLogger(GraphApiErrorHandler.class);
40 private final ResponseErrorHandler parent;
43 public GraphApiErrorHandler(ResponseErrorHandler errorHandler)
45 this.parent = errorHandler;
50 public boolean hasError(ClientHttpResponse response) throws IOException
53 HttpStatus.Series.CLIENT_ERROR.equals(response.getStatusCode().series())
54 || this.parent.hasError(response);
58 public void handleError(final ClientHttpResponse response) throws IOException
60 if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode()))
62 // We will only handle 400 BAD REQUEST
63 LOG.debug("ignoring response with status-code {}.", response.getStatusCode());
64 parent.handleError(response);
69 if (response.getBody() == null)
71 // There is no body to interpret in the HTTP-message
72 LOG.warn("Could not convert the response into an exception, because there is no message-body.");
73 parent.handleError(response);
77 final byte[] body = FileCopyUtils.copyToByteArray(response.getBody());
78 GraphApiException error;
82 error = GraphApiException.create(body);
83 if (LOG.isInfoEnabled())
84 LOG.info("error-response: {}", new String(body, Charset.forName("UTF-8")));
88 // The body of the HTTP-message could not be parsed.
89 // Let the parent error-handler try to handle the response.
91 LOG.warn("Could not convert the response into an exception, because the body is unparsable: {}", body);
93 // To do so, we have to wrap the original response to fill in
94 // the buffered body, if needed
95 ClientHttpResponse buffered = new ClientHttpResponse()
98 public HttpStatus getStatusCode() throws IOException
100 return response.getStatusCode();
104 public synchronized InputStream getBody() throws IOException
106 return new ByteArrayInputStream(body);
110 public HttpHeaders getHeaders()
112 return response.getHeaders();
116 public String getStatusText() throws IOException
118 return response.getStatusText();
128 public int getRawStatusCode() throws IOException
130 return response.getRawStatusCode();
134 parent.handleError(buffered);