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 {
HttpsURLConnection connection = null;
ensureSslSockFactory();
String iToken;
int responseCode = -1; // A reasonable default
try {
Log.d(LOG_TAG, "performRequest: apiKey = " + apiKey);
if (token != null && !token.equals("") && token.substring(0, 1).equals("%")) {
......@@ -289,7 +290,7 @@ public final class ChatBot extends AndroidNonvisibleComponent {
connection.setRequestMethod("POST");
connection.setDoOutput(true);
request.writeTo(connection.getOutputStream());
final int responseCode = connection.getResponseCode();
responseCode = connection.getResponseCode();
ChatBotToken.response response = ChatBotToken.response.parseFrom(connection.getInputStream());
String returnText;
if (responseCode == 200) {
......@@ -297,7 +298,7 @@ public final class ChatBot extends AndroidNonvisibleComponent {
this.uuid = response.getUuid();
GotResponse(returnText);
} else {
returnText = getResponseContent(connection, false);
returnText = getResponseContent(connection, true);
ErrorOccurred(responseCode, returnText);
}
} finally {
......@@ -313,9 +314,9 @@ public final class ChatBot extends AndroidNonvisibleComponent {
} catch (IOException ee) {
returnText = "Error Fetching from ChatBot";
}
ErrorOccurred(404, returnText);
ErrorOccurred(responseCode, returnText);
} else {
ErrorOccurred(400, "Error talking to ChatBot proxy");
ErrorOccurred(responseCode, "Error talking to ChatBot proxy");
}
}
}
......
......@@ -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 String description;
......@@ -542,6 +542,7 @@ public class ImageBot extends AndroidNonvisibleComponent {
private String sendRequest(ImageBotToken.request request) throws ImageException {
HttpsURLConnection connection = null;
ensureSslSockFactory();
int responseCode = -1; // This means the connection never succeeded
try {
URL url = new URL(IMAGEBOT_SERVICE_URL);
connection = (HttpsURLConnection) url.openConnection();
......@@ -550,23 +551,27 @@ public class ImageBot extends AndroidNonvisibleComponent {
connection.setRequestMethod("POST");
connection.setDoOutput(true);
request.writeTo(connection.getOutputStream());
final int responseCode = connection.getResponseCode();
ImageBotToken.response response = ImageBotToken.response.parseFrom(
connection.getInputStream());
responseCode = connection.getResponseCode();
if (responseCode == 200) {
ImageBotToken.response response = ImageBotToken.response.parseFrom(
connection.getInputStream());
byte[] imageData = response.getImage().toByteArray();
File outFile = getOutputFile();
FileOutputStream out = new FileOutputStream(outFile);
out.write(imageData);
out.flush();
out.close();
try {
out.write(imageData);
out.flush();
} finally {
out.close();
}
return Uri.fromFile(outFile).toString();
}
String errorMessage = IOUtils.readStreamAsString(connection.getErrorStream());
throw new ImageException(responseCode, errorMessage, null);
}
} 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 {
if (connection != null) {
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