Modern Apps git

← Modern-Apps.git

musicbrainz: let the server decide if a retry can help

Commit
33894be01bfeda80aa5f94cd367648d3f1ec8c7e
Author
vayun-mathur <[email protected]>
When
2026-08-20 22:32 UTC (3 months ago)
Parents
b365e4e

Diff

musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.ktindex e6fbc367e5..68bed8a444 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt@@ -14,6 +14,8 @@ internal data class NotReadyBody(     val state: String = "",     val progress: Float? = null,     val detail: String? = null,+    /** Whether waiting and asking again will ever produce a catalogue. */+    val retryable: Boolean? = null, )  /**@@ -25,14 +27,19 @@ internal data class NotReadyBody(  * a fault at their end.  *  * [reason] is the server's own explanation, carried only for [ABSENT] - a catalogue that is- * absent is not always coming. The host may be unable to build one at all, in which case- * telling the user to try again shortly would be a promise nothing is going to keep.- * [BUILDING] needs no explanation because it genuinely does resolve on its own.+ * absent may never arrive, and [BUILDING] needs no explanation because it resolves on its own.+ *+ * [retryable] is the server's word on whether asking again will ever help, and it is NOT the+ * same question as whether a catalogue is absent: a first boot before the scheduler has run,+ * and a failed build queued for the next check, are both absent yet worth retrying, while a+ * host that cannot fit a build never will be. It is reported rather than inferred because only+ * the server knows which of those it is.  */ class CatalogueNotReadyException(     message: String,     val state: String? = null,     val reason: String? = null,+    val retryable: Boolean = true, ) : IOException(message) {     companion object {         const val ABSENT = "absent"@@ -96,11 +103,16 @@ object MusicBrainzApi {     internal fun failureFor(status: Int, body: String): IOException? = when {         status == HttpURLConnection.HTTP_UNAVAILABLE -> {             val parsed = runCatching { json.decodeFromString<NotReadyBody>(body) }.getOrNull()+            val reason = parsed?.detail?.ifBlank { null }+                ?.takeIf { parsed.state == CatalogueNotReadyException.ABSENT }             CatalogueNotReadyException(                 message = "HTTP 503: ${body.take(200)}",                 state = parsed?.state?.ifBlank { null },-                reason = parsed?.detail?.ifBlank { null }-                    ?.takeIf { parsed.state == CatalogueNotReadyException.ABSENT },+                reason = reason,+                // Falls back to inferring it from the reason when the server does not say, so a+                // body without the field behaves as it did before the field existed rather than+                // offering a retry that cannot work.+                retryable = parsed?.retryable ?: (reason == null),             )         }         status in 200..299 -> null
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.ktindex c4b8cf3183..6348a5a50c 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt@@ -92,6 +92,8 @@ data class SearchUiState(     val notReady: Boolean = false,     /** The server's reason, when a missing catalogue is not simply on its way. */     val notReadyReason: String? = null,+    /** Whether asking again could ever produce a catalogue. The server decides this. */+    val notReadyRetryable: Boolean = true,     val hasSearched: Boolean = false,     val artists: List<ArtistRow> = emptyList(),     val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -105,6 +107,8 @@ data class ArtistUiState(     val notReady: Boolean = false,     /** The server's reason, when a missing catalogue is not simply on its way. */     val notReadyReason: String? = null,+    /** Whether asking again could ever produce a catalogue. The server decides this. */+    val notReadyRetryable: Boolean = true,     val name: String = "",     val subtitle: String? = null,     val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -117,6 +121,8 @@ data class ReleaseGroupUiState(     val notReady: Boolean = false,     /** The server's reason, when a missing catalogue is not simply on its way. */     val notReadyReason: String? = null,+    /** Whether asking again could ever produce a catalogue. The server decides this. */+    val notReadyRetryable: Boolean = true,     val title: String = "",     val artist: String = "",     val coverUrl: String? = null,@@ -130,6 +136,8 @@ data class ReleaseUiState(     val notReady: Boolean = false,     /** The server's reason, when a missing catalogue is not simply on its way. */     val notReadyReason: String? = null,+    /** Whether asking again could ever produce a catalogue. The server decides this. */+    val notReadyRetryable: Boolean = true,     val id: String = "",     val title: String = "",     val artist: String = "",
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.ktindex 0877288f0d..60f445d203 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt@@ -145,6 +145,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                 error = null,                 notReady = false,                 notReadyReason = null,+                notReadyRetryable = true,                 hasSearched = true,             )             try {@@ -187,6 +188,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,                     notReadyReason = e.notReadyReason(),+                    notReadyRetryable = e.notReadyRetryable(),                 )             }         }@@ -240,6 +242,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,                     notReadyReason = e.notReadyReason(),+                    notReadyRetryable = e.notReadyRetryable(),                 )             }         }@@ -298,6 +301,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,                     notReadyReason = e.notReadyReason(),+                    notReadyRetryable = e.notReadyRetryable(),                 )             }         }@@ -321,6 +325,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,                     notReadyReason = e.notReadyReason(),+                    notReadyRetryable = e.notReadyRetryable(),                 )             }         }@@ -597,6 +602,10 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat     private fun Exception.notReadyReason(): String? =         (this as? CatalogueNotReadyException)?.reason +    /** The server's word on whether retrying can help; assume it can when it did not say. */+    private fun Exception.notReadyRetryable(): Boolean =+        (this as? CatalogueNotReadyException)?.retryable ?: true+     private fun Exception.readableMessage(): String =         message?.takeIf { it.isNotBlank() }?.take(200) ?: "Something went wrong" 
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.ktindex 57eef3e1be..8c700beac7 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt@@ -54,6 +54,7 @@ fun ArtistScreen(                 error = state.error,                 notReady = state.notReady,                 notReadyReason = state.notReadyReason,+                notReadyRetryable = state.notReadyRetryable,                 modifier = Modifier.fillMaxSize().padding(padding),             )             state.releaseGroups.isEmpty() -> EmptyState(
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.ktindex f653349325..a22d70f2c4 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt@@ -65,6 +65,7 @@ fun ReleaseGroupScreen(                 error = state.error,                 notReady = state.notReady,                 notReadyReason = state.notReadyReason,+                notReadyRetryable = state.notReadyRetryable,                 modifier = Modifier.fillMaxSize().padding(padding),             )             else -> LazyColumn(
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.ktindex de8473b97f..bfff9d63ab 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt@@ -75,6 +75,7 @@ fun ReleaseScreen(                 error = state.error,                 notReady = state.notReady,                 notReadyReason = state.notReadyReason,+                notReadyRetryable = state.notReadyRetryable,                 modifier = Modifier.fillMaxSize().padding(padding),             )             else -> LazyColumn(
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.ktindex 53f9992113..5d3e82b3d7 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt@@ -102,6 +102,7 @@ fun SearchScreen(                     error = state.error,                     notReady = state.notReady,                 notReadyReason = state.notReadyReason,+                notReadyRetryable = state.notReadyRetryable,                     modifier = Modifier.fillMaxSize(),                     title = stringResource(R.string.search_failed),                     retryLabel = stringResource(R.string.retry),
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.ktindex a58480fa06..49715507cc 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/components/LoadFailureState.kt@@ -17,10 +17,12 @@ import com.vayunmathur.musicbrainz.R  * end, and there is no fallback source to fall back to. Either way this is a settled state  * rather than a spinner, so the screen never sits there implying progress it is not making.  *- * [notReadyReason] is the server explaining that a missing catalogue is NOT on its way - the- * host may not be able to build one at all. In that case it replaces the default copy, because- * "try again in a few minutes" would be a promise nothing is going to keep, and the retry- * button goes with it: there is nothing for retrying to achieve.+ * [notReadyReason] is the server explaining why a catalogue is missing, shown in place of the+ * default copy. [notReadyRetryable] is a SEPARATE question the server also answers: whether+ * asking again could ever help. The two do not move together - a build that failed and is queued+ * for the next check has a reason AND is worth retrying, while a host that cannot fit a build+ * has a reason and never will be. So the button follows [notReadyRetryable] alone, and offering+ * a retry that cannot work is the thing being avoided.  */ @Composable fun LoadFailureState(@@ -28,17 +30,17 @@ fun LoadFailureState(     notReady: Boolean,     modifier: Modifier = Modifier,     notReadyReason: String? = null,+    notReadyRetryable: Boolean = true,     title: String = stringResource(R.string.load_failed),     retryLabel: String? = null,     onRetry: (() -> Unit)? = null, ) {     if (notReady) {-        val retryable = notReadyReason == null         EmptyState(             title = stringResource(R.string.catalogue_not_ready),             modifier = modifier,             message = notReadyReason ?: stringResource(R.string.catalogue_not_ready_message),-            action = if (retryable && onRetry != null && retryLabel != null) {+            action = if (notReadyRetryable && onRetry != null && retryLabel != null) {                 { Button(onClick = onRetry) { Text(retryLabel) } }             } else {                 null
musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt
diff --git a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.ktindex bcd4ffe48e..351f4fdc46 100644--- a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt+++ b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.musicbrainz.network.api import java.io.IOException import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue @@ -70,9 +71,57 @@ class MusicBrainzApiErrorTest {             val failure = MusicBrainzApi.failureFor(503, body)             assertTrue(failure is CatalogueNotReadyException, "body ${'"'}$body${'"'} should be not-ready")             assertNull(failure.reason, "an unparseable body cannot supply a reason")+            assertTrue(failure.retryable, "with nothing to go on, retrying is worth offering")         }     } +    /**+     * `absent` is NOT the same as "never". A build that failed and is queued for the next check,+     * and a first boot before the scheduler has run, are both absent yet worth retrying - so the+     * server reports retryability and the app must not infer it from the state or from the+     * presence of a reason.+     */+    @Test+    fun `an absent catalogue that is still coming stays retryable`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"absent","retryable":true,"detail":"a build failed and is queued for the next check"}""",+        ) as CatalogueNotReadyException+        assertTrue(failure.retryable, "a queued rebuild must keep the retry button")+        assertEquals("a build failed and is queued for the next check", failure.reason)+    }++    /** The case where a retry can never work, so the button must not be offered. */+    @Test+    fun `a host that cannot build is not retryable`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"absent","retryable":false,"detail":"cannot build the catalogue here"}""",+        ) as CatalogueNotReadyException+        assertFalse(failure.retryable)+        assertEquals("cannot build the catalogue here", failure.reason)+    }++    /**+     * If the server ever stops sending `retryable`, fall back to inferring it from the reason+     * rather than offering a retry that cannot work: a body with a reason behaves as it did+     * before the field existed, and one without stays retryable.+     */+    @Test+    fun `infers retryability when the server does not report it`() {+        val withReason = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"absent","detail":"cannot build the catalogue here"}""",+        ) as CatalogueNotReadyException+        assertFalse(withReason.retryable, "a reason with no retryable field must not offer a retry")++        val building = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"building","progress":0.1}""",+        ) as CatalogueNotReadyException+        assertTrue(building.retryable)+    }+     /** Mid-build answers 503 too, and reads the same to the user: come back shortly. */     @Test     fun `503 while building is also a wait`() {