fix(extension/repo): Don't throw an error if it's a connection error

Fixes GH-130
This commit is contained in:
Ahmad Ansori Palembani 2024-07-27 20:22:24 +07:00
parent fb92d1dcb6
commit 9d4dc5f351
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
6 changed files with 37 additions and 4 deletions

View file

@ -0,0 +1,6 @@
package yokai.domain.extension.repo.exception
import java.io.IOException
class FetchExtensionRepoException(throwable: Throwable) :
IOException("Failed to retrieve Extension Repo Details", throwable)

View file

@ -5,6 +5,7 @@ import eu.kanade.tachiyomi.network.NetworkHelper
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import yokai.domain.extension.repo.ExtensionRepoRepository import yokai.domain.extension.repo.ExtensionRepoRepository
import yokai.domain.extension.repo.exception.FetchExtensionRepoException
import yokai.domain.extension.repo.exception.SaveExtensionRepoException import yokai.domain.extension.repo.exception.SaveExtensionRepoException
import yokai.domain.extension.repo.model.ExtensionRepo import yokai.domain.extension.repo.model.ExtensionRepo
import yokai.domain.extension.repo.service.ExtensionRepoService import yokai.domain.extension.repo.service.ExtensionRepoService
@ -27,7 +28,11 @@ class CreateExtensionRepo(
} }
val baseUrl = repoUrl.removeSuffix("/index.min.json") val baseUrl = repoUrl.removeSuffix("/index.min.json")
return extensionRepoService.fetchRepoDetails(baseUrl)?.let { insert(it) } ?: Result.InvalidUrl return try {
extensionRepoService.fetchRepoDetails(baseUrl)?.let { insert(it) } ?: Result.InvalidUrl
} catch (e: FetchExtensionRepoException) {
Result.RepoFetchFailed
}
} }
private suspend fun insert(repo: ExtensionRepo): Result { private suspend fun insert(repo: ExtensionRepo): Result {
@ -74,6 +79,7 @@ class CreateExtensionRepo(
data class DuplicateFingerprint(val oldRepo: ExtensionRepo, val newRepo: ExtensionRepo) : Result data class DuplicateFingerprint(val oldRepo: ExtensionRepo, val newRepo: ExtensionRepo) : Result
data object InvalidUrl : Result data object InvalidUrl : Result
data object RepoAlreadyExists : Result data object RepoAlreadyExists : Result
data object RepoFetchFailed : Result
data object Success : Result data object Success : Result
data object Error : Result data object Error : Result
} }

View file

@ -1,10 +1,12 @@
package yokai.domain.extension.repo.interactor package yokai.domain.extension.repo.interactor
import co.touchlab.kermit.Logger
import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.NetworkHelper
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import yokai.domain.extension.repo.ExtensionRepoRepository import yokai.domain.extension.repo.ExtensionRepoRepository
import yokai.domain.extension.repo.exception.FetchExtensionRepoException
import yokai.domain.extension.repo.model.ExtensionRepo import yokai.domain.extension.repo.model.ExtensionRepo
import yokai.domain.extension.repo.service.ExtensionRepoService import yokai.domain.extension.repo.service.ExtensionRepoService
@ -21,7 +23,17 @@ class UpdateExtensionRepo(
} }
suspend fun await(repo: ExtensionRepo) { suspend fun await(repo: ExtensionRepo) {
val newRepo = extensionRepoService.fetchRepoDetails(repo.baseUrl) ?: return val newRepo = try {
extensionRepoService.fetchRepoDetails(repo.baseUrl) ?: return
} catch (e: Exception) {
when (e) {
is FetchExtensionRepoException -> {
Logger.e(e) { "Failed to fetch repo details" }
return
}
else -> throw e
}
}
if ( if (
repo.signingKeyFingerprint.startsWith("NOFINGERPRINT") || repo.signingKeyFingerprint.startsWith("NOFINGERPRINT") ||
repo.signingKeyFingerprint == newRepo.signingKeyFingerprint repo.signingKeyFingerprint == newRepo.signingKeyFingerprint

View file

@ -12,7 +12,9 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.jsonPrimitive
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import yokai.domain.extension.repo.exception.FetchExtensionRepoException
import yokai.domain.extension.repo.model.ExtensionRepo import yokai.domain.extension.repo.model.ExtensionRepo
import java.net.UnknownHostException
class ExtensionRepoService( class ExtensionRepoService(
private val client: OkHttpClient, private val client: OkHttpClient,
@ -35,8 +37,12 @@ class ExtensionRepoService(
response["meta"] response["meta"]
?.jsonObject ?.jsonObject
?.let { jsonToExtensionRepo(baseUrl = repo, it) } ?.let { jsonToExtensionRepo(baseUrl = repo, it) }
} catch (_: HttpException) { } catch (e: Exception) {
null when (e) {
is HttpException -> null
is UnknownHostException -> throw FetchExtensionRepoException(e)
else -> throw e
}
} }
} }
} }

View file

@ -54,6 +54,7 @@ class ExtensionRepoViewModel :
is CreateExtensionRepo.Result.Success -> internalEvent.value = ExtensionRepoEvent.Success is CreateExtensionRepo.Result.Success -> internalEvent.value = ExtensionRepoEvent.Success
is CreateExtensionRepo.Result.Error -> internalEvent.value = ExtensionRepoEvent.InvalidUrl is CreateExtensionRepo.Result.Error -> internalEvent.value = ExtensionRepoEvent.InvalidUrl
is CreateExtensionRepo.Result.RepoAlreadyExists -> internalEvent.value = ExtensionRepoEvent.RepoAlreadyExists is CreateExtensionRepo.Result.RepoAlreadyExists -> internalEvent.value = ExtensionRepoEvent.RepoAlreadyExists
is CreateExtensionRepo.Result.RepoFetchFailed -> internalEvent.value = ExtensionRepoEvent.RepoFetchFailed
is CreateExtensionRepo.Result.DuplicateFingerprint -> { is CreateExtensionRepo.Result.DuplicateFingerprint -> {
internalEvent.value = ExtensionRepoEvent.ShowDialog(RepoDialog.Conflict(result.oldRepo, result.newRepo)) internalEvent.value = ExtensionRepoEvent.ShowDialog(RepoDialog.Conflict(result.oldRepo, result.newRepo))
} }
@ -93,6 +94,7 @@ sealed class ExtensionRepoEvent {
sealed class LocalizedMessage(val stringRes: StringResource) : ExtensionRepoEvent() sealed class LocalizedMessage(val stringRes: StringResource) : ExtensionRepoEvent()
data object InvalidUrl : LocalizedMessage(MR.strings.invalid_repo_url) data object InvalidUrl : LocalizedMessage(MR.strings.invalid_repo_url)
data object RepoAlreadyExists : LocalizedMessage(MR.strings.repo_already_exists) data object RepoAlreadyExists : LocalizedMessage(MR.strings.repo_already_exists)
data object RepoFetchFailed : LocalizedMessage(MR.strings.repo_fetch_failed)
data class ShowDialog(val dialog: RepoDialog) : ExtensionRepoEvent() data class ShowDialog(val dialog: RepoDialog) : ExtensionRepoEvent()
data object NoOp : ExtensionRepoEvent() data object NoOp : ExtensionRepoEvent()
data object Success : ExtensionRepoEvent() data object Success : ExtensionRepoEvent()

View file

@ -880,6 +880,7 @@
<string name="action_add_repo">Add repo</string> <string name="action_add_repo">Add repo</string>
<string name="invalid_repo_url">Invalid repo url</string> <string name="invalid_repo_url">Invalid repo url</string>
<string name="repo_already_exists">Repo already exists!</string> <string name="repo_already_exists">Repo already exists!</string>
<string name="repo_fetch_failed">Failed to retrieve repo details. Please try again later</string>
<string name="information_empty_repos">You haven\'t added any repos yet.</string> <string name="information_empty_repos">You haven\'t added any repos yet.</string>
<string name="confirm_delete_repo_title">Delete repo?</string> <string name="confirm_delete_repo_title">Delete repo?</string>
<string name="confirm_delete_repo">Are you sure you wish to delete the repo \"%s\"?</string> <string name="confirm_delete_repo">Are you sure you wish to delete the repo \"%s\"?</string>