summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-23 23:44:39 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-23 23:44:39 +0200
commit5dd7436fa096d67758a25a713fdd8d70ca8d605c (patch)
tree2a6497836625798e776cc58f53ed843956d1c8f9
parent413044872e2ff69023e3cac9df8348a8a2117a7a (diff)
downloadTwitterDataAnalytics-5dd7436fa096d67758a25a713fdd8d70ca8d605c.tar.gz
Catch error responses and log them
-rw-r--r--src/mining/AbstractRequester.java31
-rw-r--r--src/mining/Requester.java2
2 files changed, 30 insertions, 3 deletions
diff --git a/src/mining/AbstractRequester.java b/src/mining/AbstractRequester.java
index bfa2b6d..1e66668 100644
--- a/src/mining/AbstractRequester.java
+++ b/src/mining/AbstractRequester.java
@@ -6,6 +6,7 @@ import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
+import java.util.logging.Logger;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.json.JSONException;
@@ -18,17 +19,29 @@ import org.json.JSONObject;
*/
public abstract class AbstractRequester implements Requester {
+ private static final Logger LOGGER
+ = Logger.getLogger(AbstractRequester.class.getName());
+
private static final String API_URL = "https://api.twitter.com/";
@Override
public JSONObject getJSON(String resource) throws IOException {
HttpURLConnection conn = open(buildUrl(resource));
preconnect(conn);
+ JSONObject resp = getResponseAsJson(conn);
+ /* print response to stderr for debugging */
+ if (resp.has("errors")) {
+ try {
+ String errors = resp.get("errors").toString();
+ getLogger().fine("Request failed: " + errors);
+ } catch (JSONException ex) {
+ }
+ }
if (conn.getResponseCode() != 200) {
// TODO: print more helpful details
throw new IOException("Unexpected response code");
}
- return getResponseAsJson(conn);
+ return resp;
}
protected final URL buildUrl(String resource) throws IOException {
@@ -51,6 +64,7 @@ public abstract class AbstractRequester implements Requester {
* @throws java.io.IOException on failure to open a connection.
*/
protected final HttpURLConnection open(URL url) throws IOException {
+ getLogger().fine("Opening: " + url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// set default param: fail if no response within 5 seconds
conn.setReadTimeout(5000);
@@ -65,10 +79,17 @@ public abstract class AbstractRequester implements Requester {
* @throws java.io.IOException if the response cannot be retrieved or if the
* response does not contain well-formed JSON.
*/
- protected final JSONObject getResponseAsJson(URLConnection conn)
+ protected final JSONObject getResponseAsJson(HttpURLConnection conn)
throws IOException {
StringWriter writer = new StringWriter();
- IOUtils.copy(conn.getInputStream(), writer, Charsets.UTF_8);
+ InputStream is;
+ try {
+ is = conn.getInputStream();
+ } catch (IOException ex) {
+ /* 404 (FileNotFoundException) */
+ is = conn.getErrorStream();
+ }
+ IOUtils.copy(is, writer, Charsets.UTF_8);
try {
return new JSONObject(writer.toString());
} catch (JSONException ex) {
@@ -85,4 +106,8 @@ public abstract class AbstractRequester implements Requester {
*/
protected abstract void preconnect(URLConnection conn)
throws IOException;
+
+ private Logger getLogger() {
+ return Logger.getLogger(getClass().getName());
+ }
}
diff --git a/src/mining/Requester.java b/src/mining/Requester.java
index 931b0ac..df6053f 100644
--- a/src/mining/Requester.java
+++ b/src/mining/Requester.java
@@ -20,4 +20,6 @@ public interface Requester {
* @throws java.io.IOException on error fetching the resource.
*/
public JSONObject getJSON(String resource) throws IOException;
+
+ // TODO: retry (after sleeping) on ratelimit
}