fix(android): extract OfflineGatedDataSource gate to satisfy detekt ThrowsCount
android / Build + lint + test (push) Successful in 3m24s
android / Build + lint + test (push) Successful in 3m24s
The added IOException rethrow pushed open() to 3 throws (max 2). Move the two gating throws into a private gateOnHealth() helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+13
-8
@@ -26,6 +26,19 @@ class OfflineGatedDataSource(
|
|||||||
) : DataSource {
|
) : DataSource {
|
||||||
|
|
||||||
override fun open(dataSpec: DataSpec): Long {
|
override fun open(dataSpec: DataSpec): Long {
|
||||||
|
gateOnHealth()
|
||||||
|
return try {
|
||||||
|
val opened = delegate.open(dataSpec)
|
||||||
|
health.reportSuccess() // bytes flowing from the server == reachable
|
||||||
|
opened
|
||||||
|
} catch (e: IOException) {
|
||||||
|
health.reportFailure() // real network read failed → arbitrate via /healthz
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Fast-fail before touching the network when the server can't be reached. */
|
||||||
|
private fun gateOnHealth() {
|
||||||
when (health.state.value) {
|
when (health.state.value) {
|
||||||
ServerHealth.Offline -> throw OfflineException(
|
ServerHealth.Offline -> throw OfflineException(
|
||||||
"Track not in the on-device cache and the device is offline.",
|
"Track not in the on-device cache and the device is offline.",
|
||||||
@@ -36,14 +49,6 @@ class OfflineGatedDataSource(
|
|||||||
// Unstable is non-gating: still try the network. Healthy too.
|
// Unstable is non-gating: still try the network. Healthy too.
|
||||||
ServerHealth.Unstable, ServerHealth.Healthy -> Unit
|
ServerHealth.Unstable, ServerHealth.Healthy -> Unit
|
||||||
}
|
}
|
||||||
return try {
|
|
||||||
val opened = delegate.open(dataSpec)
|
|
||||||
health.reportSuccess() // bytes flowing from the server == reachable
|
|
||||||
opened
|
|
||||||
} catch (e: IOException) {
|
|
||||||
health.reportFailure() // real network read failed → arbitrate via /healthz
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun close() = delegate.close()
|
override fun close() = delegate.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user