Require a CoroutineExceptionHandler for MiraiConsole.coroutineContext

This commit is contained in:
Him188 2020-08-29 23:51:31 +08:00
parent 27952f537f
commit 2c67d97d58

View File

@ -12,6 +12,7 @@
package net.mamoe.mirai.console.internal
import com.vdurmont.semver4j.Semver
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import net.mamoe.mirai.Bot
@ -86,6 +87,9 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
if (coroutineContext[Job] == null) {
throw MalformedMiraiConsoleImplementationError("The coroutineContext given to MiraiConsole must have a Job in it.")
}
if (coroutineContext[CoroutineExceptionHandler] == null) {
throw MalformedMiraiConsoleImplementationError("The coroutineContext given to MiraiConsole must have a CoroutineExceptionHandler in it.")
}
MiraiConsole.job.invokeOnCompletion {
Bot.botInstances.forEach { kotlin.runCatching { it.close() }.exceptionOrNull()?.let(mainLogger::error) }