From d441540e6ab4650ae2ed46f0849ac6d02732f730 Mon Sep 17 00:00:00 2001 From: Thomas HERBETH Date: Mon, 2 Jan 2017 18:36:13 +0100 Subject: [PATCH] [Fix] #95 : now closing the socket if ANY exception occurs during an 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. --- .../booboot/vndbandroid/api/VNDBServer.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/booboot/vndbandroid/api/VNDBServer.java b/app/src/main/java/com/booboot/vndbandroid/api/VNDBServer.java index ca061a12..e5b2e0e3 100644 --- a/app/src/main/java/com/booboot/vndbandroid/api/VNDBServer.java +++ b/app/src/main/java/com/booboot/vndbandroid/api/VNDBServer.java @@ -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; @@ -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"); @@ -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; @@ -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; @@ -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(); @@ -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; @@ -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; } } @@ -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;