Skip to content

Colota - Android GPS Location tracker

fix(sync): collapse the HTTP error body before logging it (#611)

Max Dietrich22e0049a7cd7
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
index 828887cc..e968d71a 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
@@ -35,6 +35,8 @@ class NetworkManager(private val context: Context) {
private const val CONNECTION_TIMEOUT = 10000
private const val READ_TIMEOUT = 10000
private const val NETWORK_CHECK_CACHE_MS = 5000L
+ private const val ERROR_BODY_MAX_CHARS = 200
+ private val WHITESPACE = Regex("\\s+")
}
private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@@ -303,7 +305,7 @@ class NetworkManager(private val context: Context) {
TestEndpointResult(
false,
httpStatus = responseCode,
- errorMessage = "Server returned $responseCode: ${errorBody.take(200)}"
+ errorMessage = "Server returned $responseCode: $errorBody"
)
}
} catch (e: SSLHandshakeException) {
@@ -373,8 +375,13 @@ class NetworkManager(private val context: Context) {
}
}
+ /**
+ * Bounded and single-line. A rate-limited flush logs one of these per rejected point, and an
+ * nginx error page is 7 lines each - enough to push real events out of the rolling log file.
+ */
private fun readErrorBody(connection: HttpURLConnection): String = try {
- connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "No error body"
+ connection.errorStream?.bufferedReader()?.use { it.readText() }
+ ?.replace(WHITESPACE, " ")?.trim()?.take(ERROR_BODY_MAX_CHARS) ?: "No error body"
} catch (_: Exception) { "Could not read error body" }
/**
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
index e8d42692..b3ef9a15 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
@@ -109,8 +109,55 @@ class NetworkManagerTest {
assertEquals("https://server.com/api", result)
}
+ // --- readErrorBody ---
+
+ /**
+ * A rate-limited flush logs one error body per rejected point. Field logs showed ~110 nginx
+ * 429 pages in two seconds, 7 lines each, crowding real events out of the rolling log file.
+ */
+ @Test
+ fun `an html error page is logged as a single bounded line`() {
+ val nginx429 = """
+ <html>
+ <head><title>429 Too Many Requests</title></head>
+ <body>
+ <center><h1>429 Too Many Requests</h1></center>
+ <hr><center>nginx</center>
+ </body>
+ </html>
+ """.trimIndent()
+
+ val logged = invokeReadErrorBody(nginx429)
+
+ assertFalse("must not span lines", logged.contains("\n"))
+ assertTrue("must stay bounded", logged.length <= 200)
+ assertTrue("must keep the useful part", logged.contains("429 Too Many Requests"))
+ }
+
+ @Test
+ fun `a body longer than the cap is truncated`() {
+ val logged = invokeReadErrorBody("x".repeat(5000))
+
+ assertEquals(200, logged.length)
+ }
+
+ @Test
+ fun `a missing body reports that rather than throwing`() {
+ assertEquals("No error body", invokeReadErrorBody(null))
+ }
+
// --- Reflection helpers to access private methods ---
+ private fun invokeReadErrorBody(body: String?): String {
+ val connection = io.mockk.mockk<java.net.HttpURLConnection>(relaxed = true)
+ io.mockk.every { connection.errorStream } returns body?.byteInputStream()
+ val method = NetworkManager::class.java
+ .getDeclaredMethod("readErrorBody", java.net.HttpURLConnection::class.java)
+ method.isAccessible = true
+ return method.invoke(createNetworkManagerViaReflection(), connection) as String
+ }
+
+
private fun invokeBuildQueryString(payload: JSONObject): String {
val method = NetworkManager::class.java.getDeclaredMethod("buildQueryString", JSONObject::class.java)
method.isAccessible = true