Skip to content

Commit 5090028

Browse files
authored
FailFast when WS message were not proceed properly (#5459)
1 parent 62e4d9d commit 5090028

File tree

1 file changed

+12
-2
lines changed
  • common/src/main/kotlin/io/homeassistant/companion/android/common/data/websocket/impl

1 file changed

+12
-2
lines changed

common/src/main/kotlin/io/homeassistant/companion/android/common/data/websocket/impl/WebSocketCoreImpl.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import io.homeassistant.companion.android.common.data.websocket.impl.entities.St
4444
import io.homeassistant.companion.android.common.data.websocket.impl.entities.TemplateUpdatedEvent
4545
import io.homeassistant.companion.android.common.data.websocket.impl.entities.TriggerEvent
4646
import io.homeassistant.companion.android.common.data.websocket.impl.entities.UnknownTypeSocketResponse
47+
import io.homeassistant.companion.android.common.util.FailFast
4748
import io.homeassistant.companion.android.common.util.MapAnySerializer
4849
import io.homeassistant.companion.android.common.util.kotlinJsonMapper
4950
import java.io.IOException
@@ -56,6 +57,7 @@ import kotlinx.coroutines.CompletableDeferred
5657
import kotlinx.coroutines.CoroutineExceptionHandler
5758
import kotlinx.coroutines.CoroutineScope
5859
import kotlinx.coroutines.CoroutineStart
60+
import kotlinx.coroutines.DelicateCoroutinesApi
5961
import kotlinx.coroutines.Dispatchers
6062
import kotlinx.coroutines.Job
6163
import kotlinx.coroutines.channels.Channel
@@ -326,6 +328,7 @@ internal class WebSocketCoreImpl(
326328
Timber.d("Websocket: onOpen")
327329
}
328330

331+
@OptIn(DelicateCoroutinesApi::class)
329332
override fun onMessage(webSocket: WebSocket, text: String) {
330333
Timber.d("Websocket: onMessage (${if (BuildConfig.DEBUG) "text: $text" else "text"})")
331334
val jsonElement = kotlinJsonMapper.decodeFromString<JsonElement>(text)
@@ -338,7 +341,7 @@ internal class WebSocketCoreImpl(
338341
// Send messages to the queue to ensure they are handled in order and don't block the function
339342
messages.forEach { message ->
340343
Timber.d("Message id ${message.maybeId()} received")
341-
val success = messageQueue.trySend(
344+
val result = messageQueue.trySend(
342345
wsScope.launch(start = CoroutineStart.LAZY) {
343346
when (message) {
344347
is AuthRequiredSocketResponse -> Timber.d("Auth Requested")
@@ -349,7 +352,14 @@ internal class WebSocketCoreImpl(
349352
}
350353
},
351354
)
352-
if (!success.isSuccess) Timber.w("Message id ${message.maybeId()} not being processed")
355+
356+
FailFast.failWhen(!result.isSuccess) {
357+
"Failed to process message (ID: ${message.maybeId()}). " +
358+
"IsFailure? ${result.isFailure}. " +
359+
"Is wsScope active? ${wsScope.isActive}. " +
360+
"Queue status: isClosedForSend = ${messageQueue.isClosedForSend}. " +
361+
"Exception: ${result.exceptionOrNull()}"
362+
}
353363
}
354364
}
355365

0 commit comments

Comments
 (0)