OpenAI: improved error handling

This commit is contained in:
M66B 2023-03-08 19:50:16 +01:00
parent 5ae26ccdf2
commit 6b819dcd64
1 changed files with 11 additions and 2 deletions

View File

@ -115,8 +115,17 @@ public class OpenAI {
String error = "Error " + status + ": " + connection.getResponseMessage();
try {
InputStream is = connection.getErrorStream();
if (is != null)
error += "\n" + Helper.readStream(is);
if (is != null) {
json = Helper.readStream(is);
Log.w(json);
try {
JSONObject jresponse = new JSONObject(json);
JSONObject jerror = jresponse.getJSONObject("error");
error += "\n" + jerror.getString("type") + ": " + jerror.getString("message");
} catch (JSONException e) {
error += "\n" + json;
}
}
} catch (Throwable ex) {
Log.w(ex);
}