mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
refactor(config): 改进配置读取错误时输出的错误信息.
改进后的信息有助于让用户了解到底发生了什么, 可帮助用户找到出错的配置文件并修复错误的配置格式.
This commit is contained in:
parent
ac0a398afc
commit
9c05726849
@ -331,13 +331,23 @@ private object GsonConst {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal fun loadAppConfig(configFile: File = AppPaths.DEFAULT_CONFIG_APPLICATION.file): AppConfig {
|
internal fun loadAppConfig(configFile: File = AppPaths.DEFAULT_CONFIG_APPLICATION.file): AppConfig {
|
||||||
|
try {
|
||||||
configFile.bufferedReader(StandardCharsets.UTF_8).use {
|
configFile.bufferedReader(StandardCharsets.UTF_8).use {
|
||||||
return GsonConst.appConfigGson.fromJson(it, AppConfig::class.java)!!
|
return GsonConst.appConfigGson.fromJson(it, AppConfig::class.java)!!
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log.error { "读取 config.json 时发生错误, 请检查配置格式是否正确." }
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun loadBotConfig(botConfigFile: File = AppPaths.DEFAULT_CONFIG_BOT.file): Set<BotConfig> {
|
internal fun loadBotConfig(botConfigFile: File = AppPaths.DEFAULT_CONFIG_BOT.file): Set<BotConfig>? {
|
||||||
|
try {
|
||||||
botConfigFile.bufferedReader(StandardCharsets.UTF_8).use {
|
botConfigFile.bufferedReader(StandardCharsets.UTF_8).use {
|
||||||
return GsonConst.botConfigGson.fromJson(it, object : TypeToken<Set<BotConfig>>() {}.type)!!
|
return GsonConst.botConfigGson.fromJson(it, object : TypeToken<Set<BotConfig>>() {}.type)!!
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log.error(e) { "读取 Bot 配置文件 (bot.json) 时发生错误, 请检查配置格式是否正确." }
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun launch(): Boolean {
|
fun launch(): Boolean {
|
||||||
val botConfigs = loadBotConfig()
|
val botConfigs = loadBotConfig() ?: return false
|
||||||
if (botConfigs.isEmpty()) {
|
if (botConfigs.isEmpty()) {
|
||||||
log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." }
|
log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." }
|
||||||
return false
|
return false
|
||||||
|
Loading…
Reference in New Issue
Block a user