1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 01:36:55 +00:00

OpenAI: improved error handling

This commit is contained in:
M66B 2024-05-20 18:25:13 +02:00
parent eac72fabfa
commit f54e0b75e0

View file

@ -204,8 +204,8 @@ public class OpenAI {
int status = connection.getResponseCode(); int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) { if (status != HttpURLConnection.HTTP_OK) {
// https://platform.openai.com/docs/guides/error-codes/api-errors
String error = "Error " + status + ": " + connection.getResponseMessage(); String error = "Error " + status + ": " + connection.getResponseMessage();
String detail = null;
try { try {
// HTTP 429 // HTTP 429
// { // {
@ -217,24 +217,24 @@ public class OpenAI {
// } // }
//} //}
InputStream is = connection.getErrorStream(); InputStream is = connection.getErrorStream();
if (is != null) { if (is != null)
String err = Helper.readStream(is); detail = Helper.readStream(is);
if (BuildConfig.DEBUG)
error += "\n" + err;
else {
Log.w(new Throwable(err));
try {
JSONObject jerror = new JSONObject(err).getJSONObject("error");
error += "\n" + jerror.getString("type") + ": " + jerror.getString("message");
} catch (JSONException ignored) {
error += "\n" + err;
}
}
}
} catch (Throwable ex) { } catch (Throwable ex) {
Log.w(ex); Log.w(ex);
} }
throw new IOException(error); Log.w("OpenAI error=" + error + " detail=" + detail);
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());