Skip to content

Commit

Permalink
Fixed some error messages/warnings during index download (#2257)
Browse files Browse the repository at this point in the history
* Added FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR in gRPC Init errors

* Improved error reporting during Init and first-index-update
  • Loading branch information
cmaglie authored Aug 3, 2023
1 parent b678f6f commit f46cf1e
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 288 deletions.
5 changes: 5 additions & 0 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync"
"time"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-cli/arduino/discovery/discoverymanager"
Expand All @@ -34,6 +35,7 @@ import (
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/arduino/go-timeutils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
)
Expand Down Expand Up @@ -438,6 +440,9 @@ func (pme *Explorer) determineReferencedPlatformRelease(boardBuildProperties *pr
// LoadPackageIndex loads a package index by looking up the local cached file from the specified URL
func (pmb *Builder) LoadPackageIndex(URL *url.URL) error {
indexFileName := path.Base(URL.Path)
if indexFileName == "." || indexFileName == "" {
return &arduino.InvalidURLError{Cause: errors.New(URL.String())}
}
if strings.HasSuffix(indexFileName, ".tar.bz2") {
indexFileName = strings.TrimSuffix(indexFileName, ".tar.bz2") + ".json"
}
Expand Down
12 changes: 9 additions & 3 deletions arduino/resources/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ type IndexResource struct {
}

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

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

// Download index file
downloadFileName := path.Base(res.URL.Path) // == package_index.json[.gz] || package_index.tar.bz2
indexFileName := res.IndexFileName() // == package_index.json
indexFileName, err := res.IndexFileName() // == package_index.json
if err != nil {
return err
}
tmpIndexPath := tmp.Join(downloadFileName)
if err := httpclient.DownloadFile(tmpIndexPath, res.URL.String(), "", tr("Downloading index: %s", downloadFileName), downloadCB, nil, downloader.NoResume); err != nil {
return &arduino.FailedDownloadError{Message: tr("Error downloading index '%s'", res.URL), Cause: err}
Expand Down
16 changes: 14 additions & 2 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,14 @@ func Init(req *rpc.InitRequest, responseCallback func(r *rpc.InitResponse)) erro
allPackageIndexUrls = append(allPackageIndexUrls, URL)
}
}
firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls)
if err := firstUpdate(context.Background(), req.GetInstance(), downloadCallback, allPackageIndexUrls); err != nil {
e := &arduino.InitFailedError{
Code: codes.InvalidArgument,
Cause: err,
Reason: rpc.FailedInstanceInitReason_FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR,
}
responseError(e.ToRPCStatus())
}

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

0 comments on commit f46cf1e

Please sign in to comment.