Skip to content

Commit e92a83b

Browse files
committed
Enhance error messages when updating indexes or local index files are missing
1 parent bcd1a3e commit e92a83b

File tree

8 files changed

+91
-25
lines changed

8 files changed

+91
-25
lines changed

Diff for: cli/instance/instance.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package instance
1717

1818
import (
1919
"context"
20+
"fmt"
21+
"strings"
2022

2123
"github.com/arduino/arduino-cli/cli/output"
2224
"github.com/arduino/arduino-cli/commands"
@@ -126,8 +128,7 @@ func checkPlatformErrors(resp *rpc.InitResp) error {
126128
for _, err := range resp.GetPlatformsIndexErrors() {
127129
logrus.Errorf("Error loading platform index: %v", err)
128130
}
129-
// return
130-
return errors.New("There were errors loading platform indexes")
131+
return fmt.Errorf("error loading platform index: \n%v", strings.Join(resp.GetPlatformsIndexErrors(), "\n"))
131132
}
132133

133134
return nil

Diff for: commands/download.go

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package commands
1717

1818
import (
19+
"errors"
1920
"time"
2021

2122
"github.com/arduino/arduino-cli/httpclient"
@@ -60,6 +61,10 @@ func Download(d *downloader.Downloader, label string, downloadCB DownloadProgres
6061
if d.Error() != nil {
6162
return d.Error()
6263
}
64+
// The URL is not reachable for some reason
65+
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
66+
return errors.New(d.Resp.Status)
67+
}
6368
downloadCB(&rpc.DownloadProgress{Completed: true})
6469
return nil
6570
}

Diff for: commands/instances.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexReq, downloadCB Downlo
230230
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
231231
}
232232
coreIndexPath := indexpath.Join(path.Base(URL.Path))
233-
Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
234-
if d.Error() != nil {
235-
return nil, fmt.Errorf("downloading index %s: %s", URL, d.Error())
233+
err = Download(d, "Updating index: "+coreIndexPath.Base(), downloadCB)
234+
if err != nil {
235+
return nil, fmt.Errorf("downloading index %s: %s", URL, err)
236236
}
237237

238238
// Check for signature

Diff for: go.mod

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ require (
2121
github.com/h2non/filetype v1.0.8 // indirect
2222
github.com/imjasonmiller/godice v0.1.2
2323
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
24-
github.com/kr/pretty v0.2.0 // indirect
2524
github.com/kr/text v0.2.0 // indirect
2625
github.com/leonelquinteros/gotext v1.4.0
27-
github.com/marcinbor85/gohex v0.0.0-20200531163658-baab2527a9a2
2826
github.com/mattn/go-colorable v0.1.2
2927
github.com/mattn/go-isatty v0.0.8
3028
github.com/mattn/go-runewidth v0.0.9 // indirect
3129
github.com/miekg/dns v1.0.5 // indirect
32-
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
3330
github.com/oleksandr/bonjour v0.0.0-20160508152359-5dcf00d8b228 // indirect
3431
github.com/pkg/errors v0.9.1
3532
github.com/pmylund/sortutil v0.0.0-20120526081524-abeda66eb583
@@ -40,11 +37,10 @@ require (
4037
github.com/sirupsen/logrus v1.4.2
4138
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
4239
github.com/spf13/jwalterweatherman v1.0.0
43-
github.com/spf13/pflag v1.0.3
4440
github.com/spf13/viper v1.6.2
4541
github.com/stretchr/testify v1.6.1
4642
go.bug.st/cleanup v1.0.0
47-
go.bug.st/downloader/v2 v2.0.1
43+
go.bug.st/downloader/v2 v2.1.0
4844
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
4945
go.bug.st/serial v1.1.1
5046
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
@@ -53,7 +49,6 @@ require (
5349
golang.org/x/text v0.3.2
5450
google.golang.org/grpc v1.27.0
5551
google.golang.org/protobuf v1.25.0
56-
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
5752
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
5853
gopkg.in/src-d/go-git.v4 v4.13.1
5954
gopkg.in/yaml.v2 v2.3.0

Diff for: go.sum

+22-9
Large diffs are not rendered by default.

Diff for: test/test_compile.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,13 @@ def test_compile_with_invalid_url(run_command, data_dir):
557557
# Create settings with custom invalid URL
558558
assert run_command("config init --dest-dir . --additional-urls https://example.com/package_example_index.json")
559559

560-
# Verifies compilation fails cause of invalid URL
560+
# Verifies compilation fails cause of missing local index file
561561
res = run_command(f'compile -b {fqbn} "{sketch_path}"')
562562
assert res.failed
563-
assert "There were errors loading platform indexes" in res.stderr
563+
lines = [l.strip() for l in res.stderr.splitlines()]
564+
assert "Error creating instance: error loading platform index:" in lines
565+
assert (
566+
f"loading json index file {data_dir}/package_example_index.json: "
567+
+ f"open {data_dir}/package_example_index.json: no such file or directory"
568+
in lines
569+
)

Diff for: test/test_core.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,30 @@ def test_core_search_no_args(run_command, httpserver):
174174
assert len(platforms) == num_platforms
175175

176176

177-
def test_core_updateindex_invalid_url(run_command):
178-
url = "http://www.invalid-domain-asjkdakdhadjkh.com/package_example_index.json"
179-
result = run_command("core update-index --additional-urls={}".format(url))
177+
def test_core_updateindex_url_not_found(run_command, httpserver):
178+
assert run_command("core update-index")
179+
180+
# Brings up a local server to fake a failure
181+
httpserver.expect_request("/test_index.json").respond_with_data(status=404)
182+
url = httpserver.url_for("/test_index.json")
183+
184+
result = run_command(f"core update-index --additional-urls={url}")
185+
assert result.failed
186+
lines = [l.strip() for l in result.stderr.splitlines()]
187+
assert f"Error updating index: downloading index {url}: 404 NOT FOUND" in lines
188+
189+
190+
def test_core_updateindex_internal_server_error(run_command, httpserver):
191+
assert run_command("core update-index")
192+
193+
# Brings up a local server to fake a failure
194+
httpserver.expect_request("/test_index.json").respond_with_data(status=500)
195+
url = httpserver.url_for("/test_index.json")
196+
197+
result = run_command(f"core update-index --additional-urls={url}")
180198
assert result.failed
199+
lines = [l.strip() for l in result.stderr.splitlines()]
200+
assert f"Error updating index: downloading index {url}: 500 INTERNAL SERVER ERROR" in lines
181201

182202

183203
def test_core_install_without_updateindex(run_command):

Diff for: test/test_update.py

+26
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,29 @@ def test_update_showing_outdated(run_command):
4747
assert "Updating index: library_index.json downloaded" in lines
4848
assert lines[-5].startswith("Arduino AVR Boards")
4949
assert lines[-2].startswith("USBHost")
50+
51+
52+
def test_update_with_url_not_found(run_command, httpserver):
53+
assert run_command("update")
54+
55+
# Brings up a local server to fake a failure
56+
httpserver.expect_request("/test_index.json").respond_with_data(status=404)
57+
url = httpserver.url_for("/test_index.json")
58+
59+
res = run_command(f"update --additional-urls={url}")
60+
assert res.failed
61+
lines = [l.strip() for l in res.stderr.splitlines()]
62+
assert f"Error updating core and libraries index: downloading index {url}: 404 NOT FOUND" in lines
63+
64+
65+
def test_update_with_url_internal_server_error(run_command, httpserver):
66+
assert run_command("update")
67+
68+
# Brings up a local server to fake a failure
69+
httpserver.expect_request("/test_index.json").respond_with_data(status=500)
70+
url = httpserver.url_for("/test_index.json")
71+
72+
res = run_command(f"update --additional-urls={url}")
73+
assert res.failed
74+
lines = [l.strip() for l in res.stderr.splitlines()]
75+
assert f"Error updating core and libraries index: downloading index {url}: 500 INTERNAL SERVER ERROR" in lines

0 commit comments

Comments
 (0)