Skip to content

Commit 9c0c122

Browse files
committed
Removed useless ErrNotFound from apiByVidPid
The `apiByVidPid` function now masks the odd behavior of the builder-api returning an HTTP 404 if the request succeed but the result is empty.
1 parent 0b372c8 commit 9c0c122

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

Diff for: commands/board/list.go

+7-28
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ import (
3838
"github.com/sirupsen/logrus"
3939
)
4040

41-
type boardNotFoundError struct{}
42-
43-
func (e *boardNotFoundError) Error() string {
44-
return tr("board not found")
45-
}
46-
4741
var (
48-
// ErrNotFound is returned when the API returns 404
49-
ErrNotFound = &boardNotFoundError{}
5042
vidPidURL = "https://builder.arduino.cc/v3/boards/byVidPid"
5143
validVidPid = regexp.MustCompile(`0[xX][a-fA-F\d]{4}`)
5244
)
@@ -59,9 +51,6 @@ func cachedAPIByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
5951
ts := inventory.Store.GetTime(cacheKey + ".ts")
6052
if time.Since(ts) < time.Hour*24 {
6153
// Use cached response
62-
if cachedResp == "ErrNotFound" {
63-
return nil, ErrNotFound
64-
}
6554
if err := json.Unmarshal([]byte(cachedResp), &resp); err == nil {
6655
return resp, nil
6756
}
@@ -70,11 +59,7 @@ func cachedAPIByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
7059

7160
resp, err := apiByVidPid(vid, pid) // Perform API requrest
7261

73-
if err == ErrNotFound {
74-
inventory.Store.Set(cacheKey+".data", "ErrNotFound")
75-
inventory.Store.Set(cacheKey+".ts", time.Now())
76-
inventory.WriteStore()
77-
} else if err == nil {
62+
if err == nil {
7863
if cachedResp, err := json.Marshal(resp); err == nil {
7964
inventory.Store.Set(cacheKey+".data", string(cachedResp))
8065
inventory.Store.Set(cacheKey+".ts", time.Now())
@@ -109,10 +94,11 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
10994
if err != nil {
11095
return nil, errors.Wrap(err, tr("error querying Arduino Cloud Api"))
11196
}
97+
if res.StatusCode == 404 {
98+
// This is not an error, it just means that the board is not recognized
99+
return nil, nil
100+
}
112101
if res.StatusCode >= 400 {
113-
if res.StatusCode == 404 {
114-
return nil, ErrNotFound
115-
}
116102
return nil, errors.Errorf(tr("the server responded with status %s"), res.Status)
117103
}
118104

@@ -146,7 +132,7 @@ func identifyViaCloudAPI(port *discovery.Port) ([]*rpc.BoardListItem, error) {
146132
// If the port is not USB do not try identification via cloud
147133
id := port.Properties
148134
if !id.ContainsKey("vid") || !id.ContainsKey("pid") {
149-
return nil, ErrNotFound
135+
return nil, nil
150136
}
151137

152138
logrus.Debug("Querying builder API for board identification...")
@@ -181,17 +167,10 @@ func identify(pme *packagemanager.Explorer, port *discovery.Port) ([]*rpc.BoardL
181167
// the builder API if the board is a USB device port
182168
if len(boards) == 0 {
183169
items, err := identifyViaCloudAPI(port)
184-
if errors.Is(err, ErrNotFound) {
185-
// the board couldn't be detected, print a warning
186-
logrus.Debug("Board not recognized")
187-
} else if err != nil {
170+
if err != nil {
188171
// this is bad, but keep going
189172
logrus.WithError(err).Debug("Error querying builder API")
190173
}
191-
192-
// add a DetectedPort entry in any case: the `Boards` field will
193-
// be empty but the port will be shown anyways (useful for 3rd party
194-
// boards)
195174
boards = items
196175
}
197176

Diff for: commands/board/list_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ func TestGetByVidPidNotFound(t *testing.T) {
7171

7272
vidPidURL = ts.URL
7373
res, err := apiByVidPid("0x0420", "0x0069")
74-
require.NotNil(t, err)
75-
require.Equal(t, "board not found", err.Error())
76-
require.Len(t, res, 0)
74+
require.NoError(t, err)
75+
require.Empty(t, res)
7776
}
7877

7978
func TestGetByVidPid5xx(t *testing.T) {
@@ -108,7 +107,7 @@ func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
108107
Properties: properties.NewMap(),
109108
}
110109
items, err := identifyViaCloudAPI(port)
111-
require.ErrorIs(t, err, ErrNotFound)
110+
require.NoError(t, err)
112111
require.Empty(t, items)
113112
}
114113

0 commit comments

Comments
 (0)