Fix inconsistent exceptions specified in docs and actually thrown.

This commit is contained in:
Him188 2020-11-25 18:29:03 +08:00
parent 8f61943c70
commit 73533c37cb
2 changed files with 10 additions and 4 deletions

View File

@ -20,6 +20,7 @@ import net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices
import net.mamoe.mirai.console.plugin.jvm.*
import net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader
import net.mamoe.mirai.console.plugin.loader.PluginLoadException
import net.mamoe.mirai.console.plugin.name
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
import net.mamoe.mirai.utils.MiraiLogger
import java.io.File
@ -93,12 +94,17 @@ internal object BuiltInJvmPluginLoaderImpl :
return filePlugins.toSet().map { it.value }
}
private val loadedPlugins = ConcurrentHashMap<JvmPlugin, Unit>()
@Throws(PluginLoadException::class)
override fun load(plugin: JvmPlugin) {
ensureActive()
if (loadedPlugins.put(plugin, Unit) != null) {
error("Plugin '${plugin.name}' is already loaded and cannot be reloaded.")
}
runCatching {
check(plugin is JvmPluginInternal) { "A JvmPlugin must extend AbstractJvmPlugin" }
check(plugin is JvmPluginInternal) { "A JvmPlugin must extend AbstractJvmPlugin to be loaded by JvmPluginLoader.BuiltIn" }
plugin.internalOnLoad(plugin.componentStorage)
}.getOrElse {
throw PluginLoadException("Exception while loading ${plugin.description.name}", it)
@ -106,7 +112,7 @@ internal object BuiltInJvmPluginLoaderImpl :
}
override fun enable(plugin: JvmPlugin) {
if (plugin.isEnabled) return
if (plugin.isEnabled) error("Plugin '${plugin.name}' is already enabled and cannot be re-enabled.")
ensureActive()
runCatching {
if (plugin is JvmPluginInternal) {
@ -118,7 +124,7 @@ internal object BuiltInJvmPluginLoaderImpl :
}
override fun disable(plugin: JvmPlugin) {
if (!plugin.isEnabled) return
if (!plugin.isEnabled) error("Plugin '${plugin.name}' is not already disabled and cannot be re-disabled.")
if (MiraiConsole.isActive)
ensureActive()

View File

@ -76,7 +76,7 @@ public interface PluginLoader<P : Plugin, D : PluginDescription> {
* @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等).
* @throws IllegalStateException 在插件已经被加载时抛出. 这属于意料之外的情况.
*/
@Throws(PluginLoadException::class)
@Throws(IllegalStateException::class, PluginLoadException::class)
public fun load(plugin: P)
/**