Improve index updating error handling

This commit is contained in:
Daniel 2021-06-02 10:54:30 +02:00
parent 2fe8f38ac8
commit 8889e70c8b

View file

@ -15,20 +15,26 @@ import (
"github.com/safing/portbase/log"
)
// UpdateIndexes downloads all indexes and returns the first error encountered.
// UpdateIndexes downloads all indexes. An error is only returned when all
// indexes fail to update.
func (reg *ResourceRegistry) UpdateIndexes(ctx context.Context) error {
var firstErr error
var lastErr error
var anySuccess bool
client := &http.Client{}
for _, idx := range reg.getIndexes() {
if err := reg.downloadIndex(ctx, client, idx); err != nil {
if firstErr == nil {
firstErr = err
}
lastErr = err
log.Warningf("%s: failed to update index %s: %s", reg.Name, idx.Path, err)
} else {
anySuccess = true
}
}
return firstErr
if !anySuccess {
return fmt.Errorf("failed to update all indexes, last error was: %s", lastErr)
}
return nil
}
func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Client, idx Index) error {