From 79bb9e7dc992a43e8bd23ba1812a446f7efe3572 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 17 Mar 2020 21:46:15 +0800 Subject: [PATCH] Add detailed error message --- .../io/serialization/jce/JceDecoder.kt | 12 +++-- .../qqandroid/io/serialization/jce/common.kt | 51 ++++++++++++------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt index 51e453e1b..337496bff 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt @@ -181,8 +181,9 @@ internal class JceDecoder( StructureKind.MAP -> { //println("!! MAP") - return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) { - it.checkType(Jce.MAP) + val tag = popTag() + return jce.skipToHeadAndUseIfPossibleOrFail(tag.id) { + it.checkType(Jce.MAP, "beginStructure", tag, descriptor) MapReader } } @@ -191,6 +192,8 @@ internal class JceDecoder( //println("decoderTag: $currentTagOrNull") //println("jceHead: " + jce.currentHeadOrNull) return jce.skipToHeadAndUseIfPossibleOrFail(currentTag.id) { + // don't check type. it's polymorphic + //println("listHead: $it") when (it.type) { Jce.SIMPLE_LIST -> { @@ -209,8 +212,9 @@ internal class JceDecoder( //println("!! CLASS") //println("decoderTag: $currentTag") //println("jceHead: " + jce.currentHeadOrNull) - return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) { jceHead -> - jceHead.checkType(Jce.STRUCT_BEGIN) + val tag = popTag() + return jce.skipToHeadAndUseIfPossibleOrFail(tag.id) { jceHead -> + jceHead.checkType(Jce.STRUCT_BEGIN, "beginStructure", tag, descriptor) repeat(descriptor.elementsCount) { pushTag(descriptor.getTag(descriptor.elementsCount - it - 1)) // better performance diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt index 2065a87ea..cc49784fc 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/common.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.qqandroid.io.serialization.jce import kotlinx.io.core.Output +import kotlinx.serialization.SerialDescriptor import kotlinx.serialization.SerialInfo @@ -57,8 +58,15 @@ internal data class JceTagCommon( override val id: Int ) : JceTag() -fun JceHead.checkType(type: Byte) { - check(this.type == type) { "type mismatch. Expected $type, actual ${this.type}" } +internal fun JceHead.checkType(type: Byte, message: String, tag: JceTag, descriptor: SerialDescriptor) { + check(this.type == type) { + "type mismatch. " + + "Expected ${JceHead.findJceTypeName(type)}, " + + "actual ${JceHead.findJceTypeName(this.type)} for $message. " + + "Tag info: " + + "id=${tag.id}, " + + "name=${descriptor.getElementName(tag.id)} " + + "in ${descriptor.serialName}" } } @PublishedApi @@ -83,23 +91,28 @@ inline class JceHead(private val value: Long) { val type: Byte get() = value.toUInt().toByte() override fun toString(): String { - val typeString = when (type) { - Jce.BYTE -> "Byte" - Jce.DOUBLE -> "Double" - Jce.FLOAT -> "Float" - Jce.INT -> "Int" - Jce.LIST -> "List" - Jce.LONG -> "Long" - Jce.MAP -> "Map" - Jce.SHORT -> "Short" - Jce.SIMPLE_LIST -> "SimpleList" - Jce.STRING1 -> "String1" - Jce.STRING4 -> "String4" - Jce.STRUCT_BEGIN -> "StructBegin" - Jce.STRUCT_END -> "StructEnd" - Jce.ZERO_TYPE -> "Zero" - else -> error("illegal jce type: $type") + return "JceHead(tag=$tag, type=$type(${findJceTypeName(type)}))" + } + + companion object { + fun findJceTypeName(type: Byte): String { + return when (type) { + Jce.BYTE -> "Byte" + Jce.DOUBLE -> "Double" + Jce.FLOAT -> "Float" + Jce.INT -> "Int" + Jce.LIST -> "List" + Jce.LONG -> "Long" + Jce.MAP -> "Map" + Jce.SHORT -> "Short" + Jce.SIMPLE_LIST -> "SimpleList" + Jce.STRING1 -> "String1" + Jce.STRING4 -> "String4" + Jce.STRUCT_BEGIN -> "StructBegin" + Jce.STRUCT_END -> "StructEnd" + Jce.ZERO_TYPE -> "Zero" + else -> error("illegal jce type: $type") + } } - return "JceHead(tag=$tag, type=$type($typeString))" } } \ No newline at end of file