Skip to content

Commit 11b202d

Browse files
committed
GlobalExceptionHandlerConfig for printing exception trace
1 parent 518a7c9 commit 11b202d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/plugin/GlobalExceptionHandler.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ package net.mamoe.mirai.api.http.adapter.http.plugin
1212
import io.ktor.server.application.*
1313
import io.ktor.server.application.hooks.*
1414
import io.ktor.server.response.*
15+
import io.ktor.util.*
1516
import net.mamoe.mirai.api.http.adapter.internal.handler.toStateCode
1617

17-
val GlobalExceptionHandler = createApplicationPlugin("GlobalExceptionHandler") {
18+
val GlobalExceptionHandler = createApplicationPlugin(
19+
"GlobalExceptionHandler",
20+
::GlobalExceptionHandlerConfig
21+
) {
1822
on(CallFailed) { call, cause ->
23+
if (pluginConfig.printTrace) {
24+
cause.printStackTrace()
25+
}
1926
call.respond(cause.toStateCode())
2027
}
28+
}
29+
30+
@KtorDsl
31+
class GlobalExceptionHandlerConfig {
32+
var printTrace: Boolean = false
2133
}

mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fun Application.httpModule(adapter: HttpAdapter) {
3535
}
3636

3737
install(ContentNegotiation) { json(json = BuiltinJsonSerializer.buildJson()) }
38-
install(GlobalExceptionHandler)
38+
install(GlobalExceptionHandler) { printTrace = MahContextHolder.debug }
3939
install(Authorization)
4040
if (MahContextHolder.debug) {
4141
install(DoubleReceive)

0 commit comments

Comments
 (0)