2025-08-14 07:36:02 -04:00
|
|
|
package viper
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-18 15:16:36 -04:00
|
|
|
// 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
|
|
|
|
|
// file does not exist or because it cannot find any file matching finder criteria.
|
|
|
|
|
type FileLookupError interface {
|
|
|
|
|
error
|
|
|
|
|
|
|
|
|
|
fileLookup()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 10:19:26 -04:00
|
|
|
// ConfigFileNotFoundError denotes failing to find a configuration file from a search.
|
|
|
|
|
//
|
2025-10-08 11:57:41 +02:00
|
|
|
// Deprecated: This is error wraps [FileNotFoundFromSearchError], which should be used instead.
|
2025-08-17 10:19:26 -04:00
|
|
|
type ConfigFileNotFoundError struct {
|
|
|
|
|
locations []string
|
2025-08-18 15:16:36 -04:00
|
|
|
name string
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 10:19:26 -04:00
|
|
|
// Error returns the formatted error.
|
2025-08-18 12:44:17 -04:00
|
|
|
func (e ConfigFileNotFoundError) Error() string {
|
2025-10-03 19:11:57 -04:00
|
|
|
return e.Unwrap().Error()
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 10:19:26 -04:00
|
|
|
// Unwraps to FileNotFoundFromSearchError.
|
2025-08-18 15:23:49 -04:00
|
|
|
func (e ConfigFileNotFoundError) Unwrap() error {
|
2025-09-26 21:50:57 +02:00
|
|
|
return FileNotFoundFromSearchError(e)
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 10:19:26 -04:00
|
|
|
// FileNotFoundFromSearchError denotes failing to find a configuration file from a search.
|
|
|
|
|
// Wraps ConfigFileNotFoundError.
|
|
|
|
|
type FileNotFoundFromSearchError struct {
|
2025-08-18 15:16:36 -04:00
|
|
|
locations []string
|
|
|
|
|
name string
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-26 21:50:57 +02:00
|
|
|
func (e FileNotFoundFromSearchError) fileLookup() {}
|
2025-08-14 07:36:02 -04:00
|
|
|
|
2025-08-18 15:23:49 -04:00
|
|
|
// Error returns the formatted error.
|
|
|
|
|
func (e FileNotFoundFromSearchError) Error() string {
|
2025-10-03 19:11:57 -04:00
|
|
|
message := fmt.Sprintf("File %q not found", e.name)
|
2025-09-26 21:50:57 +02:00
|
|
|
|
|
|
|
|
if len(e.locations) > 0 {
|
|
|
|
|
message += fmt.Sprintf(" in %v", e.locations)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return message
|
2025-08-18 15:16:36 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 10:19:26 -04:00
|
|
|
// FileNotFoundError denotes failing to find a specific configuration file.
|
|
|
|
|
type FileNotFoundError struct {
|
2025-08-18 15:23:49 -04:00
|
|
|
err error
|
2025-08-17 10:19:26 -04:00
|
|
|
path string
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
2025-09-26 21:50:57 +02:00
|
|
|
func (e FileNotFoundError) fileLookup() {}
|
2025-08-14 07:36:02 -04:00
|
|
|
|
2025-08-18 15:23:49 -04:00
|
|
|
// Error returns the formatted error.
|
|
|
|
|
func (e FileNotFoundError) Error() string {
|
|
|
|
|
return fmt.Sprintf("file not found: %s", e.path)
|
2025-08-18 15:16:36 -04:00
|
|
|
}
|
|
|
|
|
|
2025-08-14 07:36:02 -04:00
|
|
|
// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
|
|
|
|
|
type ConfigFileAlreadyExistsError string
|
|
|
|
|
|
|
|
|
|
// Error returns the formatted error when configuration already exists.
|
2025-08-18 15:23:49 -04:00
|
|
|
func (e ConfigFileAlreadyExistsError) Error() string {
|
|
|
|
|
return fmt.Sprintf("Config File %q Already Exists", string(e))
|
2025-08-14 07:36:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ConfigMarshalError happens when failing to marshal the configuration.
|
|
|
|
|
type ConfigMarshalError struct {
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Error returns the formatted configuration error.
|
|
|
|
|
func (e ConfigMarshalError) Error() string {
|
|
|
|
|
return fmt.Sprintf("While marshaling config: %s", e.err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnsupportedConfigError denotes encountering an unsupported
|
|
|
|
|
// configuration filetype.
|
|
|
|
|
type UnsupportedConfigError string
|
|
|
|
|
|
|
|
|
|
// Error returns the formatted configuration error.
|
|
|
|
|
func (str UnsupportedConfigError) Error() string {
|
|
|
|
|
return fmt.Sprintf("Unsupported Config Type %q", string(str))
|
|
|
|
|
}
|