diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/GlobalExceptionHandler.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/GlobalExceptionHandler.kt new file mode 100644 index 00000000..4b98fde4 --- /dev/null +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/GlobalExceptionHandler.kt @@ -0,0 +1,21 @@ +/* + * 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.http.plugin + +import io.ktor.server.application.* +import io.ktor.server.application.hooks.* +import net.mamoe.mirai.api.http.adapter.http.router.respondStateCode +import net.mamoe.mirai.api.http.adapter.internal.handler.toStateCode + +val GlobalExceptionHandler = createApplicationPlugin("GlobalExceptionHandler") { + on(CallFailed) { call, cause -> + call.respondStateCode(cause.toStateCode()) + } +} \ No newline at end of file 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 4015e1b6..366bd1e1 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 @@ -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. @@ -15,6 +15,7 @@ import io.ktor.server.plugins.defaultheaders.* import io.ktor.server.plugins.doublereceive.* import net.mamoe.mirai.api.http.adapter.http.HttpAdapter import net.mamoe.mirai.api.http.adapter.http.plugin.Authorization +import net.mamoe.mirai.api.http.adapter.http.plugin.GlobalExceptionHandler import net.mamoe.mirai.api.http.adapter.http.plugin.HttpRouterMonitor import net.mamoe.mirai.api.http.context.MahContextHolder @@ -30,6 +31,7 @@ fun Application.httpModule(adapter: HttpAdapter) { } } + install(GlobalExceptionHandler) install(Authorization) if (MahContextHolder.debug) { install(DoubleReceive) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/handler/exceptionHandle.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/handler/exceptionHandle.kt index 01ddaaea..0126d68a 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/handler/exceptionHandle.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/handler/exceptionHandle.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. @@ -24,24 +24,17 @@ internal suspend inline fun handleException( ): StateCode? = try { blk() null -} catch (e: NoSuchBotException) { // Bot不存在 - StateCode.NoBot -} catch (e: IllegalSessionException) { // Session过期 - StateCode.IllegalSession -} catch (e: NotVerifiedSessionException) { // Session未认证 - StateCode.NotVerifySession -} catch (e: NoSuchElementException) { // 指定对象不存在 - StateCode.NoElement -} catch (e: NoSuchFileException) { // 文件不存在 - StateCode.NoFile(e.file) -} catch (e: PermissionDeniedException) { // 缺少权限 - StateCode.PermissionDenied -} catch (e: BotIsBeingMutedException) { // Bot被禁言 - StateCode.BotMuted -} catch (e: MessageTooLargeException) { // 消息过长 - StateCode.MessageTooLarge -} catch (e: IllegalAccessException) { // 错误访问 - StateCode.IllegalAccess(e.message) -} catch (e: Throwable) { - StateCode.InternalError(e.localizedMessage ?: "", e) -} +} catch (e: Throwable) { e.toStateCode() } + +internal fun Throwable.toStateCode(): StateCode = when (this) { + is NoSuchBotException -> StateCode.NoBot + is IllegalSessionException -> StateCode.IllegalSession + is NotVerifiedSessionException -> StateCode.NotVerifySession + is NoSuchElementException -> StateCode.NoElement + is NoSuchFileException -> StateCode.NoFile(this.file) + is PermissionDeniedException -> StateCode.PermissionDenied + is BotIsBeingMutedException -> StateCode.BotMuted + is MessageTooLargeException -> StateCode.MessageTooLarge + is IllegalAccessException -> StateCode.IllegalAccess(this.message) + else -> StateCode.InternalError(this.localizedMessage ?: "", this) +} \ No newline at end of file