Modern Apps git

← Modern-Apps.git

musicbrainz: pin 503 not-ready against 500 failure mapping

Commit
08c5a2e02ae3cd125a874a6876e45aca272e9051
Author
vayun-mathur <[email protected]>
When
2026-08-20 21:30 UTC (3 months ago)
Parents
6c641b5

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 0cfac89c14..c84684e6f5 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@@ -58,17 +58,26 @@ object MusicBrainzApi {     }      /**-     * A 503 is the mirror saying it has no catalogue loaded yet, which is a wait rather-     * than a fault; every other non-2xx is a real failure.+     * The failure for a non-2xx status, or null when the response is fine.+     *+     * 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.+     *+     * 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 in 200..299 -> null+        else -> IOException("HTTP $status: ${body.take(500)}")+    }+     private suspend inline fun <reified T> get(path: String): T {         val response = NetworkClient.performRequest("$BASE/$path", headers = headers)-        if (response.status == HttpURLConnection.HTTP_UNAVAILABLE) {-            throw CatalogueNotReadyException("HTTP 503: ${response.body.take(200)}")-        }-        if (!response.isSuccess) {-            throw IOException("HTTP ${response.status}: ${response.body.take(500)}")-        }+        failureFor(response.status, response.body)?.let { throw it }         return json.decodeFromString<T>(response.body)     } 
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.ktnew file mode 100644index 0000000000..86310af136--- /dev/null+++ b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApiErrorTest.kt@@ -0,0 +1,97 @@+package com.vayunmathur.musicbrainz.network.api++import java.io.IOException+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue++/**+ * Pins how HTTP statuses from the self-hosted catalogue map onto failures.+ *+ * This is the contract with the server written down as assertions: 503 means "no catalogue+ * loaded yet", which is a wait, and 500 means a genuine fault. The two never overlap, which is+ * what lets the app say "catalogue not ready" instead of "something went wrong". If the server+ * ever starts answering 503 for real faults, or 500 while it is still importing, the screens+ * quietly start lying to the user - so the split is asserted here rather than trusted.+ */+class MusicBrainzApiErrorTest {++    @Test+    fun `503 is a wait rather than a fault`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"absent","detail":"no catalogue pack on this server"}""",+        )+        assertTrue(+            failure is CatalogueNotReadyException,+            "503 must surface as not-ready, got ${failure?.javaClass?.simpleName}",+        )+    }++    /** Mid-build answers 503 too, and reads the same to the user: come back shortly. */+    @Test+    fun `503 while building is also a wait`() {+        val failure = MusicBrainzApi.failureFor(+            503,+            """{"error":"not_ready","state":"building","progress":0.42,"detail":"spilling tables"}""",+        )+        assertTrue(failure is CatalogueNotReadyException)+    }++    /**+     * 500 is the server's signal for a real fault and must NOT be dressed up as a wait -+     * telling the user to try again in a few minutes would hide a genuine server bug.+     */+    @Test+    fun `500 is a genuine failure`() {+        val failure = MusicBrainzApi.failureFor(500, """{"error":"internal"}""")+        assertTrue(failure is IOException)+        assertTrue(+            failure !is CatalogueNotReadyException,+            "500 must not be reported to the user as a catalogue that is still importing",+        )+    }++    /**+     * A 404 means the route is not there at all, which is a deployment fault rather than a+     * catalogue still importing. Reporting it as not-ready would reassure the user while+     * hiding that the server was never rolled out.+     */+    @Test+    fun `404 is a genuine failure, not a missing catalogue`() {+        val failure = MusicBrainzApi.failureFor(404, "Not Found")+        assertTrue(failure is IOException)+        assertTrue(failure !is CatalogueNotReadyException)+    }++    @Test+    fun `other error statuses are genuine failures`() {+        for (status in listOf(400, 401, 403, 429, 502, 504)) {+            val failure = MusicBrainzApi.failureFor(status, "boom")+            assertTrue(failure is IOException, "$status should fail")+            assertTrue(failure !is CatalogueNotReadyException, "$status must not read as not-ready")+        }+    }++    @Test+    fun `success statuses are not failures`() {+        for (status in listOf(200, 201, 204, 299)) {+            assertNull(MusicBrainzApi.failureFor(status, "{}"), "$status should not fail")+        }+    }++    /** The status is kept in the message so a report names the code that caused it. */+    @Test+    fun `carries the status into the message`() {+        assertEquals("HTTP 500: boom", MusicBrainzApi.failureFor(500, "boom")?.message)+        assertTrue(MusicBrainzApi.failureFor(503, "waiting")?.message?.contains("503") == true)+    }++    /** A long error body must not be pasted wholesale into a message the UI may show. */+    @Test+    fun `truncates a long body`() {+        val message = MusicBrainzApi.failureFor(500, "x".repeat(5_000))?.message.orEmpty()+        assertTrue(message.length < 600, "message was ${message.length} chars")+    }+}