fix: improve error handling in InitEnv function to only panic for non-config file not found errors

This commit is contained in:
keven1024
2025-06-03 11:04:02 +08:00
parent 58c9b694d3
commit 304aa45b52
2 changed files with 8 additions and 2 deletions

View File

@@ -21,8 +21,11 @@ func InitEnv() {
v.AddConfigPath("../")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)
}
}
}
func GetEnv(key string) string {

View File

@@ -21,8 +21,11 @@ func InitEnv() {
v.AddConfigPath("../")
v.AutomaticEnv()
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// 只有当错误不是"配置文件未找到"时才 panic
panic(err)
}
}
}
func GetEnv(key string) string {