Modern Apps git

← Modern-Apps.git

musicbrainz: say why a missing catalogue is not coming

Commit
b365e4e68b603225a9ca7cefa24ceeba8d11e5d1
Author
vayun-mathur <[email protected]>
When
2026-08-20 22:23 UTC (3 months ago)
Parents
739962c

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 c84684e6f5..e6fbc367e5 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@@ -1,20 +1,44 @@ package com.vayunmathur.musicbrainz.network.api  import com.vayunmathur.library.network.NetworkClient+import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import java.io.IOException import java.net.HttpURLConnection import java.net.URLEncoder +/** The 503 body the server sends when it has no catalogue to answer from. */+@Serializable+internal data class NotReadyBody(+    val error: String = "",+    val state: String = "",+    val progress: Float? = null,+    val detail: String? = null,+)+ /**  * The server has a catalogue to serve, but not yet: either the data pack was never  * imported or an import is still running. The server signals both as HTTP 503 with an  * `{"error":"not_ready"}` body, and reserves other statuses for genuine faults.  *- * Kept apart from an ordinary failure because it resolves on its own, so the UI can tell- * the user to come back shortly instead of reporting a fault they cannot act on.+ * Kept apart from an ordinary failure because the user can do nothing about it and it is not+ * 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.  */-class CatalogueNotReadyException(message: String) : IOException(message)+class CatalogueNotReadyException(+    message: String,+    val state: String? = null,+    val reason: String? = null,+) : IOException(message) {+    companion object {+        const val ABSENT = "absent"+        const val BUILDING = "building"+    }+}  /**  * Client for the self-hosted MusicBrainz mirror.@@ -62,15 +86,23 @@ object MusicBrainzApi {      *      * 503 is the server saying it has no catalogue loaded yet, which is a wait rather than a      * fault. The server uses 503 EXCLUSIVELY for that and 500 exclusively for a real failure,-     * with no overlap, so the status alone is enough to tell them apart - the body is carried-     * for the log, not parsed to make the decision.+     * with no overlap, so the STATUS alone decides which kind of failure this is. The body is+     * parsed only to carry the server's explanation through to the screen, and a body that does+     * not parse costs nothing but that explanation.      *      * Split out from [get] so `MusicBrainzApiErrorTest` can pin the mapping without a server;      * it is the one place the contract with the server is encoded.      */     internal fun failureFor(status: Int, body: String): IOException? = when {-        status == HttpURLConnection.HTTP_UNAVAILABLE ->-            CatalogueNotReadyException("HTTP 503: ${body.take(200)}")+        status == HttpURLConnection.HTTP_UNAVAILABLE -> {+            val parsed = runCatching { json.decodeFromString<NotReadyBody>(body) }.getOrNull()+            CatalogueNotReadyException(+                message = "HTTP 503: ${body.take(200)}",+                state = parsed?.state?.ifBlank { null },+                reason = parsed?.detail?.ifBlank { null }+                    ?.takeIf { parsed.state == CatalogueNotReadyException.ABSENT },+            )+        }         status in 200..299 -> null         else -> IOException("HTTP $status: ${body.take(500)}")     }
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 9abece5f0c..c4b8cf3183 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt@@ -90,6 +90,8 @@ data class SearchUiState(     val error: String? = null,     /** The catalogue is still importing, so this is a wait rather than a fault. */     val notReady: Boolean = false,+    /** The server's reason, when a missing catalogue is not simply on its way. */+    val notReadyReason: String? = null,     val hasSearched: Boolean = false,     val artists: List<ArtistRow> = emptyList(),     val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -101,6 +103,8 @@ data class ArtistUiState(     val error: String? = null,     /** The catalogue is still importing, so this is a wait rather than a fault. */     val notReady: Boolean = false,+    /** The server's reason, when a missing catalogue is not simply on its way. */+    val notReadyReason: String? = null,     val name: String = "",     val subtitle: String? = null,     val releaseGroups: List<ReleaseGroupRow> = emptyList(),@@ -111,6 +115,8 @@ data class ReleaseGroupUiState(     val error: String? = null,     /** The catalogue is still importing, so this is a wait rather than a fault. */     val notReady: Boolean = false,+    /** The server's reason, when a missing catalogue is not simply on its way. */+    val notReadyReason: String? = null,     val title: String = "",     val artist: String = "",     val coverUrl: String? = null,@@ -122,6 +128,8 @@ data class ReleaseUiState(     val error: String? = null,     /** The catalogue is still importing, so this is a wait rather than a fault. */     val notReady: Boolean = false,+    /** The server's reason, when a missing catalogue is not simply on its way. */+    val notReadyReason: String? = null,     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 69716644a1..0877288f0d 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt@@ -144,6 +144,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                 loading = true,                 error = null,                 notReady = false,+                notReadyReason = null,                 hasSearched = true,             )             try {@@ -185,6 +186,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     loading = false,                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,+                    notReadyReason = e.notReadyReason(),                 )             }         }@@ -237,6 +239,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     loading = false,                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,+                    notReadyReason = e.notReadyReason(),                 )             }         }@@ -294,6 +297,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     loading = false,                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,+                    notReadyReason = e.notReadyReason(),                 )             }         }@@ -316,6 +320,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                     loading = false,                     error = e.readableMessage(),                     notReady = e is CatalogueNotReadyException,+                    notReadyReason = e.notReadyReason(),                 )             }         }@@ -585,6 +590,13 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat         title = row.title,     ) +    /**+     * The server's explanation for a catalogue that is not merely on its way, so the screen can+     * say why instead of promising it will be along shortly.+     */+    private fun Exception.notReadyReason(): String? =+        (this as? CatalogueNotReadyException)?.reason+     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 dd3764d338..57eef3e1be 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ArtistScreen.kt@@ -53,6 +53,7 @@ fun ArtistScreen(             state.error != null -> LoadFailureState(                 error = state.error,                 notReady = state.notReady,+                notReadyReason = state.notReadyReason,                 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 6673d42361..f653349325 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseGroupScreen.kt@@ -64,6 +64,7 @@ fun ReleaseGroupScreen(             state.error != null -> LoadFailureState(                 error = state.error,                 notReady = state.notReady,+                notReadyReason = state.notReadyReason,                 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 fe2d431bcf..de8473b97f 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt@@ -74,6 +74,7 @@ fun ReleaseScreen(             state.error != null -> LoadFailureState(                 error = state.error,                 notReady = state.notReady,+                notReadyReason = state.notReadyReason,                 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 1fad4fa97c..53f9992113 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/SearchScreen.kt@@ -101,6 +101,7 @@ fun SearchScreen(                 state.error != null -> LoadFailureState(                     error = state.error,                     notReady = state.notReady,+                notReadyReason = state.notReadyReason,                     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 8929334db1..a58480fa06 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@@ -16,23 +16,29 @@ import com.vayunmathur.musicbrainz.R  * wait, not a fault: reporting it as an error sends the user hunting for a problem at their  * 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.  */ @Composable fun LoadFailureState(     error: String?,     notReady: Boolean,     modifier: Modifier = Modifier,+    notReadyReason: String? = null,     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 = stringResource(R.string.catalogue_not_ready_message),-            // The message tells the user to try again, so keep the button that does it.-            action = if (onRetry != null && retryLabel != null) {+            message = notReadyReason ?: stringResource(R.string.catalogue_not_ready_message),+            action = if (retryable && 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 86310af136..bcd4ffe48e 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@@ -29,6 +29,50 @@ class MusicBrainzApiErrorTest {         )     } +    /**+     * An absent catalogue is not always on its way - the host may not be able to build one at+     * all - so the server's explanation is carried through to replace copy that would otherwise+     * promise it is coming shortly.+     */+    @Test+    fun `carries the reason an absent catalogue is not coming`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"absent","detail":"cannot build the catalogue here: 5.2 GB RAM free, a build needs 8.0 GB"}""",+        ) as CatalogueNotReadyException+        assertEquals(CatalogueNotReadyException.ABSENT, failure.state)+        assertEquals(+            "cannot build the catalogue here: 5.2 GB RAM free, a build needs 8.0 GB",+            failure.reason,+        )+    }++    /**+     * A build in progress genuinely does resolve on its own, so it carries NO reason - the+     * screen keeps its own "still being prepared, try again shortly" copy, which is true here+     * and would be a false promise for an absent catalogue. `detail` is build-stage jargon+     * ("spilling tables") and is not shown to anyone.+     */+    @Test+    fun `does not carry a reason while building`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"building","progress":0.42,"detail":"spilling tables"}""",+        ) as CatalogueNotReadyException+        assertEquals(CatalogueNotReadyException.BUILDING, failure.state)+        assertNull(failure.reason, "a build in progress must not present itself as a dead end")+    }++    /** A 503 whose body is not the documented JSON still has to read as not-ready. */+    @Test+    fun `survives a 503 body that is not the documented json`() {+        for (body in listOf("", "Service Unavailable", "<html>502 upstream</html>", "{")) {+            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")+        }+    }+     /** Mid-build answers 503 too, and reads the same to the user: come back shortly. */     @Test     fun `503 while building is also a wait`() {