|
| 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 | +} |
0 commit comments