refactor: apply review comments

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar
2025-10-08 12:00:40 +02:00
committed by Márk Sági-Kazár
parent b3621ce3dd
commit 196113234f
3 changed files with 27 additions and 30 deletions
+11 -14
View File
@@ -4,8 +4,6 @@ import (
"fmt"
)
/* File look-up errors */
// FileLookupError is returned when Viper cannot resolve a configuration file.
//
// This is meant to be a common interface for all file look-up errors, occurring either because a
@@ -18,7 +16,7 @@ type FileLookupError interface {
// ConfigFileNotFoundError denotes failing to find a configuration file from a search.
//
// Deprecated: This is wrapped by FileNotFoundFromSearchError, which should be used instead.
// Deprecated: This is wrapped by [FileNotFoundFromSearchError], which should be used instead.
type ConfigFileNotFoundError struct {
locations []string
name string
@@ -36,24 +34,27 @@ func (e ConfigFileNotFoundError) Error() string {
// Unwraps to FileNotFoundFromSearchError.
func (e ConfigFileNotFoundError) Unwrap() error {
return FileNotFoundFromSearchError{err: e, locations: e.locations, name: e.name}
return FileNotFoundFromSearchError(e)
}
// FileNotFoundFromSearchError denotes failing to find a configuration file from a search.
// Wraps ConfigFileNotFoundError.
type FileNotFoundFromSearchError struct {
err ConfigFileNotFoundError
locations []string
name string
}
func (e FileNotFoundFromSearchError) fileLookup() {
return
}
func (e FileNotFoundFromSearchError) fileLookup() {}
// Error returns the formatted error.
func (e FileNotFoundFromSearchError) Error() string {
return e.err.Error()
message := fmt.Sprintf("hile %q not found", e.name)
if len(e.locations) > 0 {
message += fmt.Sprintf(" in %v", e.locations)
}
return message
}
// FileNotFoundError denotes failing to find a specific configuration file.
@@ -62,17 +63,13 @@ type FileNotFoundError struct {
path string
}
func (e FileNotFoundError) fileLookup() {
return
}
func (e FileNotFoundError) fileLookup() {}
// Error returns the formatted error.
func (e FileNotFoundError) Error() string {
return fmt.Sprintf("file not found: %s", e.path)
}
/* Other error types */
// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
type ConfigFileAlreadyExistsError string
+7 -10
View File
@@ -1480,8 +1480,6 @@ func (v *Viper) Set(key string, value any) {
func ReadInConfig() error { return v.ReadInConfig() }
func (v *Viper) ReadInConfig() error {
var pathError *fs.PathError
v.logger.Info("attempting to read in config file")
filename, err := v.getConfigFile()
if err != nil {
@@ -1493,15 +1491,14 @@ func (v *Viper) ReadInConfig() error {
}
v.logger.Debug("reading file", "file", filename)
file, err := afero.ReadFile(v.fs, filename)
if err != nil {
if errors.As(err, &pathError) {
// The specified config file is missing
return FileNotFoundError{err: err, path: filename}
} else {
// We hit some other error from the filesystem that isn't a missing file
return err
}
if errors.Is(err, fs.ErrNotExist) {
// The specified config file is missing
return FileNotFoundError{err: err, path: filename}
} else if err != nil {
// We hit some other error from the filesystem that isn't a missing file
return err
}
config := make(map[string]any)
+9 -6
View File
@@ -1574,8 +1574,6 @@ func TestReadConfigWithSetConfigFile(t *testing.T) {
}
func TestWrongFileNotFound(t *testing.T) {
var fileLookupError FileLookupError
_, config := initDirs(t)
v := New()
@@ -1585,6 +1583,9 @@ func TestWrongFileNotFound(t *testing.T) {
v.SetConfigFile(`whatareyoutalkingabout.yaml`)
err := v.ReadInConfig()
var fileLookupError FileLookupError
// It matches all error types and the shared error interface.
assert.ErrorAs(t, err, &FileNotFoundError{})
assert.ErrorAs(t, err, &fileLookupError)
@@ -1663,8 +1664,6 @@ func TestDirsSearch(t *testing.T) {
}
func TestWrongDirsSearchNotFound(t *testing.T) {
var fileLookupError FileLookupError
_, config := initDirs(t)
v := New()
@@ -1675,6 +1674,9 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
v.AddConfigPath(`thispathaintthere`)
err := v.ReadInConfig()
var fileLookupError FileLookupError
// It matches all error types and the shared error interface.
assert.ErrorAs(t, err, &ConfigFileNotFoundError{})
assert.ErrorAs(t, err, &FileNotFoundFromSearchError{})
@@ -1686,8 +1688,6 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
}
func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
var fileLookupError FileLookupError
_, config := initDirs(t)
v := New()
@@ -1698,6 +1698,9 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
v.AddConfigPath(`thispathaintthere`)
err := v.MergeInConfig()
var fileLookupError FileLookupError
// It matches both types of errors.
assert.ErrorAs(t, err, &ConfigFileNotFoundError{})
assert.ErrorAs(t, err, &FileNotFoundFromSearchError{})