From 2ea86b377a886b9800078e80cf320a35ecb86c4b Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 31 Aug 2020 13:09:02 +0800 Subject: [PATCH] Fix #139, rethrow exceptions caught in JvmPluginInternal.onEnable --- .../net/mamoe/mirai/console/data/AutoSavePluginData.kt | 5 +++-- .../console/internal/plugin/JarPluginLoaderImpl.kt | 3 ++- .../mirai/console/internal/plugin/JvmPluginInternal.kt | 10 +++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/AutoSavePluginData.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/AutoSavePluginData.kt index 32f749746..3fd2e2e77 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/AutoSavePluginData.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/data/AutoSavePluginData.kt @@ -143,14 +143,15 @@ internal val debuggingLogger1 by lazy { DefaultLogger("debug").withSwitch(true) } -internal inline fun MiraiLogger.runCatchingLog(message: String? = null, block: () -> R): R? { +@Suppress("RESULT_CLASS_IN_RETURN_TYPE") +internal inline fun MiraiLogger.runCatchingLog(message: String? = null, block: () -> R): Result { return kotlin.runCatching { block() }.onFailure { if (message != null) { error(message, it) } else error(it) - }.getOrNull() + } } @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt index 8d2dbac8e..2b54d9392 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt @@ -36,7 +36,8 @@ internal object JarPluginLoaderImpl : override val configStorage: PluginDataStorage get() = MiraiConsoleImplementationBridge.configStorageForJarPluginLoader - private val logger: MiraiLogger = MiraiConsole.createLogger(JarPluginLoader::class.simpleName!!) + @JvmStatic + internal val logger: MiraiLogger = MiraiConsole.createLogger(JarPluginLoader::class.simpleName!!) override val dataStorage: PluginDataStorage get() = MiraiConsoleImplementationBridge.dataStorageForJarPluginLoader diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt index b52fac667..d26f1a6eb 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt @@ -13,6 +13,7 @@ import kotlinx.atomicfu.AtomicLong import kotlinx.atomicfu.locks.withLock import kotlinx.coroutines.* import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.data.runCatchingLog import net.mamoe.mirai.console.internal.data.mkdir import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.PluginManager @@ -43,9 +44,11 @@ internal abstract class JvmPluginInternal( // region JvmPlugin final override val logger: MiraiLogger by lazy { - MiraiConsole.createLogger( - "Plugin ${this.description.name}" - ) + JarPluginLoaderImpl.logger.runCatchingLog { + MiraiConsole.createLogger( + "Plugin ${this.description.name}" + ) + }.getOrThrow() } private var firstRun = true @@ -97,6 +100,7 @@ internal abstract class JvmPluginInternal( }, onFailure = { cancel(CancellationException("Exception while enabling plugin", it)) + logger.error(it) return false } )