mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-09 19:50:27 +08:00
Improve CheckableResponse
This commit is contained in:
parent
d727bae9d0
commit
5188d88483
@ -28,6 +28,17 @@ public abstract class CheckableResponseA : CheckableResponse {
|
|||||||
final override val _errorMessage: String? get() = errorMessage
|
final override val _errorMessage: String? get() = errorMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
public abstract class CheckableResponseB : CheckableResponse {
|
||||||
|
public abstract val result: Int
|
||||||
|
|
||||||
|
@Suppress("SpellCheckingInspection")
|
||||||
|
public abstract val errmsg: String
|
||||||
|
|
||||||
|
final override val _errorCode: Int get() = result
|
||||||
|
final override val _errorMessage: String get() = errmsg
|
||||||
|
}
|
||||||
|
|
||||||
public class DeserializationFailure(
|
public class DeserializationFailure(
|
||||||
structType: KType,
|
structType: KType,
|
||||||
public val json: String,
|
public val json: String,
|
||||||
@ -40,17 +51,42 @@ public class DeserializationFailure(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun <T : CheckableResponse> T.checked(): T {
|
/*
|
||||||
|
* `check`: throws exception, or returns succeed value.
|
||||||
|
* `checked`: do `check` and wrap result into an `Either`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public fun <T : CheckableResponse> T.check(): T {
|
||||||
check(_errorCode == 0) { "Error code: $_errorCode, Error message: $_errorMessage" }
|
check(_errorCode == 0) { "Error code: $_errorCode, Error message: $_errorMessage" }
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun DeserializationFailure.checked(): Nothing = throw this.createException()
|
public open class FailureResponse(
|
||||||
|
public val errorCode: Int,
|
||||||
public inline fun <reified T : CheckableResponse> Either<DeserializationFailure, T>.checked(): T {
|
public val errorMessage: String,
|
||||||
return this.fold(onLeft = { it.checked() }, onRight = { it.checked() })
|
) {
|
||||||
|
public fun createException(): Exception {
|
||||||
|
return IllegalStateException("Error code: $errorCode, Error message: $errorMessage")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public inline fun <reified T> Either<DeserializationFailure, T>.checked(): T {
|
public inline fun <reified T : CheckableResponse> T.checked(): Either<FailureResponse, T> {
|
||||||
return this.fold(onLeft = { it.checked() }, onRight = { it })
|
if (_errorCode == 0) return Either<FailureResponse, T>(this)
|
||||||
|
return Either(FailureResponse(_errorCode, _errorMessage.toString()))
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun DeserializationFailure.check(): Nothing = throw this.createException()
|
||||||
|
public fun FailureResponse.check(): Nothing = throw this.createException()
|
||||||
|
|
||||||
|
public inline fun <reified T : CheckableResponse> Either<DeserializationFailure, T>.check(): T {
|
||||||
|
return this.fold(onLeft = { it.check() }, onRight = { it.check() })
|
||||||
|
}
|
||||||
|
|
||||||
|
public inline fun <reified T> Either<DeserializationFailure, T>.check(): T {
|
||||||
|
return this.fold(onLeft = { it.check() }, onRight = { it })
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmName("checkedFailureResponseT")
|
||||||
|
public inline fun <reified T> Either<FailureResponse, T>.check(): T {
|
||||||
|
return this.fold(onLeft = { it.check() }, onRight = { it })
|
||||||
}
|
}
|
@ -188,8 +188,8 @@ internal object AnnouncementProtocol {
|
|||||||
cookie("p_uin", "o$id")
|
cookie("p_uin", "o$id")
|
||||||
cookie("skey", sKey)
|
cookie("skey", sKey)
|
||||||
cookie("p_skey", psKey("qun.qq.com"))
|
cookie("p_skey", psKey("qun.qq.com"))
|
||||||
}.loadSafelyAs(UploadImageResp.serializer()).checked()
|
}.loadSafelyAs(UploadImageResp.serializer()).check()
|
||||||
return resp.id.replace(""", "\"").loadSafelyAs(GroupAnnouncementImage.serializer()).checked().toPublic()
|
return resp.id.replace(""", "\"").loadSafelyAs(GroupAnnouncementImage.serializer()).check().toPublic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ -233,7 +233,7 @@ internal object AnnouncementProtocol {
|
|||||||
cookie("p_uin", "o$id")
|
cookie("p_uin", "o$id")
|
||||||
cookie("skey", sKey)
|
cookie("skey", sKey)
|
||||||
cookie("p_skey", psKey("qun.qq.com"))
|
cookie("p_skey", psKey("qun.qq.com"))
|
||||||
}.loadSafelyAs(SendGroupAnnouncementResp.serializer()).checked().fid
|
}.loadSafelyAs(SendGroupAnnouncementResp.serializer()).check().fid
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun QQAndroidBot.getRawGroupAnnouncements(
|
suspend fun QQAndroidBot.getRawGroupAnnouncements(
|
||||||
@ -271,7 +271,7 @@ internal object AnnouncementProtocol {
|
|||||||
cookie("p_uin", "o$id")
|
cookie("p_uin", "o$id")
|
||||||
cookie("skey", sKey)
|
cookie("skey", sKey)
|
||||||
cookie("p_skey", psKey("qun.qq.com"))
|
cookie("p_skey", psKey("qun.qq.com"))
|
||||||
}.loadSafelyAs(DeleteResp.serializer()).checked()
|
}.loadSafelyAs(DeleteResp.serializer()).check()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import kotlinx.serialization.Serializable
|
|||||||
import kotlinx.serialization.protobuf.ProtoNumber
|
import kotlinx.serialization.protobuf.ProtoNumber
|
||||||
import net.mamoe.mirai.internal.network.Packet
|
import net.mamoe.mirai.internal.network.Packet
|
||||||
import net.mamoe.mirai.internal.utils.io.ProtoBuf
|
import net.mamoe.mirai.internal.utils.io.ProtoBuf
|
||||||
|
import net.mamoe.mirai.utils.CheckableResponseB
|
||||||
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY
|
import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
@ -650,14 +651,14 @@ internal class MsgSvc : ProtoBuf {
|
|||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
internal class PbGetRoamMsgResp(
|
internal class PbGetRoamMsgResp(
|
||||||
@ProtoNumber(1) @JvmField val result: Int = 0,
|
@ProtoNumber(1) override val result: Int = 0,
|
||||||
@ProtoNumber(2) @JvmField val errmsg: String = "",
|
@ProtoNumber(2) override val errmsg: String = "",
|
||||||
@ProtoNumber(3) @JvmField val peerUin: Long = 0L,
|
@ProtoNumber(3) @JvmField val peerUin: Long = 0L,
|
||||||
@ProtoNumber(4) @JvmField val lastMsgtime: Long = 0L,
|
@ProtoNumber(4) @JvmField val lastMsgtime: Long = 0L,
|
||||||
@ProtoNumber(5) @JvmField val random: Long = 0L,
|
@ProtoNumber(5) @JvmField val random: Long = 0L,
|
||||||
@ProtoNumber(6) @JvmField val msg: List<MsgComm.Msg> = emptyList(),
|
@ProtoNumber(6) @JvmField val msg: List<MsgComm.Msg> = emptyList(),
|
||||||
@ProtoNumber(7) @JvmField val sig: ByteArray = EMPTY_BYTE_ARRAY,
|
@ProtoNumber(7) @JvmField val sig: ByteArray = EMPTY_BYTE_ARRAY,
|
||||||
) : ProtoBuf
|
) : ProtoBuf, CheckableResponseB()
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
internal class PbDiscussReadedReportReq(
|
internal class PbDiscussReadedReportReq(
|
||||||
|
Loading…
Reference in New Issue
Block a user