Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the "failed to download" error message #1465

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion pkg/fileutils/download.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileutils

import (
"errors"
"fmt"
"path"

Expand All @@ -9,10 +10,13 @@ import (
"github.com/sirupsen/logrus"
)

// ErrSkipped is returned when the downloader did not attempt to download the specified file.
var ErrSkipped = errors.New("skipped to download")

// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
func DownloadFile(dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
if f.Arch != expectedArch {
return "", fmt.Errorf("unsupported arch: %q", f.Arch)
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
}
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
logrus.WithFields(fields).Infof("Attempting to download %s", description)
Expand All @@ -36,3 +40,25 @@ func DownloadFile(dest string, f limayaml.File, decompress bool, description str
}
return res.CachePath, nil
}

// Errors compose multiple into a single error.
// Errors filters out ErrSkipped.
func Errors(errs []error) error {
var finalErr error
for _, err := range errs {
if errors.Is(err, ErrSkipped) {
logrus.Debug(err)
} else {
if finalErr == nil {
finalErr = err
} else {
finalErr = fmt.Errorf("%v, %w", finalErr, err)
}
}
}
if len(errs) > 0 && finalErr == nil {
// errs only contains ErrSkipped
finalErr = fmt.Errorf("%v", errs)
}
return finalErr
}
3 changes: 1 addition & 2 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func EnsureDisk(cfg Config) error {
break
}
if !ensuredBaseDisk {
return fmt.Errorf("failed to download the image, attempted %d candidates, errors=%v",
len(cfg.LimaYAML.Images), errs)
return fileutils.Errors(errs)
}
}
diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk)
Expand Down
3 changes: 1 addition & 2 deletions pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
return path, nil
}

return "", fmt.Errorf("failed to download the nerdctl archive, attempted %d candidates, errors=%v",
len(y.Containerd.Archives), errs)
return "", fileutils.Errors(errs)
}

func Start(ctx context.Context, inst *store.Instance) error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/vz/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func EnsureDisk(driver *driver.BaseDriver) error {
break
}
if !ensuredBaseDisk {
return fmt.Errorf("failed to download the image, attempted %d candidates, errors=%v",
len(driver.Yaml.Images), errs)
return fileutils.Errors(errs)
}
}
diskSize, _ := units.RAMInBytes(*driver.Yaml.Disk)
Expand Down