From ddabd8375bda8880dd4000fc4d2a8b3248f1db00 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 19 Sep 2022 15:16:15 -0300 Subject: [PATCH] Explicitly bubble up NotFoundException to UI Otherwise, the UI might not catch it and crash. This can happen if a file is not available anymore on all mirrors. --- .../full/java/org/fdroid/fdroid/nearby/SwapService.java | 3 ++- .../java/org/fdroid/fdroid/net/DownloaderService.java | 3 ++- .../androidMain/kotlin/org/fdroid/download/Downloader.kt | 9 +++++++-- .../kotlin/org/fdroid/download/HttpDownloader.kt | 2 +- .../kotlin/org/fdroid/download/HttpDownloaderV2.kt | 4 ++-- .../commonMain/kotlin/org/fdroid/download/HttpManager.kt | 1 + 6 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java index 2549d41fc..88a8ffa00 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/SwapService.java @@ -20,6 +20,7 @@ import androidx.lifecycle.MutableLiveData; import org.fdroid.database.Repository; import org.fdroid.download.Downloader; +import org.fdroid.download.NotFoundException; import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.NotificationHelper; import org.fdroid.fdroid.Preferences; @@ -129,7 +130,7 @@ public class SwapService extends Service { } private void updateRepo(@NonNull Peer peer, Repository repo) - throws IOException, InterruptedException, SigningException { + throws IOException, InterruptedException, SigningException, NotFoundException { Uri uri = Uri.parse(repo.getAddress()).buildUpon().appendPath("index-v1.jar").build(); FileV2 indexFile = FileV2.fromPath("/index-v1.jar"); File swapJarFile = diff --git a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java index bef7b389d..8e9923fd3 100644 --- a/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java +++ b/app/src/main/java/org/fdroid/fdroid/net/DownloaderService.java @@ -36,6 +36,7 @@ import android.util.LogPrinter; import org.fdroid.database.FDroidDatabase; import org.fdroid.database.Repository; import org.fdroid.download.Downloader; +import org.fdroid.download.NotFoundException; import org.fdroid.fdroid.BuildConfig; import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.ProgressListener; @@ -284,7 +285,7 @@ public class DownloaderService extends Service { sendBroadcast(uri, DownloaderService.ACTION_INTERRUPTED, localFile, repoId, canonicalUrl); } catch (ConnectException | HttpRetryException | NoRouteToHostException | SocketTimeoutException | SSLHandshakeException | SSLKeyException | SSLPeerUnverifiedException | SSLProtocolException - | ProtocolException | UnknownHostException e) { + | ProtocolException | UnknownHostException | NotFoundException e) { // if the above list of exceptions changes, also change it in IndexV1Updater.update() Log.e(TAG, "CONNECTION_FAILED: " + e.getLocalizedMessage()); sendBroadcast(uri, DownloaderService.ACTION_CONNECTION_FAILED, localFile, repoId, canonicalUrl); diff --git a/libs/download/src/androidMain/kotlin/org/fdroid/download/Downloader.kt b/libs/download/src/androidMain/kotlin/org/fdroid/download/Downloader.kt index 086b5e9f7..7510b198c 100644 --- a/libs/download/src/androidMain/kotlin/org/fdroid/download/Downloader.kt +++ b/libs/download/src/androidMain/kotlin/org/fdroid/download/Downloader.kt @@ -43,7 +43,7 @@ public abstract class Downloader constructor( * Call this to start the download. * Never call this more than once. Create a new [Downloader], if you need to download again! */ - @Throws(IOException::class, InterruptedException::class) + @Throws(IOException::class, InterruptedException::class, NotFoundException::class) public abstract fun download() @Throws(IOException::class, NotFoundException::class) @@ -92,7 +92,12 @@ public abstract class Downloader constructor( } @Suppress("BlockingMethodInNonBlockingContext") - @Throws(InterruptedException::class, IOException::class, NoResumeException::class) + @Throws( + InterruptedException::class, + IOException::class, + NoResumeException::class, + NotFoundException::class, + ) protected suspend fun downloadFromBytesReceiver(isResume: Boolean) { try { val messageDigest: MessageDigest? = if (indexFile.sha256 == null) null else { diff --git a/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloader.kt b/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloader.kt index 3f88b5854..7c011a470 100644 --- a/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloader.kt +++ b/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloader.kt @@ -51,7 +51,7 @@ public class HttpDownloader constructor( throw NotImplementedError("Use getInputStreamSuspend instead.") } - @Throws(IOException::class, NoResumeException::class) + @Throws(IOException::class, NoResumeException::class, NotFoundException::class) protected override suspend fun getBytes(resumable: Boolean, receiver: BytesReceiver) { val skipBytes = if (resumable) outputFile.length() else null return try { diff --git a/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloaderV2.kt b/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloaderV2.kt index 6dce71ea9..b3c283333 100644 --- a/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloaderV2.kt +++ b/libs/download/src/androidMain/kotlin/org/fdroid/download/HttpDownloaderV2.kt @@ -46,7 +46,7 @@ public class HttpDownloaderV2 constructor( throw NotImplementedError("Use getInputStreamSuspend instead.") } - @Throws(IOException::class, NoResumeException::class) + @Throws(IOException::class, NoResumeException::class, NotFoundException::class) protected override suspend fun getBytes(resumable: Boolean, receiver: BytesReceiver) { val skipBytes = if (resumable) outputFile.length() else null return try { @@ -57,7 +57,7 @@ public class HttpDownloaderV2 constructor( } } - @Throws(IOException::class, InterruptedException::class) + @Throws(IOException::class, InterruptedException::class, NotFoundException::class) public override fun download() { var resumable = false val fileLength = outputFile.length() diff --git a/libs/download/src/commonMain/kotlin/org/fdroid/download/HttpManager.kt b/libs/download/src/commonMain/kotlin/org/fdroid/download/HttpManager.kt index 631cb75fd..e5eba4eea 100644 --- a/libs/download/src/commonMain/kotlin/org/fdroid/download/HttpManager.kt +++ b/libs/download/src/commonMain/kotlin/org/fdroid/download/HttpManager.kt @@ -91,6 +91,7 @@ public open class HttpManager @JvmOverloads constructor( * This is useful for checking if the repository index has changed before downloading it again. * However, due to non-standard ETags on mirrors, change detection is unreliable. */ + @Throws(NotFoundException::class) public suspend fun head(request: DownloadRequest, eTag: String? = null): HeadInfo? { val response: HttpResponse = try { mirrorChooser.mirrorRequest(request) { mirror, url ->