refactor(config): 改进配置读取错误时输出的错误信息.

改进后的信息有助于让用户了解到底发生了什么, 可帮助用户找到出错的配置文件并修复错误的配置格式.
This commit is contained in:
LamGC 2022-06-11 16:19:08 +08:00
parent ac0a398afc
commit 9c05726849
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 16 additions and 6 deletions

View File

@ -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 {
configFile.bufferedReader(StandardCharsets.UTF_8).use { try {
return GsonConst.appConfigGson.fromJson(it, AppConfig::class.java)!! configFile.bufferedReader(StandardCharsets.UTF_8).use {
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>? {
botConfigFile.bufferedReader(StandardCharsets.UTF_8).use { try {
return GsonConst.botConfigGson.fromJson(it, object : TypeToken<Set<BotConfig>>() {}.type)!! botConfigFile.bufferedReader(StandardCharsets.UTF_8).use {
return GsonConst.botConfigGson.fromJson(it, object : TypeToken<Set<BotConfig>>() {}.type)!!
}
} catch (e: Exception) {
log.error(e) { "读取 Bot 配置文件 (bot.json) 时发生错误, 请检查配置格式是否正确." }
return null
} }
} }

View File

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