Unverified Commit f46cf1e5 authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

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
parent b678f6f2
......@@ -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"
......@@ -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"
)
......@@ -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"
}
......
......@@ -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.
......@@ -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}
......
......@@ -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
......@@ -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,
......
......@@ -223,6 +223,9 @@ enum FailedInstanceInitReason {
// FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR failure encountered while
// loading a tool
FAILED_INSTANCE_INIT_REASON_TOOL_LOAD_ERROR = 3;
// FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR failure encountered while
// downloading an index
FAILED_INSTANCE_INIT_REASON_INDEX_DOWNLOAD_ERROR = 4;
}
message FailedInstanceInitError {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment