Skip to content

Commit 59019dc

Browse files
committed
fix(log): Optimize log printing when the danmu request fails
1 parent a2a719c commit 59019dc

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

composeApp/src/commonMain/kotlin/com/jankinwu/fntv/client/data/network/impl/FlyNarwhalApiImpl.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import io.ktor.client.statement.HttpResponse
3232
import io.ktor.client.statement.bodyAsChannel
3333
import io.ktor.client.statement.bodyAsText
3434
import io.ktor.http.HttpHeaders
35+
import io.ktor.http.isSuccess
3536
import io.ktor.serialization.jackson.jackson
3637
import io.ktor.utils.io.readUTF8Line
3738

@@ -122,6 +123,17 @@ class FlyNarwhalApiImpl : FlyNarwhalApi {
122123
}
123124

124125
private suspend fun readDanmakuFromSse(response: HttpResponse): Map<String, List<Danmaku>> {
126+
if (!response.status.isSuccess()) {
127+
val errorBody = response.bodyAsText()
128+
val errorMessage = try {
129+
mapper.readValue<SmartAnalysisResult<*>>(errorBody).msg
130+
} catch (_: Exception) {
131+
errorBody
132+
}
133+
logger.e { "SSE request failed with status ${response.status}: $errorMessage" }
134+
throw IllegalStateException(errorMessage.ifBlank { "SSE request failed with status ${response.status}" })
135+
}
136+
125137
val channel = response.bodyAsChannel()
126138
var eventName: String? = null
127139
val dataLines = mutableListOf<String>()

0 commit comments

Comments
 (0)