Permalink
Browse files

Better exceptions handling.

  • Loading branch information...
1 parent d1b9e1f commit 2dfd83c39868815abbc3939189d091aea9429d8e Andrew Kirilenko committed Mar 21, 2012
@@ -114,15 +114,14 @@ public void setMaxRetries(int maxRetries) {
this.maxRetries = maxRetries;
}
- private HttpResponse doRequestExecute(HttpRequestBase request) {
+ private HttpResponse doRequestExecute(HttpRequestBase request) throws APIException {
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
int currentTry = 0;
while (currentTry < maxRetries) {
try {
- response = null;
response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 503) {
@@ -132,8 +131,14 @@ private HttpResponse doRequestExecute(HttpRequestBase request) {
try {
Thread.sleep((long)(Math.pow(4, currentTry) * 100 * Math.random()));
} catch (InterruptedException e) {
+ if (currentTry == maxRetries - 1) {
+ throw new APIException(null, e);
+ }
}
} catch (IOException e) {
+ if (currentTry == maxRetries - 1) {
+ throw new APIException(null, e);
+ }
}
currentTry += 1;
@@ -142,7 +147,7 @@ private HttpResponse doRequestExecute(HttpRequestBase request) {
return response;
}
- private HttpResponse doRequest(HttpRequestBase request, String method, Map<String, String> params) {
+ private HttpResponse doRequest(HttpRequestBase request, String method, Map<String, String> params) throws APIException {
List<NameValuePair> qParams = new ArrayList<NameValuePair>();
if (params != null) {
@@ -161,7 +166,7 @@ private HttpResponse doRequest(HttpRequestBase request, String method, Map<Strin
try {
uri = URIUtils.createURI(scheme, host, port, "" + apiVersion + "/" + method, query, null);
} catch (URISyntaxException e) {
- return null;
+ throw new APIException(null, e);
}
request.setURI(uri);
@@ -173,12 +178,12 @@ private HttpResponse doRequest(HttpRequestBase request, String method, Map<Strin
return doRequestExecute(request);
}
- private HttpResponse doFileRequest(File file, String method, Map<String, String> params) {
+ private HttpResponse doFileRequest(File file, String method, Map<String, String> params) throws APIException {
URI uri = null;
try {
uri = URIUtils.createURI(scheme, host, port, "" + apiVersion + "/" + method, null, null);
} catch (URISyntaxException e) {
- return null;
+ throw new APIException(null, e);
}
HttpPost request = new HttpPost();
@@ -194,7 +199,7 @@ private HttpResponse doFileRequest(File file, String method, Map<String, String>
entity.addPart("data", new StringBody((new Gson()).toJson(params)));
entity.addPart("file", new FileBody(file));
} catch (UnsupportedEncodingException e) {
- return null;
+ throw new APIException(null, e);
}
request.setEntity(entity);
@@ -203,30 +208,22 @@ private HttpResponse doFileRequest(File file, String method, Map<String, String>
}
private InputStream parseResponseGeneral(HttpResponse response) throws APIException {
- if (response == null) {
- throw new APIException("No response");
- }
-
InputStream result = null;
try {
result = response.getEntity().getContent();
} catch (IOException e) {
- throw new APIException("No content");
- }
-
- if (result == null) {
- throw new APIException("No content");
+ throw new APIException(null, e);
}
if (response.getStatusLine().getStatusCode() != 200) {
String r = null;
try {
r = IOUtils.toString(result);
} catch (IOException e) {
- throw new APIException("No content");
+ throw new APIException(null, e);
}
- throw new APIException(r);
+ throw new APIException(r, null);
}
return result;
@@ -236,14 +233,14 @@ private InputStream parseResponseGeneral(HttpResponse response) throws APIExcept
try {
return IOUtils.toByteArray(parseResponseGeneral(response));
} catch (IOException e) {
- throw new APIException("No content");
+ throw new APIException(null, e);
}
}
private String parseResponseAsString(HttpResponse response) throws APIException {
try {
return IOUtils.toString(parseResponseGeneral(response));
} catch (IOException e) {
- throw new APIException("No content");
+ throw new APIException(null, e);
}
}
@@ -268,7 +265,7 @@ public JsonObject codesCreate(String name, String file, String runtime, String r
File f = new File(file);
if (!f.exists()) {
- throw new APIException("No file");
+ throw new APIException("File " + file + " not found", null);
}
return parseResponseAsJson(doFileRequest(f, "projects/" + projectId + "/codes", params));
@@ -2,16 +2,21 @@
public class APIException extends Exception {
private String message;
+ private Exception innerException;
- public APIException(String message) {
+ public APIException(String message, Exception innerException) {
this.message = message;
}
public String getMessage() {
return message;
}
+ public Exception getInnerException() {
+ return innerException;
+ }
+
public String toString() {
- return message;
+ return message != null ? message : innerException.toString();
}
}

0 comments on commit 2dfd83c

Please sign in to comment.