Skip to content

Commit 73232e7

Browse files
committed
send headers, betteer error handling
1 parent fa4d9d4 commit 73232e7

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

cli/output/table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func (t *Table) makeTableRow(columns ...interface{}) *TableRow {
7171
case TextBox:
7272
cells[i] = text
7373
case string:
74-
cells[i] = Sprintf("%s", text)
74+
cells[i] = sprintf("%s", text)
7575
case fmt.Stringer:
76-
cells[i] = Sprintf("%s", text.String())
76+
cells[i] = sprintf("%s", text.String())
7777
default:
7878
panic(fmt.Sprintf("invalid column argument type: %t", col))
7979
}

cli/output/text.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ func spaces(n int) string {
121121
return res
122122
}
123123

124-
// Sprintf FIXMEDOC
125-
func Sprintf(format string, args ...interface{}) TextBox {
124+
func sprintf(format string, args ...interface{}) TextBox {
126125
cleanArgs := make([]interface{}, len(args))
127126
for i, arg := range args {
128127
if text, ok := arg.(*Text); ok {

commands/board/list.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,31 @@ import (
2323
"io/ioutil"
2424
"net/http"
2525

26+
"github.com/arduino/arduino-cli/cli/globals"
2627
"github.com/arduino/arduino-cli/commands"
2728
rpc "github.com/arduino/arduino-cli/rpc/commands"
2829
"github.com/pkg/errors"
2930
)
3031

32+
var (
33+
// ErrNotFound is returned when the API returns 404
34+
ErrNotFound = errors.New("board not found")
35+
)
36+
3137
func apiByVidPid(url string) ([]*rpc.BoardListItem, error) {
3238
retVal := []*rpc.BoardListItem{}
3339
req, _ := http.NewRequest("GET", url, nil)
40+
req.Header = globals.HTTPClientHeader
41+
req.Header.Set("Content-Type", "application/json")
42+
3443
if res, err := http.DefaultClient.Do(req); err == nil {
44+
if res.StatusCode >= 400 {
45+
if res.StatusCode == 404 {
46+
return nil, ErrNotFound
47+
}
48+
return nil, errors.Errorf("the server responded with status %s", res.Status)
49+
}
50+
3551
body, _ := ioutil.ReadAll(res.Body)
3652
res.Body.Close()
3753

@@ -100,7 +116,11 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
100116
port.IdentificationPrefs.Get("vid"),
101117
port.IdentificationPrefs.Get("pid"))
102118
items, err := apiByVidPid(url)
103-
if err != nil {
119+
if err == ErrNotFound {
120+
// the board couldn't be detected, keep going with the next port
121+
continue
122+
} else if err != nil {
123+
// this is bad, bail out
104124
return nil, errors.Wrap(err, "error getting bard info from Arduino Cloud")
105125
}
106126

commands/board/list_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ func TestGetByVidPid(t *testing.T) {
5151
require.NotNil(t, err)
5252
}
5353

54+
func TestGetByVidPidNotFound(t *testing.T) {
55+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
56+
w.WriteHeader(http.StatusNotFound)
57+
}))
58+
defer ts.Close()
59+
60+
res, err := apiByVidPid(ts.URL)
61+
require.NotNil(t, err)
62+
require.Equal(t, "board not found", err.Error())
63+
require.Len(t, res, 0)
64+
}
65+
66+
func TestGetByVidPid5xx(t *testing.T) {
67+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68+
w.WriteHeader(http.StatusInternalServerError)
69+
w.Write([]byte("500 - Ooooops!"))
70+
}))
71+
defer ts.Close()
72+
73+
res, err := apiByVidPid(ts.URL)
74+
require.NotNil(t, err)
75+
require.Equal(t, "the server responded with status 500 Internal Server Error", err.Error())
76+
require.Len(t, res, 0)
77+
}
78+
5479
func TestGetByVidPidMalformedResponse(t *testing.T) {
5580
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5681
fmt.Fprintln(w, "{}")

0 commit comments

Comments
 (0)