From 6470b22c711907baeba54873f26266970d07d3c2 Mon Sep 17 00:00:00 2001 From: M66B <259573+M66B@users.noreply.github.com> Date: Thu, 28 Mar 2024 18:17:00 +0100 Subject: [PATCH] Gemini: basic error handling --- .../main/java/eu/faircode/email/Gemini.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Gemini.java b/app/src/main/java/eu/faircode/email/Gemini.java index e900e7368d..369083dd11 100644 --- a/app/src/main/java/eu/faircode/email/Gemini.java +++ b/app/src/main/java/eu/faircode/email/Gemini.java @@ -75,18 +75,42 @@ public class Gemini { JSONObject jresponse = call(context, "POST", path, jrequest); + // { + // "promptFeedback": { + // "blockReason": "SAFETY", + // "safetyRatings": [ + // { + // "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", + // "probability": "NEGLIGIBLE" + // }, + // { + // "category": "HARM_CATEGORY_HATE_SPEECH", + // "probability": "NEGLIGIBLE" + // }, + // { + // "category": "HARM_CATEGORY_HARASSMENT", + // "probability": "MEDIUM" + // }, + // { + // "category": "HARM_CATEGORY_DANGEROUS_CONTENT", + // "probability": "NEGLIGIBLE" + // } + // ] + // } + // } + JSONArray jcandidates = jresponse.optJSONArray("candidates"); if (jcandidates == null || jcandidates.length() < 1) - throw new IOException("candidates missing"); + throw new IOException(jresponse.toString(2)); JSONObject jcontent = jcandidates.getJSONObject(0).optJSONObject("content"); if (jcontent == null) - throw new IOException("content missing"); + throw new IOException(jresponse.toString(2)); JSONArray jparts = jcontent.optJSONArray("parts"); if (jparts == null || jparts.length() < 1) - throw new IOException("parts missing"); + throw new IOException(jresponse.toString(2)); JSONObject jtext = jparts.getJSONObject(0); if (!jtext.has("text")) - throw new IOException("text missing"); + throw new IOException(jresponse.toString(2)); return new String[]{jtext.getString("text")}; }