mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
fix: improve error handling in InitEnv function to only panic for non-config file not found errors
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user