Skip to content

Commit

Permalink
fix: crashes when there's no network
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Oct 15, 2024
1 parent 7821389 commit d28acc7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,9 +60,9 @@ class EinkBroApplication : Application() {

instance = this

Thread.setDefaultUncaughtExceptionHandler(
CustomExceptionHandler(Thread.getDefaultUncaughtExceptionHandler())
)
// Thread.setDefaultUncaughtExceptionHandler(
// CustomExceptionHandler(Thread.getDefaultUncaughtExceptionHandler())
// )
}

override fun onTerminate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand All @@ -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
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -176,20 +176,21 @@ 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()) {
val item = array[i] as JSONArray
result.append(item[0].toString())
}
result.toString()
} catch (e: Exception) {
null
}
} catch (e: Exception) {
Log.e("TranslateRepository", "gTranslateWithApi: $e")
""
}
}
}
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit d28acc7

Please sign in to comment.