Postpone error reporting in parsing in NewContact (#1147)

* Add `ParseErrorPacket` for postpone error report

* Continue to parse even if an error occurs
This commit is contained in:
Karlatemp 2021-04-03 22:17:49 +08:00 committed by GitHub
parent 6e914f6941
commit f6fd4de14b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View File

@ -9,6 +9,9 @@
package net.mamoe.mirai.internal.network
import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.utils.MiraiLogger
/*
// moved to `mirai-core`
@ -40,4 +43,20 @@ internal open class MultiPacketBySequence<out P : Packet>(internal val delegate:
override operator fun iterator(): Iterator<P> = delegate.iterator()
override fun toString(): String = "MultiPacketBySequence"
}
}
internal class ParseErrorPacket(
val error: Throwable,
val direction: Direction = Direction.TO_BOT_LOGGER,
) : Packet, Packet.NoLog {
enum class Direction {
TO_BOT_LOGGER {
override fun getLogger(bot: QQAndroidBot): MiraiLogger = bot.logger
},
TO_NETWORK_LOGGER {
override fun getLogger(bot: QQAndroidBot): MiraiLogger = bot.network.logger
};
abstract fun getLogger(bot: QQAndroidBot): MiraiLogger
}
}

View File

@ -517,6 +517,9 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
// highest priority: pass to listeners (attached by sendAndExpect).
if (packet != null && (bot.logger.isEnabled || logger.isEnabled)) {
when {
packet is ParseErrorPacket -> {
packet.direction.getLogger(bot).error(packet.error)
}
packet is Packet.NoLog -> {
// nothing to do
}

View File

@ -21,6 +21,7 @@ import net.mamoe.mirai.internal.QQAndroidBot
import net.mamoe.mirai.internal.message.contextualBugReportException
import net.mamoe.mirai.internal.network.MultiPacketByIterable
import net.mamoe.mirai.internal.network.Packet
import net.mamoe.mirai.internal.network.ParseErrorPacket
import net.mamoe.mirai.internal.network.QQAndroidClient
import net.mamoe.mirai.internal.network.protocol.data.proto.Structmsg
import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketFactory
@ -260,7 +261,11 @@ internal class NewContact {
) { // duplicate
return@mapNotNull null
}
handleStruct(struct)
try {
handleStruct(struct)
} catch (e: Throwable) {
ParseErrorPacket(e)
}
}.let { packets ->
when {
packets.isEmpty() -> null