From 277fc6a0d65649c01bd1a3b27cf26744f0794601 Mon Sep 17 00:00:00 2001 From: ryoii Date: Mon, 27 Nov 2023 15:27:32 +0800 Subject: [PATCH] Add GlobalExceptionHandler for `websocket` adapter --- .../api/http/adapter/http/router/base.kt | 3 +- .../adapter/internal/convertor/convertor.kt | 47 ------------------- .../http/adapter/internal/convertor/event.kt | 13 ++++- .../adapter/internal/convertor/message.kt | 10 ++++ .../mirai/api/http/adapter/ws/router/base.kt | 5 ++ .../net/mamoe/mirai/api/http/util/ktor.kt | 20 ++++++++ 6 files changed, 49 insertions(+), 49 deletions(-) delete mode 100644 mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/convertor.kt create mode 100644 mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/ktor.kt diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt index 6fd9b04a..79a19446 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt @@ -22,6 +22,7 @@ import net.mamoe.mirai.api.http.adapter.http.plugin.HttpRouterMonitor import net.mamoe.mirai.api.http.adapter.internal.serializer.BuiltinJsonSerializer import net.mamoe.mirai.api.http.adapter.uploading.uploadingRouter import net.mamoe.mirai.api.http.context.MahContextHolder +import net.mamoe.mirai.api.http.util.installOnce fun Application.httpModule(adapter: HttpAdapter) { @@ -36,7 +37,7 @@ fun Application.httpModule(adapter: HttpAdapter) { } install(ContentNegotiation) { json(json = BuiltinJsonSerializer.buildJson()) } - install(GlobalExceptionHandler) { printTrace = MahContextHolder.debug } + installOnce(GlobalExceptionHandler) { printTrace = MahContextHolder.debug } install(Authorization) if (MahContextHolder.debug) { install(DoubleReceive) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/convertor.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/convertor.kt deleted file mode 100644 index 7deb42eb..00000000 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/convertor.kt +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.api.http.adapter.internal.convertor - -import net.mamoe.mirai.api.http.adapter.internal.dto.EventDTO -import net.mamoe.mirai.api.http.adapter.internal.dto.MessageChainDTO -import net.mamoe.mirai.api.http.spi.persistence.Persistence -import net.mamoe.mirai.contact.Contact -import net.mamoe.mirai.event.events.BotEvent -import net.mamoe.mirai.event.events.MessageEvent -import net.mamoe.mirai.message.data.MessageChain -import net.mamoe.mirai.message.data.buildMessageChain - -/*********************** - * 转换函数 - ***********************/ - - -/** - * Core 对象转换为 DTO 对象 - * - * 对于事件类型, 见 event.kt - * 对于消息类型, 见 message.kt - */ -internal suspend fun BotEvent.toDTO(): EventDTO = when (this) { - is MessageEvent -> toDTO() - else -> convertBotEvent() -} - - -/************************************ - * 以下为 DTO 对象转换为 Core 对象 - ************************************/ - -/** - * 转换一条消息链 - */ -internal suspend fun MessageChainDTO.toMessageChain(contact: Contact, cache: Persistence): MessageChain { - return buildMessageChain { this@toMessageChain.forEach { it.convertToMessage(contact, cache)?.let(::add) } } -} diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/event.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/event.kt index e1f6a97a..60cecbac 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/event.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/event.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. @@ -19,6 +19,17 @@ import net.mamoe.mirai.event.events.* import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.debug +/** + * Core 对象转换为 DTO 对象 + * + * 对于事件类型, 见 event.kt + * 对于消息类型, 见 message.kt + */ +internal suspend fun BotEvent.toDTO(): EventDTO = when (this) { + is MessageEvent -> toDTO() + else -> convertBotEvent() +} + // TODO: 切换为 跳表 或利用函数重载去掉冗长的 when 语句 @OptIn(MiraiExperimentalApi::class) internal suspend fun BotEvent.convertBotEvent() = when (this) { diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt index f4157338..6130060b 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/convertor/message.kt @@ -10,9 +10,11 @@ package net.mamoe.mirai.api.http.adapter.internal.convertor import net.mamoe.mirai.api.http.adapter.internal.dto.* +import net.mamoe.mirai.api.http.spi.persistence.Persistence import net.mamoe.mirai.api.http.util.FaceMap import net.mamoe.mirai.api.http.util.PokeMap import net.mamoe.mirai.api.http.util.toHexString +import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Group import net.mamoe.mirai.event.events.* import net.mamoe.mirai.message.data.* @@ -20,6 +22,14 @@ import net.mamoe.mirai.message.data.Image.Key.queryUrl import net.mamoe.mirai.message.data.MarketFace import net.mamoe.mirai.utils.MiraiExperimentalApi +/** + * 转换一条消息链 + */ +internal suspend fun MessageChainDTO.toMessageChain(contact: Contact, cache: Persistence): MessageChain { + return buildMessageChain { this@toMessageChain.forEach { it.convertToMessage(contact, cache)?.let(::add) } } +} + + /*************************** * Core Message 对象转换函数 ***************************/ diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/ws/router/base.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/ws/router/base.kt index 958321a7..81797bbe 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/ws/router/base.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/ws/router/base.kt @@ -14,6 +14,7 @@ import io.ktor.server.routing.* import io.ktor.server.websocket.* import io.ktor.websocket.* import kotlinx.coroutines.channels.SendChannel +import net.mamoe.mirai.api.http.adapter.http.plugin.GlobalExceptionHandler import net.mamoe.mirai.api.http.adapter.internal.dto.VerifyRetDTO import net.mamoe.mirai.api.http.adapter.internal.serializer.toJson import net.mamoe.mirai.api.http.adapter.internal.serializer.toJsonElement @@ -22,6 +23,7 @@ import net.mamoe.mirai.api.http.adapter.ws.WebsocketAdapter import net.mamoe.mirai.api.http.adapter.ws.dto.WsOutgoing import net.mamoe.mirai.api.http.adapter.ws.extension.FrameLogExtension import net.mamoe.mirai.api.http.context.MahContextHolder +import net.mamoe.mirai.api.http.util.installOnce /** * ktor websocket 模块加载 @@ -34,6 +36,9 @@ fun Application.websocketRouteModule(wsAdapter: WebsocketAdapter) { } } } + + + installOnce(GlobalExceptionHandler) { printTrace = MahContextHolder.debug } wsRouter(wsAdapter) uploadingRouter() } diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/ktor.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/ktor.kt new file mode 100644 index 00000000..c42e7d28 --- /dev/null +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/util/ktor.kt @@ -0,0 +1,20 @@ +/* + * 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. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.api.http.util + +import io.ktor.server.application.* +import io.ktor.util.pipeline.* + +fun

, B : Any, F : Any> P.installOnce( + plugin: Plugin, + configure: B.() -> Unit = {} +) { + pluginOrNull(plugin) ?: install(plugin, configure) +} \ No newline at end of file