Skip to content

Commit 7574cd9

Browse files
authored
Merge pull request #1465 from AkihiroSuda/dev
Improve the "failed to download" error message
2 parents 7994a5b + b305c76 commit 7574cd9

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

pkg/fileutils/download.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fileutils
22

33
import (
4+
"errors"
45
"fmt"
56
"path"
67

@@ -9,10 +10,13 @@ import (
910
"github.com/sirupsen/logrus"
1011
)
1112

13+
// ErrSkipped is returned when the downloader did not attempt to download the specified file.
14+
var ErrSkipped = errors.New("skipped to download")
15+
1216
// DownloadFile downloads a file to the cache, optionally copying it to the destination. Returns path in cache.
1317
func DownloadFile(dest string, f limayaml.File, decompress bool, description string, expectedArch limayaml.Arch) (string, error) {
1418
if f.Arch != expectedArch {
15-
return "", fmt.Errorf("unsupported arch: %q", f.Arch)
19+
return "", fmt.Errorf("%w: %q: unsupported arch: %q", ErrSkipped, f.Location, f.Arch)
1620
}
1721
fields := logrus.Fields{"location": f.Location, "arch": f.Arch, "digest": f.Digest}
1822
logrus.WithFields(fields).Infof("Attempting to download %s", description)
@@ -36,3 +40,25 @@ func DownloadFile(dest string, f limayaml.File, decompress bool, description str
3640
}
3741
return res.CachePath, nil
3842
}
43+
44+
// Errors compose multiple into a single error.
45+
// Errors filters out ErrSkipped.
46+
func Errors(errs []error) error {
47+
var finalErr error
48+
for _, err := range errs {
49+
if errors.Is(err, ErrSkipped) {
50+
logrus.Debug(err)
51+
} else {
52+
if finalErr == nil {
53+
finalErr = err
54+
} else {
55+
finalErr = fmt.Errorf("%v, %w", finalErr, err)
56+
}
57+
}
58+
}
59+
if len(errs) > 0 && finalErr == nil {
60+
// errs only contains ErrSkipped
61+
finalErr = fmt.Errorf("%v", errs)
62+
}
63+
return finalErr
64+
}

pkg/qemu/qemu.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ func EnsureDisk(cfg Config) error {
7676
break
7777
}
7878
if !ensuredBaseDisk {
79-
return fmt.Errorf("failed to download the image, attempted %d candidates, errors=%v",
80-
len(cfg.LimaYAML.Images), errs)
79+
return fileutils.Errors(errs)
8180
}
8281
}
8382
diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk)

pkg/start/start.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ func ensureNerdctlArchiveCache(y *limayaml.LimaYAML) (string, error) {
5353
return path, nil
5454
}
5555

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

6059
func Start(ctx context.Context, inst *store.Instance) error {

pkg/vz/disk.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func EnsureDisk(driver *driver.BaseDriver) error {
3737
break
3838
}
3939
if !ensuredBaseDisk {
40-
return fmt.Errorf("failed to download the image, attempted %d candidates, errors=%v",
41-
len(driver.Yaml.Images), errs)
40+
return fileutils.Errors(errs)
4241
}
4342
}
4443
diskSize, _ := units.RAMInBytes(*driver.Yaml.Disk)

0 commit comments

Comments
 (0)