Skip to content

Commit d8387f6

Browse files
spacez320sagikazarmark
authored andcommitted
refactor: segregate errors, config file not found interface
1 parent 93dc9f6 commit d8387f6

File tree

4 files changed

+90
-48
lines changed

4 files changed

+90
-48
lines changed

‎errors.go‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package viper
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
/* ConfigFileNotFoundError types */
8+
9+
// For matching on any ConfigFileNotFoundFrom*Error
10+
type ConfigFileNotFoundError interface {
11+
Error() string
12+
Name() string
13+
Locations() string
14+
}
15+
16+
// ConfigFileNotFoundError denotes failing to find configuration file.
17+
type ConfigFileNotFoundFromFinderError struct {
18+
name, locations string
19+
}
20+
21+
// Error returns the formatted configuration error.
22+
func (fnfe ConfigFileNotFoundFromFinderError) Error() string {
23+
message := fmt.Sprintf("Config file %q Not Found", fnfe.name)
24+
if fnfe.locations != "" {
25+
message += fmt.Sprintf(" in %q", fnfe.locations)
26+
}
27+
28+
return message
29+
}
30+
31+
func (fnfe ConfigFileNotFoundFromFinderError) Name() string {
32+
return fnfe.name
33+
}
34+
35+
func (fnfe ConfigFileNotFoundFromFinderError) Locations() string {
36+
return fnfe.locations
37+
}
38+
39+
// ConfigFileNotFoundError denotes failing to find configuration file.
40+
type ConfigFileNotFoundFromReadError struct {
41+
name string
42+
}
43+
44+
// Error returns the formatted configuration error.
45+
func (fnfe ConfigFileNotFoundFromReadError) Error() string {
46+
return fmt.Sprintf("Config file %q Not Found", fnfe.name)
47+
}
48+
49+
func (fnfe ConfigFileNotFoundFromReadError) Name() string {
50+
return fnfe.name
51+
}
52+
53+
func (fnfe ConfigFileNotFoundFromReadError) Locations() string {
54+
return ""
55+
}
56+
57+
/* Other error types */
58+
59+
// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
60+
type ConfigFileAlreadyExistsError string
61+
62+
// Error returns the formatted error when configuration already exists.
63+
func (faee ConfigFileAlreadyExistsError) Error() string {
64+
return fmt.Sprintf("Config File %q Already Exists", string(faee))
65+
}
66+
67+
// ConfigMarshalError happens when failing to marshal the configuration.
68+
type ConfigMarshalError struct {
69+
err error
70+
}
71+
72+
// Error returns the formatted configuration error.
73+
func (e ConfigMarshalError) Error() string {
74+
return fmt.Sprintf("While marshaling config: %s", e.err.Error())
75+
}
76+
77+
// UnsupportedConfigError denotes encountering an unsupported
78+
// configuration filetype.
79+
type UnsupportedConfigError string
80+
81+
// Error returns the formatted configuration error.
82+
func (str UnsupportedConfigError) Error() string {
83+
return fmt.Sprintf("Unsupported Config Type %q", string(str))
84+
}

‎file.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (v *Viper) findConfigFileWithFinder(finder Finder) (string, error) {
5050
}
5151

5252
if len(results) == 0 {
53-
return "", ConfigFileNotFoundError{
53+
return "", ConfigFileNotFoundFromFinderError{
5454
name: v.configName, locations: fmt.Sprintf("%s", v.configPaths),
5555
}
5656
}
@@ -71,7 +71,7 @@ func (v *Viper) findConfigFileOld() (string, error) {
7171
return file, nil
7272
}
7373
}
74-
return "", ConfigFileNotFoundError{
74+
return "", ConfigFileNotFoundFromFinderError{
7575
name: v.configName, locations: fmt.Sprintf("%s", v.configPaths),
7676
}
7777
}

‎viper.go‎

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,12 @@ import (
4444
"github.com/spf13/viper/internal/features"
4545
)
4646

47-
// ConfigMarshalError happens when failing to marshal the configuration.
48-
type ConfigMarshalError struct {
49-
err error
50-
}
51-
52-
// Error returns the formatted configuration error.
53-
func (e ConfigMarshalError) Error() string {
54-
return fmt.Sprintf("While marshaling config: %s", e.err.Error())
55-
}
56-
5747
var v *Viper
5848

5949
func init() {
6050
v = New()
6151
}
6252

63-
// UnsupportedConfigError denotes encountering an unsupported
64-
// configuration filetype.
65-
type UnsupportedConfigError string
66-
67-
// Error returns the formatted configuration error.
68-
func (str UnsupportedConfigError) Error() string {
69-
return fmt.Sprintf("Unsupported Config Type %q", string(str))
70-
}
71-
72-
// ConfigFileNotFoundError denotes failing to find configuration file.
73-
type ConfigFileNotFoundError struct {
74-
name, locations string
75-
}
76-
77-
// Error returns the formatted configuration error.
78-
func (fnfe ConfigFileNotFoundError) Error() string {
79-
message := fmt.Sprintf("Config file %q Not Found", fnfe.name)
80-
if fnfe.locations != "" {
81-
message += fmt.Sprintf(" in %q", fnfe.locations)
82-
}
83-
84-
return message
85-
}
86-
87-
// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
88-
type ConfigFileAlreadyExistsError string
89-
90-
// Error returns the formatted error when configuration already exists.
91-
func (faee ConfigFileAlreadyExistsError) Error() string {
92-
return fmt.Sprintf("Config File %q Already Exists", string(faee))
93-
}
94-
9553
// A DecoderConfigOption can be passed to viper.Unmarshal to configure
9654
// mapstructure.DecoderConfig options.
9755
type DecoderConfigOption func(*mapstructure.DecoderConfig)
@@ -1537,7 +1495,7 @@ func (v *Viper) ReadInConfig() error {
15371495
return err
15381496
}
15391497
if !exists {
1540-
return ConfigFileNotFoundError{name: filename, locations: ""}
1498+
return ConfigFileNotFoundFromReadError{name: filename}
15411499
}
15421500
file, err := afero.ReadFile(v.fs, filename)
15431501
if err != nil {

‎viper_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ func TestWrongConfigWithSetConfigFileNotFound(t *testing.T) {
15831583
v.SetConfigFile(`whatareyoutalkingabout.yaml`)
15841584

15851585
err := v.ReadInConfig()
1586-
assert.IsType(t, ConfigFileNotFoundError{name: "", locations: ""}, err)
1586+
assert.IsType(t, ConfigFileNotFoundFromReadError{}, err)
15871587

15881588
// Even though config did not load and the error might have
15891589
// been ignored by the client, the default still loads
@@ -1669,7 +1669,7 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
16691669
v.AddConfigPath(`thispathaintthere`)
16701670

16711671
err := v.ReadInConfig()
1672-
assert.IsType(t, ConfigFileNotFoundError{name: "", locations: ""}, err)
1672+
assert.IsType(t, ConfigFileNotFoundFromFinderError{name: "", locations: ""}, err)
16731673

16741674
// Even though config did not load and the error might have
16751675
// been ignored by the client, the default still loads
@@ -1687,7 +1687,7 @@ func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
16871687
v.AddConfigPath(`thispathaintthere`)
16881688

16891689
err := v.MergeInConfig()
1690-
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{name: "", locations: ""}), reflect.TypeOf(err))
1690+
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundFromFinderError{name: "", locations: ""}), reflect.TypeOf(err))
16911691

16921692
// Even though config did not load and the error might have
16931693
// been ignored by the client, the default still loads

0 commit comments

Comments
 (0)