Skip to content

Commit

Permalink
Add GlobalExceptionHandler for websocket adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Nov 27, 2023
1 parent 447bb49 commit 277fc6a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@
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.*
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 对象转换函数
***************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 模块加载
Expand All @@ -34,6 +36,9 @@ fun Application.websocketRouteModule(wsAdapter: WebsocketAdapter) {
}
}
}


installOnce(GlobalExceptionHandler) { printTrace = MahContextHolder.debug }
wsRouter(wsAdapter)
uploadingRouter()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <P : Pipeline<*, ApplicationCall>, B : Any, F : Any> P.installOnce(
plugin: Plugin<P, B, F>,
configure: B.() -> Unit = {}
) {
pluginOrNull(plugin) ?: install(plugin, configure)
}

0 comments on commit 277fc6a

Please sign in to comment.