Handle retrofit errors

This commit is contained in:
topjohnwu 2020-10-20 03:03:40 -07:00
parent ce6cceae8b
commit fa2dbe981e
3 changed files with 48 additions and 28 deletions

View File

@ -44,11 +44,12 @@ data class OnlineModule(
@Throws(IllegalRepoException::class) @Throws(IllegalRepoException::class)
suspend fun load() { suspend fun load() {
val props = svc.fetchString(prop_url) try {
props.split("\\n".toRegex()).dropLastWhile { it.isEmpty() }.runCatching { val rawProps = svc.fetchString(prop_url)
parseProps(this) val props = rawProps.split("\\n".toRegex()).dropLastWhile { it.isEmpty() }
}.onFailure { parseProps(props)
throw IllegalRepoException("Repo [$id] parse error: ", it) } catch (e: Exception) {
throw IllegalRepoException("Repo [$id] parse error:", e)
} }
if (versionCode < 0) { if (versionCode < 0) {

View File

@ -20,17 +20,18 @@ class RepoUpdater(
val cachedMap = HashMap<String, Date>().also { map -> val cachedMap = HashMap<String, Date>().also { map ->
repoDB.getModuleStubs().forEach { map[it.id] = Date(it.last_update) } repoDB.getModuleStubs().forEach { map[it.id] = Date(it.last_update) }
}.synchronized() }.synchronized()
val info = svc.fetchRepoInfo() svc.fetchRepoInfo()?.also { info ->
coroutineScope { coroutineScope {
info.modules.forEach { info.modules.forEach {
launch { launch {
val lastUpdated = cachedMap.remove(it.id) val lastUpdated = cachedMap.remove(it.id)
if (forced || lastUpdated?.before(Date(it.last_update)) != false) { if (forced || lastUpdated?.before(Date(it.last_update)) != false) {
try { try {
val repo = OnlineModule(it).apply { load() } val repo = OnlineModule(it).apply { load() }
repoDB.addModule(repo) repoDB.addModule(repo)
} catch (e: OnlineModule.IllegalRepoException) { } catch (e: OnlineModule.IllegalRepoException) {
Timber.e(e) Timber.e(e)
}
} }
} }
} }

View File

@ -10,7 +10,6 @@ import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.* import com.topjohnwu.magisk.core.model.*
import com.topjohnwu.magisk.data.network.* import com.topjohnwu.magisk.data.network.*
import okhttp3.ResponseBody
import retrofit2.HttpException import retrofit2.HttpException
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
@ -46,10 +45,10 @@ class NetworkService(
} }
// UpdateInfo // UpdateInfo
suspend fun fetchStableUpdate() = pages.fetchStableUpdate() private suspend fun fetchStableUpdate() = pages.fetchStableUpdate()
suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate() private suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate()
suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url) private suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url)
suspend fun fetchCanaryUpdate(): UpdateInfo { private suspend fun fetchCanaryUpdate(): UpdateInfo {
val sha = fetchCanaryVersion() val sha = fetchCanaryVersion()
val info = jsd.fetchCanaryUpdate(sha) val info = jsd.fetchCanaryUpdate(sha)
@ -67,18 +66,37 @@ class NetworkService(
) )
} }
private inline fun <T> safe(factory: () -> T): T? {
return try {
factory()
} catch (e: HttpException) {
Timber.e(e)
null
}
}
private inline fun <T> wrap(factory: () -> T): T {
return try {
factory()
} catch (e: HttpException) {
throw IOException(e)
}
}
// Modules related // 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 // Fetch files
suspend fun fetchSafetynet() = jsd.fetchSafetynet() suspend fun fetchSafetynet() = wrap { jsd.fetchSafetynet() }
suspend fun fetchBootctl() = jsd.fetchBootctl() suspend fun fetchBootctl() = wrap { jsd.fetchBootctl() }
suspend fun fetchInstaller(): ResponseBody { suspend fun fetchInstaller() = wrap {
val sha = fetchMainVersion() val sha = fetchMainVersion()
return jsd.fetchInstaller(sha) jsd.fetchInstaller(sha)
} }
suspend fun fetchFile(url: String) = raw.fetchFile(url) suspend fun fetchFile(url: String) = wrap { raw.fetchFile(url) }
suspend fun fetchString(url: String) = raw.fetchString(url) suspend fun fetchString(url: String) = wrap { raw.fetchString(url) }
private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha
private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha