From fa2dbe981e11185eff8fd73198180ae8b2fb5118 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 20 Oct 2020 03:03:40 -0700 Subject: [PATCH] Handle retrofit errors --- .../magisk/core/model/module/OnlineModule.kt | 11 ++--- .../magisk/core/tasks/RepoUpdater.kt | 23 +++++----- .../magisk/data/repository/NetworkService.kt | 42 +++++++++++++------ 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/module/OnlineModule.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/module/OnlineModule.kt index d39140987..9a1491a1b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/module/OnlineModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/module/OnlineModule.kt @@ -44,11 +44,12 @@ data class OnlineModule( @Throws(IllegalRepoException::class) suspend fun load() { - val props = svc.fetchString(prop_url) - props.split("\\n".toRegex()).dropLastWhile { it.isEmpty() }.runCatching { - parseProps(this) - }.onFailure { - throw IllegalRepoException("Repo [$id] parse error: ", it) + try { + val rawProps = svc.fetchString(prop_url) + val props = rawProps.split("\\n".toRegex()).dropLastWhile { it.isEmpty() } + parseProps(props) + } catch (e: Exception) { + throw IllegalRepoException("Repo [$id] parse error:", e) } if (versionCode < 0) { diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/RepoUpdater.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/RepoUpdater.kt index d162bdff2..878f10b68 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/RepoUpdater.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/RepoUpdater.kt @@ -20,17 +20,18 @@ class RepoUpdater( val cachedMap = HashMap().also { map -> repoDB.getModuleStubs().forEach { map[it.id] = Date(it.last_update) } }.synchronized() - val info = svc.fetchRepoInfo() - coroutineScope { - info.modules.forEach { - launch { - val lastUpdated = cachedMap.remove(it.id) - if (forced || lastUpdated?.before(Date(it.last_update)) != false) { - try { - val repo = OnlineModule(it).apply { load() } - repoDB.addModule(repo) - } catch (e: OnlineModule.IllegalRepoException) { - Timber.e(e) + svc.fetchRepoInfo()?.also { info -> + coroutineScope { + info.modules.forEach { + launch { + val lastUpdated = cachedMap.remove(it.id) + if (forced || lastUpdated?.before(Date(it.last_update)) != false) { + try { + val repo = OnlineModule(it).apply { load() } + repoDB.addModule(repo) + } catch (e: OnlineModule.IllegalRepoException) { + Timber.e(e) + } } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt b/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt index 4c28d0ebc..e1615ee81 100644 --- a/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt +++ b/app/src/main/java/com/topjohnwu/magisk/data/repository/NetworkService.kt @@ -10,7 +10,6 @@ import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.model.* import com.topjohnwu.magisk.data.network.* -import okhttp3.ResponseBody import retrofit2.HttpException import timber.log.Timber import java.io.IOException @@ -46,10 +45,10 @@ class NetworkService( } // UpdateInfo - suspend fun fetchStableUpdate() = pages.fetchStableUpdate() - suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate() - suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url) - suspend fun fetchCanaryUpdate(): UpdateInfo { + private suspend fun fetchStableUpdate() = pages.fetchStableUpdate() + private suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate() + private suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url) + private suspend fun fetchCanaryUpdate(): UpdateInfo { val sha = fetchCanaryVersion() val info = jsd.fetchCanaryUpdate(sha) @@ -67,18 +66,37 @@ class NetworkService( ) } + private inline fun safe(factory: () -> T): T? { + return try { + factory() + } catch (e: HttpException) { + Timber.e(e) + null + } + } + + private inline fun wrap(factory: () -> T): T { + return try { + factory() + } catch (e: HttpException) { + throw IOException(e) + } + } + // Modules related - suspend fun fetchRepoInfo(url: String = Const.Url.OFFICIAL_REPO) = raw.fetchRepoInfo(url) + suspend fun fetchRepoInfo(url: String = Const.Url.OFFICIAL_REPO) = safe { + raw.fetchRepoInfo(url) + } // Fetch files - suspend fun fetchSafetynet() = jsd.fetchSafetynet() - suspend fun fetchBootctl() = jsd.fetchBootctl() - suspend fun fetchInstaller(): ResponseBody { + suspend fun fetchSafetynet() = wrap { jsd.fetchSafetynet() } + suspend fun fetchBootctl() = wrap { jsd.fetchBootctl() } + suspend fun fetchInstaller() = wrap { val sha = fetchMainVersion() - return jsd.fetchInstaller(sha) + jsd.fetchInstaller(sha) } - suspend fun fetchFile(url: String) = raw.fetchFile(url) - suspend fun fetchString(url: String) = raw.fetchString(url) + suspend fun fetchFile(url: String) = wrap { raw.fetchFile(url) } + suspend fun fetchString(url: String) = wrap { raw.fetchString(url) } private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha