Commit f1fb7404 authored by topjohnwu's avatar topjohnwu

Catch exceptions when loading remote modules

Fix #3004
parent fc67c019
...@@ -5,6 +5,7 @@ import com.topjohnwu.magisk.core.Const ...@@ -5,6 +5,7 @@ import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.model.module.Repo import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.data.database.RepoDao import com.topjohnwu.magisk.data.database.RepoDao
import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.GithubApiServices
import com.topjohnwu.magisk.ktx.synchronized
import kotlinx.coroutines.* import kotlinx.coroutines.*
import timber.log.Timber import timber.log.Timber
import java.net.HttpURLConnection import java.net.HttpURLConnection
...@@ -64,6 +65,7 @@ class RepoUpdater( ...@@ -64,6 +65,7 @@ class RepoUpdater(
page: Int = 1, page: Int = 1,
etag: String = "" etag: String = ""
): PageResult = coroutineScope { ): PageResult = coroutineScope {
runCatching {
val result = api.fetchRepos(page, etag) val result = api.fetchRepos(page, etag)
result.run { result.run {
if (code() == HttpURLConnection.HTTP_NOT_MODIFIED) if (code() == HttpURLConnection.HTTP_NOT_MODIFIED)
...@@ -84,13 +86,18 @@ class RepoUpdater( ...@@ -84,13 +86,18 @@ class RepoUpdater(
repoLoad.await() repoLoad.await()
return@coroutineScope next.await() return@coroutineScope next.await()
} }
}.getOrElse {
Timber.e(it)
PageResult.ERROR
}
} }
suspend operator fun invoke(forced: Boolean) = withContext(Dispatchers.IO) { suspend operator fun invoke(forced: Boolean) = withContext(Dispatchers.IO) {
val cached = Collections.synchronizedSet(HashSet(repoDB.repoIDList)) val cached = HashSet(repoDB.repoIDList).synchronized()
when (loadPage(cached, etag = repoDB.etagKey)) { when (loadPage(cached, etag = repoDB.etagKey)) {
PageResult.CACHED -> if (forced) forcedReload(cached) PageResult.CACHED -> if (forced) forcedReload(cached)
PageResult.SUCCESS -> repoDB.removeRepos(cached) PageResult.SUCCESS -> repoDB.removeRepos(cached)
PageResult.ERROR -> Unit
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment