From 4e82db6dbca4385264440834a71ed98c3fa11667 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Mon, 20 Jan 2025 17:12:59 +0100 Subject: [PATCH] Fix IOException Catch exceptions in a better way --- .../AppPrivacyInfoRepositoryImpl.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/data/exodus/repositories/AppPrivacyInfoRepositoryImpl.kt b/app/src/main/java/foundation/e/apps/data/exodus/repositories/AppPrivacyInfoRepositoryImpl.kt index 0584b8c8c..e5338ab91 100644 --- a/app/src/main/java/foundation/e/apps/data/exodus/repositories/AppPrivacyInfoRepositoryImpl.kt +++ b/app/src/main/java/foundation/e/apps/data/exodus/repositories/AppPrivacyInfoRepositoryImpl.kt @@ -33,7 +33,8 @@ import java.lang.reflect.Modifier import javax.inject.Inject import javax.inject.Singleton import foundation.e.apps.data.Result -import okio.IOException +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @Singleton class AppPrivacyInfoRepositoryImpl @Inject constructor( @@ -60,7 +61,7 @@ class AppPrivacyInfoRepositoryImpl @Inject constructor( return Result.success(buildPrivacyInfo(reports.first())) } - private fun fetchReports(packageName: String) : List { + private suspend fun fetchReports(packageName: String) : List { val requestBody = mapOf( "type" to "application", "query" to packageName, @@ -73,20 +74,19 @@ class AppPrivacyInfoRepositoryImpl @Inject constructor( .post(jsonBody.toRequestBody("application/json".toMediaType())) .build() - try { - okHttpClient.newCall(request).execute().use { response -> + return withContext(Dispatchers.IO) { + var result: List = emptyList() + try { + val response = okHttpClient.newCall(request).execute() if (response.isSuccessful) { val responseBody = response.body?.string() - return parseReports(responseBody ?: "") - } else { - throw IllegalStateException("Failed to fetch reports") + result = parseReports(responseBody ?: "") } + } catch (exception: Exception) { + exception.printStackTrace() } - } catch (exception: IOException) { - exception.printStackTrace() + result } - - return emptyList() } private fun parseReports(response: String): List { -- GitLab