Add detailed error message

This commit is contained in:
Him188 2020-03-17 21:46:15 +08:00
parent 0407b99ae2
commit 79bb9e7dc9
2 changed files with 40 additions and 23 deletions

View File

@ -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

View File

@ -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,7 +91,12 @@ inline class JceHead(private val value: Long) {
val type: Byte get() = value.toUInt().toByte()
override fun toString(): String {
val typeString = when (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"
@ -100,6 +113,6 @@ inline class JceHead(private val value: Long) {
Jce.ZERO_TYPE -> "Zero"
else -> error("illegal jce type: $type")
}
return "JceHead(tag=$tag, type=$type($typeString))"
}
}
}