Skip to content

Commit

Permalink
[Fix] #95 : now closing the socket if ANY exception occurs during an …
Browse files Browse the repository at this point in the history
…API request.

[Fix] #95 : if VNDB.org returns an empty response, we no longer assume we have been throttled, and just always leave here and now.
[Fix] We now catch ArrayIndexOutOfBoundsExceptions during InputStream.read(), to properly execute the error callback, because it might exceptionally throw this exception at runtime.
  • Loading branch information
herbeth1u committed Jan 2, 2017
1 parent 45bff03 commit d441540
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions app/src/main/java/com/booboot/vndbandroid/api/VNDBServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public static VNDBCommand sendCommand(String command, VNDBCommand params, int so
query.append(JSON.mapper.writeValueAsString(params));
query.append(EOM);
} catch (JsonProcessingException jpe) {
VNDBServer.close(socketIndex);
errorCallback.message = "Unable to process the query to the API as JSON. Aborting operation.";
errorCallback.call();
return null;
Expand All @@ -248,7 +249,7 @@ public static VNDBCommand sendCommand(String command, VNDBCommand params, int so
out.write(query.toString().getBytes("UTF-8"));

isThrottled = false;
response = getResponse(in);
response = getResponse(socketIndex, in);
if (response instanceof Error) {
Error error = (Error) response;
isThrottled = error.getId().equals("throttled");
Expand All @@ -272,6 +273,7 @@ public static VNDBCommand sendCommand(String command, VNDBCommand params, int so
}
} while (isThrottled);
} catch (UnsupportedEncodingException uee) {
VNDBServer.close(socketIndex);
errorCallback.message = "Tried to send a query to the API with a wrong encoding. Aborting operation.";
errorCallback.call();
return null;
Expand All @@ -283,6 +285,7 @@ public static VNDBCommand sendCommand(String command, VNDBCommand params, int so
return null;
} catch (IOException ioe) {
ioe.printStackTrace();
VNDBServer.close(socketIndex);
errorCallback.message = "An error occurred while sending a query to the API. Please try again later.";
errorCallback.call();
return null;
Expand All @@ -295,7 +298,7 @@ public static VNDBCommand sendCommand(String command, VNDBCommand params, int so
}
}

private static VNDBCommand getResponse(InputStreamReader in) {
private static VNDBCommand getResponse(int socketIndex, InputStreamReader in) {
StringBuilder response = new StringBuilder();
try {
int read = in.read();
Expand All @@ -304,8 +307,9 @@ private static VNDBCommand getResponse(InputStreamReader in) {
read = in.read();
// Log.e("D", response.toString());
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
VNDBServer.close(socketIndex);
errorCallback.message = "An error occurred while receiving the response from the API. Please try again later.";
errorCallback.call();
return null;
Expand All @@ -319,13 +323,11 @@ private static VNDBCommand getResponse(InputStreamReader in) {
if (response.toString().trim().equals("ok"))
return new Ok();
else {
if (threadManager == null || !threadManager.isShutdown()) {
/* Calling the error callback only if we're not pipelining (might be wrong to call the error callback while there are other threads running) */
errorCallback.message = "An error occurred while reading the response from the API. Aborting operation.";
errorCallback.call();
}
/* Undocumented error : the server returned an empty response (""), which (most of the time) means we have been throttled */
return new Error("throttled", 6, 10 * 60);
/* Undocumented error : the server returned an empty response (""), which means absolutely nothing but "leave the ship because something undebuggable happened!" */
VNDBServer.close(socketIndex);
errorCallback.message = "VNDB.org returned an unexpected error. Please try again later.";
errorCallback.call();
return null;
}
}

Expand All @@ -335,6 +337,7 @@ private static VNDBCommand getResponse(InputStreamReader in) {
return (VNDBCommand) JSON.mapper.readValue(params, VNDBCommand.getClass(command));
} catch (IOException ioe) {
ioe.printStackTrace();
VNDBServer.close(socketIndex);
errorCallback.message = "An error occurred while decoding the response from the API. Aborting operation.";
errorCallback.call();
return null;
Expand Down

0 comments on commit d441540

Please sign in to comment.