Kaynağa Gözat

Abi breaking change for `StateCode` serialization

ryoii 2 yıl önce
ebeveyn
işleme
3868edbfd6

+ 17 - 37
mirai-api-http-spi/src/main/java/net/mamoe/mirai/api/http/adapter/common/StateCode.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -14,44 +14,24 @@ import net.mamoe.mirai.api.http.adapter.internal.dto.DTO
 import java.io.File
 
 @Serializable
+@Suppress("FunctionName")
 open class StateCode(val code: Int, var msg: String) : DTO {
-    object Success : StateCode(0, "success") // 成功
-    object AuthKeyFail : StateCode(1, "Auth Key错误")
-    object NoBot : StateCode(2, "指定Bot不存在")
-    object IllegalSession : StateCode(3, "Session失效或不存在")
-    object NotVerifySession : StateCode(4, "Session未认证")
-    object NoElement : StateCode(5, "指定对象不存在")
-    object NoOperateSupport : StateCode(6, "指定操作不支持")
-    object PermissionDenied : StateCode(10, "无操作权限")
-    object BotMuted : StateCode(20, "Bot被禁言")
-    object MessageTooLarge : StateCode(30, "消息过长")
-    object InvalidParameter : StateCode(400, "无效参数")
 
-    class NoFile() : StateCode(6, "") {
-        constructor(file: File) : this() {
-            this.msg = "文件不存在:${file.absolutePath}"
-        }
-    }
-
-    // KS bug: 主构造器中不能有非字段参数 https://github.com/Kotlin/kotlinx.serialization/issues/575
-    /**
-     * 异常访问
-     */
-    class IllegalAccess() : StateCode(400, "") { // 非法访问
-        constructor(msg: String) : this() {
-            this.msg = msg
-        }
-    }
-
-    /**
-     * 内部错误
-     */
-    class InternalError() : StateCode(500, "") {
-        var throwable: Throwable? = null
+    companion object {
+        val Success = StateCode(0, "success")
+        val AuthKeyFail = StateCode(1, "Auth Key错误")
+        val NoBot = StateCode(2, "指定Bot不存在")
+        val IllegalSession = StateCode(3, "Session失效或不存在")
+        val NotVerifySession = StateCode(4, "Session未认证")
+        val NoElement = StateCode(5, "指定对象不存在")
+        val NoOperateSupport = StateCode(6, "指定操作不支持")
+        val PermissionDenied = StateCode(10, "无操作权限")
+        val BotMuted = StateCode(20, "Bot被禁言")
+        val MessageTooLarge = StateCode(30, "消息过长")
+        val InvalidParameter = StateCode(400, "无效参数")
 
-        constructor(msg: String, throwable: Throwable) : this() {
-            this.msg = msg
-            this.throwable = throwable
-        }
+        fun NoFile(file: File? = null) = StateCode(6, file?.run { "文件不存在:${absolutePath}" } ?: "")
+        fun IllegalAccess(msg: String? = null) = StateCode(400, msg ?: "")
+        fun InternalError(msg: String? = null) = StateCode(500, msg ?: "")
     }
 }

+ 2 - 2
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/handler/exceptionHandle.kt

@@ -38,9 +38,9 @@ internal fun Throwable.toStateCode(): StateCode = when (this) {
     is PermissionDeniedException -> StateCode.PermissionDenied
     is BotIsBeingMutedException -> StateCode.BotMuted
     is MessageTooLargeException -> StateCode.MessageTooLarge
-    is BadRequestException -> StateCode.IllegalAccess(findMissingFiled() ?: this.localizedMessage ?: "")
+    is BadRequestException -> StateCode.IllegalAccess(findMissingFiled() ?: this.localizedMessage)
     is IllegalAccessException -> StateCode.IllegalAccess(this.message)
-    else -> StateCode.InternalError(this.localizedMessage ?: "", this)
+    else -> StateCode.InternalError(this.localizedMessage)
 }
 
 @OptIn(ExperimentalSerializationApi::class)