Gemini: improved error handling

pull/215/head
M66B 10 months ago
parent 248b2382bc
commit eac72fabfa

@ -188,15 +188,27 @@ public class Gemini {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) { if (status != HttpURLConnection.HTTP_OK) {
String error = "Error " + status + ": " + connection.getResponseMessage(); String error = "Error " + status + ": " + connection.getResponseMessage();
String detail = null;
try { try {
InputStream is = connection.getErrorStream(); InputStream is = connection.getErrorStream();
if (is != null) if (is != null)
error += "\n" + Helper.readStream(is); detail = Helper.readStream(is);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
Log.w("Gemini error=" + error); Log.w("Gemini error=" + error + " detail=" + detail);
throw new IOException(error); if (detail != null)
try {
JSONObject jroot = new JSONObject(detail);
JSONObject jerror = jroot.optJSONObject("error");
if (jerror != null) {
String msg = jerror.optString("message");
if (!TextUtils.isEmpty(msg))
detail = msg;
}
} catch (Throwable ignored) {
}
throw new IOException(TextUtils.isEmpty(detail) ? error : detail);
} }
String response = Helper.readStream(connection.getInputStream()); String response = Helper.readStream(connection.getInputStream());

Loading…
Cancel
Save