From d28acc7d55b34bea296e83bfea3b2e1fd40bf10a Mon Sep 17 00:00:00 2001 From: Daniel Kao Date: Wed, 16 Oct 2024 00:45:48 +0800 Subject: [PATCH] fix: crashes when there's no network --- .../plateaukao/einkbro/EinkBroApplication.kt | 7 +- .../einkbro/service/OpenAiRepository.kt | 69 ++++++++++--------- .../einkbro/service/TranslateRepository.kt | 47 +++++++------ 3 files changed, 65 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/info/plateaukao/einkbro/EinkBroApplication.kt b/app/src/main/java/info/plateaukao/einkbro/EinkBroApplication.kt index 86b4fb87..f084bbf4 100644 --- a/app/src/main/java/info/plateaukao/einkbro/EinkBroApplication.kt +++ b/app/src/main/java/info/plateaukao/einkbro/EinkBroApplication.kt @@ -14,7 +14,6 @@ import info.plateaukao.einkbro.pocket.PocketNetwork import info.plateaukao.einkbro.preference.ConfigManager import info.plateaukao.einkbro.service.TtsManager import info.plateaukao.einkbro.unit.LocaleManager -import info.plateaukao.einkbro.util.CustomExceptionHandler import org.koin.android.ext.koin.androidContext import org.koin.core.context.GlobalContext.startKoin import org.koin.dsl.module @@ -61,9 +60,9 @@ class EinkBroApplication : Application() { instance = this - Thread.setDefaultUncaughtExceptionHandler( - CustomExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()) - ) +// Thread.setDefaultUncaughtExceptionHandler( +// CustomExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()) +// ) } override fun onTerminate() { diff --git a/app/src/main/java/info/plateaukao/einkbro/service/OpenAiRepository.kt b/app/src/main/java/info/plateaukao/einkbro/service/OpenAiRepository.kt index 1ab81c9d..20ea082a 100644 --- a/app/src/main/java/info/plateaukao/einkbro/service/OpenAiRepository.kt +++ b/app/src/main/java/info/plateaukao/einkbro/service/OpenAiRepository.kt @@ -93,6 +93,7 @@ class OpenAiRepository : KoinComponent { json.decodeFromString(ChatCompletionDelta.serializer(), data) appendResponseAction(chatCompletion.choices.first().delta.content.orEmpty()) } catch (e: Exception) { + Log.e("OpenAiRepository", "Error parsing chat completion: $data", e) failureAction() eventSource.cancel() this@OpenAiRepository.eventSource = null @@ -114,20 +115,20 @@ class OpenAiRepository : KoinComponent { failureAction: () -> Unit, ) { val request = createGeminiRequest(messages, gptActionInfo, true) - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) { - failureAction() - return - } - val inputStream = response.body?.byteStream() ?: return - inputStream.source().buffer().use { source -> - while (!source.exhausted()) { - val chunk = source.readUtf8Line() - if (chunk == null) { - failureAction() - return - } - try { + try { + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) { + failureAction() + return + } + val inputStream = response.body?.byteStream() ?: return + inputStream.source().buffer().use { source -> + while (!source.exhausted()) { + val chunk = source.readUtf8Line() + if (chunk == null) { + failureAction() + return + } Log.d("OpenAiRepository", "chunk: $chunk") val textField = "\"text\": \"" if (chunk.contains(textField)) { @@ -136,12 +137,13 @@ class OpenAiRepository : KoinComponent { Log.d("OpenAiRepository", "text: $text") appendResponseAction(text) } - } catch (e: Exception) { - failureAction() - return } } } + } catch (e: Exception) { + Log.e("OpenAiRepository", "Error fetching Gemini stream", e) + failureAction() + return } } @@ -152,15 +154,16 @@ class OpenAiRepository : KoinComponent { voiceOption = config.gptVoiceOption, ) - client.newCall(request).execute().use { response -> - if (response.code != 200 || response.body == null) { - return@use continuation.resume(null) - } - try { + try { + client.newCall(request).execute().use { response -> + if (response.code != 200 || response.body == null) { + return@use continuation.resume(null) + } continuation.resume(response.body?.bytes()) - } catch (e: Exception) { - continuation.resume(null) } + } catch (e: Exception) { + Log.e("OpenAiRepository", "Error fetching TTS", e) + continuation.resume(null) } } @@ -169,20 +172,21 @@ class OpenAiRepository : KoinComponent { gptActionInfo: ChatGPTActionInfo, ): ChatCompletion? = suspendCoroutine { continuation -> val request = createCompletionRequest(messages, gptActionInfo) - client.newCall(request).execute().use { response -> - if (response.code != 200 || response.body == null) { - return@use continuation.resume(null) - } + try { + client.newCall(request).execute().use { response -> + if (response.code != 200 || response.body == null) { + return@use continuation.resume(null) + } - val responseString = response.body?.string().orEmpty() - try { + val responseString = response.body?.string().orEmpty() val chatCompletion = json.decodeFromString(ChatCompletion.serializer(), responseString) Log.d("OpenAiRepository", "chatCompletion: $chatCompletion") continuation.resume(chatCompletion) - } catch (e: Exception) { - continuation.resume(null) } + } catch (e: Exception) { + Log.e("OpenAiRepository", "Error fetching chat completion", e) + continuation.resume(null) } } @@ -201,6 +205,7 @@ class OpenAiRepository : KoinComponent { responseData.candidates.firstOrNull()?.content?.parts?.firstOrNull()?.text ?: "No content available" } catch (exception: Exception) { + Log.e("OpenAiRepository", "Error querying Gemini API", exception) "something wrong" } } diff --git a/app/src/main/java/info/plateaukao/einkbro/service/TranslateRepository.kt b/app/src/main/java/info/plateaukao/einkbro/service/TranslateRepository.kt index 9e270779..288848dc 100644 --- a/app/src/main/java/info/plateaukao/einkbro/service/TranslateRepository.kt +++ b/app/src/main/java/info/plateaukao/einkbro/service/TranslateRepository.kt @@ -138,17 +138,17 @@ class TranslateRepository : KoinComponent { .post(body) .build() - return withContext(IO) { - client.newCall(request).execute().use { response -> - val body = response.body?.string() ?: return@use null - try { + try { + return withContext(IO) { + client.newCall(request).execute().use { response -> + val body = response.body?.string() ?: return@use null val result = JSONObject(body).getJSONObject("result") return@use result.getJSONArray("texts").getJSONObject(0).getString("text") - } catch (e: Exception) { - Log.d("TranslateRepository", "deepLTranslate: $e") - return@use null } } + } catch (e: Exception) { + Log.d("TranslateRepository", "deepLTranslate: $e") + return null } } @@ -176,10 +176,10 @@ class TranslateRepository : KoinComponent { .addHeader("Referer", "https://translate.google.com/") .build() - client.newCall(request).execute().use { response -> - val body = response.body?.string() ?: return@use null + try { + client.newCall(request).execute().use { response -> + val body = response.body?.string() ?: return@use null - try { val result = StringBuilder() val array: JSONArray = JSONArray(body).get(0) as JSONArray for (i in 0 until array.length()) { @@ -187,9 +187,10 @@ class TranslateRepository : KoinComponent { result.append(item[0].toString()) } result.toString() - } catch (e: Exception) { - null } + } catch (e: Exception) { + Log.e("TranslateRepository", "gTranslateWithApi: $e") + "" } } } @@ -214,17 +215,18 @@ class TranslateRepository : KoinComponent { ) .build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) return@use null + try { + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) return@use null - try { val body = JSONObject(response.body?.string() ?: return@use null) body.getJSONObject("message") .getJSONObject("result") .getString("translatedText") - } catch (e: Exception) { - null } + } catch (e: Exception) { + Log.e("TranslateRepository", "pTranslate: $e") + null } } } @@ -342,15 +344,16 @@ class TranslateRepository : KoinComponent { ) .build() - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) return@use null + try { + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) return@use null - try { val body = JSONObject(response.body?.string() ?: return@use null) body.getString("langCode") - } catch (e: Exception) { - null } + } catch (e: Exception) { + Log.d("TranslateRepository", "pDetectLanguage: $e") + null } } }