Skip to content

Commit

Permalink
优化未知事件序列化失败的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Apr 29, 2024
1 parent cf204aa commit 66d0b60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion simbot-component-qq-guild-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ kotlin {
}

jvmTest.dependencies {
runtimeOnly(libs.ktor.client.cio)
implementation(libs.ktor.client.cio)
// runtimeOnly(libs.kotlinx.coroutines.reactor)
// implementation(libs.reactor.core)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,25 @@ internal class ReceiveEvent(
val dispatch = try {
bot.wsDecoder.decodeFromJsonElement(Signal.Dispatch.serializer(), json)
} catch (serEx: SerializationException) {
if (serEx.message?.startsWith("Polymorphic serializer was not found for") == true) {
// 未知的事件类型
val disSeq = runCatching {
json.jsonObject["s"]?.jsonPrimitive?.longOrNull ?: seq.value
}.getOrElse { seq.value }
Signal.Dispatch.Unknown(disSeq, json, raw).also {
val t =
kotlin.runCatching { json.jsonObject[Signal.Dispatch.DISPATCH_CLASS_DISCRIMINATOR]?.jsonPrimitive?.content }
.getOrNull()
// if (tryCheckIsPolymorphicException(serEx)) {
// 未知的事件类型
val disSeq = runCatching {
json.jsonObject["s"]?.jsonPrimitive?.longOrNull ?: seq.value
}.getOrElse { seq.value }
Signal.Dispatch.Unknown(disSeq, json, raw).also {
val t =
kotlin.runCatching { json.jsonObject[Signal.Dispatch.DISPATCH_CLASS_DISCRIMINATOR]?.jsonPrimitive?.content }
.getOrNull()
if (tryCheckIsPolymorphicException(serEx)) {
logger.warn("Unknown event type {}, decode it as Unknown event: {}", t, it)
} else {
logger.warn("Unknown event type {}, decode it as Unknown event: {}", t, it, serEx)
}
} else {
// throw out
throw serEx
}
// } else {
// // throw out
// throw serEx
// }
}
logger.debug("Received dispatch: {}", dispatch)
val dispatchSeq = dispatch.seq
Expand Down Expand Up @@ -407,6 +411,20 @@ internal class ReceiveEvent(
}
}

private fun tryCheckIsPolymorphicException(exception: SerializationException): Boolean {
// 似乎是某个版本的错误提示,但是至少在 JSON v1.6.3 已经不适用了
if (exception.message?.startsWith("Polymorphic serializer was not found for") == true) {
return true
}

// kotlinx.serialization.json.internal.JsonDecodingException: Serializer for subclass 'MESSAGE_REACTION_ADD' is not found in the polymorphic scope of 'Dispatch'
if (exception.message?.contains("is not found in the polymorphic scope of") == true) {
return true
}

return false
}

@OptIn(ExperimentalSimbotCollectionApi::class)
private suspend fun emitEvent(bot: BotImpl, dispatch: Signal.Dispatch, raw: String) {
val logger = bot.logger
Expand Down

0 comments on commit 66d0b60

Please sign in to comment.