From f54e0b75e0bb49f527a936eba2aece5a82c7b56e Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 20 May 2024 18:25:13 +0200 Subject: [PATCH] OpenAI: improved error handling --- .../main/java/eu/faircode/email/OpenAI.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/OpenAI.java b/app/src/main/java/eu/faircode/email/OpenAI.java index 5995bf875d..f460e0f1fd 100644 --- a/app/src/main/java/eu/faircode/email/OpenAI.java +++ b/app/src/main/java/eu/faircode/email/OpenAI.java @@ -204,8 +204,8 @@ public class OpenAI { int status = connection.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { - // https://platform.openai.com/docs/guides/error-codes/api-errors String error = "Error " + status + ": " + connection.getResponseMessage(); + String detail = null; try { // HTTP 429 // { @@ -217,24 +217,24 @@ public class OpenAI { // } //} InputStream is = connection.getErrorStream(); - if (is != null) { - String err = 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; - } - } - } + if (is != null) + detail = Helper.readStream(is); } catch (Throwable 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());