From b305c7665c798ff8960747bfd2e1837f76a12142 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Fri, 7 Apr 2023 20:21:10 +0900 Subject: [PATCH] Improve the "failed to download" error message Example: ```yaml images: - location: "https://example.com/dummy-x86_64" arch: "x86_64" - location: "https://example.com/dummy-aarch64" arch: "aarch64" ``` Before: ```console $ limactl start dummy INFO[0000] Using the existing instance "dummy" INFO[0000] Attempting to download the image arch=x86_64 digest= location="https://example.com/dummy-x86_64" FATA[0000] failed to download the image, attempted 2 candidates, errors=[failed to download "https://example.com/dummy-x86_64": expected HTTP status 200, got 404 Not Found unsupported arch: "aarch64"] ``` After: ```console $ limactl start dummy INFO[0000] Using the existing instance "dummy" INFO[0000] Attempting to download the image arch=x86_64 digest= location="https://example.com/dummy-x86_64" FATA[0000] failed to download "https://example.com/dummy-x86_64": expected HTTP status 200, got 404 Not Found ``` Signed-off-by: Akihiro Suda --- pkg/fileutils/download.go | 28 +++++++++++++++++++++++++++- pkg/qemu/qemu.go | 3 +-- pkg/start/start.go | 3 +-- pkg/vz/disk.go | 3 +-- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/pkg/fileutils/download.go b/pkg/fileutils/download.go index a973dc02de8..7d8d2aded72 100644 --- a/pkg/fileutils/download.go +++ b/pkg/fileutils/download.go @@ -1,6 +1,7 @@ package fileutils import ( + "errors" "fmt" "path" @@ -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) @@ -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 +} diff --git a/pkg/qemu/qemu.go b/pkg/qemu/qemu.go index a9193601563..81a42de84dc 100644 --- a/pkg/qemu/qemu.go +++ b/pkg/qemu/qemu.go @@ -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) diff --git a/pkg/start/start.go b/pkg/start/start.go index 70e8c377057..3c8b9f772cc 100644 --- a/pkg/start/start.go +++ b/pkg/start/start.go @@ -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 { diff --git a/pkg/vz/disk.go b/pkg/vz/disk.go index 9fb8fad24e7..557361273bb 100644 --- a/pkg/vz/disk.go +++ b/pkg/vz/disk.go @@ -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)