Fix Error Handling in ChatBot and ImageBot

Properly pass through the HTTP Status code from the Chat/Image Bot proxy

Change-Id: I35c2852dec19ad7bdd4a61dab47e771783209614
parent aa3bf82b
...@@ -256,6 +256,7 @@ public final class ChatBot extends AndroidNonvisibleComponent { ...@@ -256,6 +256,7 @@ public final class ChatBot extends AndroidNonvisibleComponent {
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
ensureSslSockFactory(); ensureSslSockFactory();
String iToken; String iToken;
int responseCode = -1; // A reasonable default
try { try {
Log.d(LOG_TAG, "performRequest: apiKey = " + apiKey); Log.d(LOG_TAG, "performRequest: apiKey = " + apiKey);
if (token != null && !token.equals("") && token.substring(0, 1).equals("%")) { if (token != null && !token.equals("") && token.substring(0, 1).equals("%")) {
...@@ -289,7 +290,7 @@ public final class ChatBot extends AndroidNonvisibleComponent { ...@@ -289,7 +290,7 @@ public final class ChatBot extends AndroidNonvisibleComponent {
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
request.writeTo(connection.getOutputStream()); request.writeTo(connection.getOutputStream());
final int responseCode = connection.getResponseCode(); responseCode = connection.getResponseCode();
ChatBotToken.response response = ChatBotToken.response.parseFrom(connection.getInputStream()); ChatBotToken.response response = ChatBotToken.response.parseFrom(connection.getInputStream());
String returnText; String returnText;
if (responseCode == 200) { if (responseCode == 200) {
...@@ -297,7 +298,7 @@ public final class ChatBot extends AndroidNonvisibleComponent { ...@@ -297,7 +298,7 @@ public final class ChatBot extends AndroidNonvisibleComponent {
this.uuid = response.getUuid(); this.uuid = response.getUuid();
GotResponse(returnText); GotResponse(returnText);
} else { } else {
returnText = getResponseContent(connection, false); returnText = getResponseContent(connection, true);
ErrorOccurred(responseCode, returnText); ErrorOccurred(responseCode, returnText);
} }
} finally { } finally {
...@@ -313,9 +314,9 @@ public final class ChatBot extends AndroidNonvisibleComponent { ...@@ -313,9 +314,9 @@ public final class ChatBot extends AndroidNonvisibleComponent {
} catch (IOException ee) { } catch (IOException ee) {
returnText = "Error Fetching from ChatBot"; returnText = "Error Fetching from ChatBot";
} }
ErrorOccurred(404, returnText); ErrorOccurred(responseCode, returnText);
} else { } else {
ErrorOccurred(400, "Error talking to ChatBot proxy"); ErrorOccurred(responseCode, "Error talking to ChatBot proxy");
} }
} }
} }
......
...@@ -408,7 +408,7 @@ public class ImageBot extends AndroidNonvisibleComponent { ...@@ -408,7 +408,7 @@ public class ImageBot extends AndroidNonvisibleComponent {
} }
} }
private static class ImageException extends IOException { private static class ImageException extends Exception {
private final int code; private final int code;
private final String description; private final String description;
...@@ -542,6 +542,7 @@ public class ImageBot extends AndroidNonvisibleComponent { ...@@ -542,6 +542,7 @@ public class ImageBot extends AndroidNonvisibleComponent {
private String sendRequest(ImageBotToken.request request) throws ImageException { private String sendRequest(ImageBotToken.request request) throws ImageException {
HttpsURLConnection connection = null; HttpsURLConnection connection = null;
ensureSslSockFactory(); ensureSslSockFactory();
int responseCode = -1; // This means the connection never succeeded
try { try {
URL url = new URL(IMAGEBOT_SERVICE_URL); URL url = new URL(IMAGEBOT_SERVICE_URL);
connection = (HttpsURLConnection) url.openConnection(); connection = (HttpsURLConnection) url.openConnection();
...@@ -550,23 +551,27 @@ public class ImageBot extends AndroidNonvisibleComponent { ...@@ -550,23 +551,27 @@ public class ImageBot extends AndroidNonvisibleComponent {
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setDoOutput(true); connection.setDoOutput(true);
request.writeTo(connection.getOutputStream()); request.writeTo(connection.getOutputStream());
final int responseCode = connection.getResponseCode(); responseCode = connection.getResponseCode();
ImageBotToken.response response = ImageBotToken.response.parseFrom(
connection.getInputStream());
if (responseCode == 200) { if (responseCode == 200) {
ImageBotToken.response response = ImageBotToken.response.parseFrom(
connection.getInputStream());
byte[] imageData = response.getImage().toByteArray(); byte[] imageData = response.getImage().toByteArray();
File outFile = getOutputFile(); File outFile = getOutputFile();
FileOutputStream out = new FileOutputStream(outFile); FileOutputStream out = new FileOutputStream(outFile);
out.write(imageData); try {
out.flush(); out.write(imageData);
out.close(); out.flush();
} finally {
out.close();
}
return Uri.fromFile(outFile).toString(); return Uri.fromFile(outFile).toString();
} }
String errorMessage = IOUtils.readStreamAsString(connection.getErrorStream()); String errorMessage = IOUtils.readStreamAsString(connection.getErrorStream());
throw new ImageException(responseCode, errorMessage, null); throw new ImageException(responseCode, errorMessage, null);
} }
} catch (IOException e) { } catch (IOException e) {
throw new ImageException(404, e.toString(), e); Log.e(LOG_TAG, "Got an IOException", e);
throw new ImageException(responseCode, e.toString(), e);
} finally { } finally {
if (connection != null) { if (connection != null) {
connection.disconnect(); connection.disconnect();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment