pkg/utils: Address the confusion around handling errors from Viper
It turns out that Viper's custom error implementations use non-pointer receivers, whereas often people assume pointer receivers. This can cause confusion when trying to use errors.As(...) with those errors [1]. Secondly, Viper may or may not throw ConfigFileNotFoundError depending on its build tags. [1] https://github.com/spf13/viper/issues/1139 https://github.com/containers/toolbox/pull/1105
This commit is contained in:
parent
53c5694040
commit
00def007f5
1 changed files with 7 additions and 15 deletions
|
@ -572,27 +572,19 @@ func SetUpConfiguration() error {
|
|||
viper.SetConfigFile(configFile)
|
||||
|
||||
if err := viper.MergeInConfig(); err != nil {
|
||||
// Seems like Viper's errors can't be examined with
|
||||
// errors.As.
|
||||
var errConfigFileNotFound viper.ConfigFileNotFoundError
|
||||
var errConfigParse viper.ConfigParseError
|
||||
|
||||
// Seems like Viper doesn't actually throw
|
||||
// viper.ConfigFileNotFoundError if a configuration
|
||||
// file is not found. We still check for it for the
|
||||
// sake of completion or in case Viper uses it in a
|
||||
// different version.
|
||||
_, ok := err.(viper.ConfigFileNotFoundError)
|
||||
if ok || os.IsNotExist(err) {
|
||||
if errors.As(err, &errConfigFileNotFound) || os.IsNotExist(err) {
|
||||
logrus.Debugf("Setting up configuration: file %s not found", configFile)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := err.(viper.ConfigParseError); ok {
|
||||
} else if errors.As(err, &errConfigParse) {
|
||||
logrus.Debugf("Setting up configuration: failed to parse file %s: %s", configFile, err)
|
||||
return fmt.Errorf("failed to parse file %s", configFile)
|
||||
} else {
|
||||
logrus.Debugf("Setting up configuration: failed to read file %s: %s", configFile, err)
|
||||
return fmt.Errorf("failed to read file %s", configFile)
|
||||
}
|
||||
|
||||
logrus.Debugf("Setting up configuration: failed to read file %s: %s", configFile, err)
|
||||
return fmt.Errorf("failed to read file %s", configFile)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue