config manager: Improve source-fetch error handling (#967)
config manager: Improve source-fetch error handling * Allow detection of source-download errors by adding IsSourceDownloadError method. * Treat non-success response codes as a failure to fetch the source. * Set the Source url before errors happens so it can be consulted even in that case.
This commit is contained in:
parent
e1faf12bae
commit
ce3c50187e
|
@ -19,12 +19,24 @@ import (
|
|||
|
||||
var logger = logging.Logger("config")
|
||||
|
||||
var (
|
||||
// Error when downloading a Source-based configuration
|
||||
errFetchingSource = errors.New("could not fetch configuration from source")
|
||||
// Error when remote source points to another remote-source
|
||||
errSourceRedirect = errors.New("a sourced configuration cannot point to another source")
|
||||
)
|
||||
|
||||
// IsErrFetchingSource reports whether this error happened when trying to
|
||||
// fetch a remote configuration source (as opposed to an error parsing the
|
||||
// config).
|
||||
func IsErrFetchingSource(err error) bool {
|
||||
return errors.Is(err, errFetchingSource)
|
||||
}
|
||||
|
||||
// ConfigSaveInterval specifies how often to save the configuration file if
|
||||
// it needs saving.
|
||||
var ConfigSaveInterval = time.Second
|
||||
|
||||
var errSourceRedirect = errors.New("a sourced configuration cannot point to another source")
|
||||
|
||||
// The ComponentConfig interface allows components to define configurations
|
||||
// which can be managed as part of the ipfs-cluster configuration file by the
|
||||
// Manager.
|
||||
|
@ -339,16 +351,16 @@ func (cfg *Manager) LoadJSONFromFile(path string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
err = cfg.LoadJSON(file)
|
||||
return err
|
||||
return cfg.LoadJSON(file)
|
||||
}
|
||||
|
||||
// LoadJSONFromHTTPSource reads a Configuration file from a URL and parses it.
|
||||
func (cfg *Manager) LoadJSONFromHTTPSource(url string) error {
|
||||
logger.Infof("loading configuration from %s", url)
|
||||
cfg.Source = url
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
return errFetchingSource
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
@ -356,6 +368,10 @@ func (cfg *Manager) LoadJSONFromHTTPSource(url string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("unsuccessful request (%d): %s", resp.StatusCode, body)
|
||||
}
|
||||
|
||||
// Avoid recursively loading remote sources
|
||||
if cfg.sourceRedirs > 0 {
|
||||
return errSourceRedirect
|
||||
|
@ -368,7 +384,6 @@ func (cfg *Manager) LoadJSONFromHTTPSource(url string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.Source = url
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user