Skip to content

Commit f46cf1e

Browse files
authored
Fixed some error messages/warnings during index download (#2257)
* Added FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR in gRPC Init errors * Improved error reporting during Init and first-index-update
1 parent b678f6f commit f46cf1e

File tree

5 files changed

+322
-288
lines changed

5 files changed

+322
-288
lines changed

arduino/cores/packagemanager/package_manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"sync"
2727
"time"
2828

29+
"github.com/arduino/arduino-cli/arduino"
2930
"github.com/arduino/arduino-cli/arduino/cores"
3031
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
3132
"github.com/arduino/arduino-cli/arduino/discovery/discoverymanager"
@@ -34,6 +35,7 @@ import (
3435
paths "github.com/arduino/go-paths-helper"
3536
properties "github.com/arduino/go-properties-orderedmap"
3637
"github.com/arduino/go-timeutils"
38+
"github.com/pkg/errors"
3739
"github.com/sirupsen/logrus"
3840
semver "go.bug.st/relaxed-semver"
3941
)
@@ -438,6 +440,9 @@ func (pme *Explorer) determineReferencedPlatformRelease(boardBuildProperties *pr
438440
// LoadPackageIndex loads a package index by looking up the local cached file from the specified URL
439441
func (pmb *Builder) LoadPackageIndex(URL *url.URL) error {
440442
indexFileName := path.Base(URL.Path)
443+
if indexFileName == "." || indexFileName == "" {
444+
return &arduino.InvalidURLError{Cause: errors.New(URL.String())}
445+
}
441446
if strings.HasSuffix(indexFileName, ".tar.bz2") {
442447
indexFileName = strings.TrimSuffix(indexFileName, ".tar.bz2") + ".json"
443448
}

arduino/resources/index.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ type IndexResource struct {
3838
}
3939

4040
// IndexFileName returns the index file name as it is saved in data dir (package_xxx_index.json).
41-
func (res *IndexResource) IndexFileName() string {
41+
func (res *IndexResource) IndexFileName() (string, error) {
4242
filename := path.Base(res.URL.Path) // == package_index.json[.gz] || packacge_index.tar.bz2
43+
if filename == "." || filename == "" {
44+
return "", &arduino.InvalidURLError{}
45+
}
4346
if i := strings.Index(filename, "."); i != -1 {
4447
filename = filename[:i]
4548
}
46-
return filename + ".json"
49+
return filename + ".json", nil
4750
}
4851

4952
// Download will download the index and possibly check the signature using the Arduino's public key.
@@ -63,7 +66,10 @@ func (res *IndexResource) Download(destDir *paths.Path, downloadCB rpc.DownloadP
6366

6467
// Download index file
6568
downloadFileName := path.Base(res.URL.Path) // == package_index.json[.gz] || package_index.tar.bz2
66-
indexFileName := res.IndexFileName() // == package_index.json
69+
indexFileName, err := res.IndexFileName() // == package_index.json
70+
if err != nil {
71+
return err
72+
}
6773
tmpIndexPath := tmp.Join(downloadFileName)
6874
if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), "", tr("Downloading index: %s", downloadFileName), downloadCB, nil, downloader.NoResume); err != nil {
6975
return &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", res.URL), Cause: err}

commands/instances.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,14 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
280280
allPackageIndexUrls = append(allPackageIndexUrls, URL)
281281
}
282282
}
283-
firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls)
283+
if err := firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls); err != nil {
284+
e := &arduino.InitFailedError{
285+
Code: codes.InvalidArgument,
286+
Cause: err,
287+
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR,
288+
}
289+
responseError(e.ToRPCStatus())
290+
}
284291

285292
{
286293
// We need to rebuild the PackageManager currently in use by this instance
@@ -589,7 +596,12 @@ func firstUpdate(ctx context.Context, instance *rpc.Instance, downloadCb func(ms
589596
if URL.Scheme == "file" {
590597
continue
591598
}
592-
packageIndexFileName := (&resources.IndexResource{URL: URL}).IndexFileName()
599+
packageIndexFileName, err := (&resources.IndexResource{URL: URL}).IndexFileName()
600+
if err != nil {
601+
return &arduino.FailedDownloadError{
602+
Message: tr("Error downloading index '%s'", URL),
603+
Cause: &arduino.InvalidURLError{}}
604+
}
593605
packageIndexFile := dataDir.Join(packageIndexFileName)
594606
if packageIndexFile.NotExist() {
595607
// The index file doesn't exists, that means the CLI is run for the first time,

0 commit comments

Comments
 (0)