Fix #139, rethrow exceptions caught in JvmPluginInternal.onEnable

This commit is contained in:
Him188 2020-08-31 13:09:02 +08:00
parent 3001de5834
commit 2ea86b377a
3 changed files with 12 additions and 6 deletions

View File

@ -143,14 +143,15 @@ internal val debuggingLogger1 by lazy {
DefaultLogger("debug").withSwitch(true)
}
internal inline fun <R> MiraiLogger.runCatchingLog(message: String? = null, block: () -> R): R? {
@Suppress("RESULT_CLASS_IN_RETURN_TYPE")
internal inline fun <R> MiraiLogger.runCatchingLog(message: String? = null, block: () -> R): Result<R> {
return kotlin.runCatching {
block()
}.onFailure {
if (message != null) {
error(message, it)
} else error(it)
}.getOrNull()
}
}
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")

View File

@ -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

View File

@ -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
}
)