Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch all exception when loading URL for offline tiles. #2270

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.google.android.ground.ui.map.gms.mog
import android.util.LruCache
import com.google.android.ground.persistence.remote.RemoteStorageManager
import com.google.android.ground.ui.map.Bounds
import java.io.FileNotFoundException
import java.io.InputStream
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
Expand Down Expand Up @@ -187,11 +186,11 @@ class MogClient(val collection: MogCollection, val remoteStorageManager: RemoteS

private suspend fun MogPathOrUrl.toUrl(): MogUrl? =
if (startsWith("/")) {
nullIfNotFound { remoteStorageManager.getDownloadUrl(this).toString() }
nullIfError { remoteStorageManager.getDownloadUrl(this).toString() }
} else this

private fun MogUrl.readMetadata(mogBounds: TileCoordinates): MogMetadata? =
nullIfNotFound { UrlInputStream(this) }?.use { this.readMogMetadataAndClose(mogBounds, it) }
nullIfError { UrlInputStream(this) }?.use { this.readMogMetadataAndClose(mogBounds, it) }

/** Reads the metadata from the specified input stream. */
private fun MogUrl.readMogMetadataAndClose(
Expand All @@ -208,9 +207,10 @@ class MogClient(val collection: MogCollection, val remoteStorageManager: RemoteS
}
}

private inline fun <T> nullIfNotFound(fn: () -> T) =
private inline fun <T> nullIfError(fn: () -> T) =
try {
fn()
} catch (_: FileNotFoundException) {
} catch (e: Exception) {
Timber.e(e)
null
}