Skip to content

Commit 7a007be

Browse files
per1234cmaglie
authored andcommitted
Update gRPC API of board command to return status.Status instead of error
1 parent 9546264 commit 7a007be

File tree

6 files changed

+58
-37
lines changed

6 files changed

+58
-37
lines changed

commands/board/attach.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package board
1717

1818
import (
1919
"context"
20-
"errors"
2120
"fmt"
2221
"net/url"
2322
"strings"
@@ -31,23 +30,26 @@ import (
3130
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3231
discovery "github.com/arduino/board-discovery"
3332
"github.com/arduino/go-paths-helper"
33+
"google.golang.org/grpc/codes"
34+
"google.golang.org/grpc/status"
3435
)
3536

3637
var tr = i18n.Tr
3738

3839
// Attach FIXMEDOC
39-
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResponse, error) {
40+
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResponse, *status.Status) {
4041
pm := commands.GetPackageManager(req.GetInstance().GetId())
4142
if pm == nil {
42-
return nil, errors.New(tr("invalid instance"))
43+
44+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
4345
}
4446
var sketchPath *paths.Path
4547
if req.GetSketchPath() != "" {
4648
sketchPath = paths.New(req.GetSketchPath())
4749
}
4850
sk, err := sketch.New(sketchPath)
4951
if err != nil {
50-
return nil, fmt.Errorf(tr("opening sketch: %s"), err)
52+
return nil, status.Newf(codes.FailedPrecondition, tr("Error opening sketch: %s"), err)
5153
}
5254

5355
boardURI := req.GetBoardUri()
@@ -63,7 +65,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
6365
} else {
6466
deviceURI, err := url.Parse(boardURI)
6567
if err != nil {
66-
return nil, fmt.Errorf(tr("invalid Device URL format: %s"), err)
68+
return nil, status.Newf(codes.InvalidArgument, tr("Invalid Device URL format: %s"), err)
6769
}
6870

6971
var findBoardFunc func(*packagemanager.PackageManager, *discovery.Monitor, *url.URL) *cores.Board
@@ -73,7 +75,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
7375
case "http", "https", "tcp", "udp":
7476
findBoardFunc = findNetworkConnectedBoard
7577
default:
76-
return nil, fmt.Errorf(tr("invalid device port type provided"))
78+
return nil, status.New(codes.InvalidArgument, tr("Invalid device port type provided"))
7779
}
7880

7981
duration, err := time.ParseDuration(req.GetSearchTimeout())
@@ -89,7 +91,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
8991
// TODO: Handle the case when no board is found.
9092
board := findBoardFunc(pm, monitor, deviceURI)
9193
if board == nil {
92-
return nil, fmt.Errorf(tr("no supported board found at %s"), deviceURI.String())
94+
return nil, status.Newf(codes.NotFound, tr("No supported board found at %s"), deviceURI.String())
9395
}
9496
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Board found: %s"), board.Name())})
9597

@@ -104,7 +106,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
104106

105107
err = sk.ExportMetadata()
106108
if err != nil {
107-
return nil, fmt.Errorf(tr("cannot export sketch metadata: %s"), err)
109+
return nil, status.Newf(codes.PermissionDenied, tr("Cannot export sketch metadata: %s"), err)
108110
}
109111
taskCB(&rpc.TaskProgress{Name: fmt.Sprintf(tr("Selected fqbn: %s"), sk.Metadata.CPU.Fqbn), Completed: true})
110112
return &rpc.BoardAttachResponse{}, nil

commands/board/details.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@ package board
1717

1818
import (
1919
"context"
20-
"errors"
21-
"fmt"
2220

2321
"github.com/arduino/arduino-cli/arduino/cores"
2422
"github.com/arduino/arduino-cli/commands"
2523
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24+
"google.golang.org/grpc/codes"
25+
"google.golang.org/grpc/status"
2626
)
2727

2828
// Details returns all details for a board including tools and HW identifiers.
2929
// This command basically gather al the information and translates it into the required grpc struct properties
30-
func Details(ctx context.Context, req *rpc.BoardDetailsRequest) (*rpc.BoardDetailsResponse, error) {
30+
func Details(ctx context.Context, req *rpc.BoardDetailsRequest) (*rpc.BoardDetailsResponse, *status.Status) {
3131
pm := commands.GetPackageManager(req.GetInstance().GetId())
3232
if pm == nil {
33-
return nil, errors.New(tr("invalid instance"))
33+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
3434
}
3535

3636
fqbn, err := cores.ParseFQBN(req.GetFqbn())
3737
if err != nil {
38-
return nil, fmt.Errorf(tr("parsing fqbn: %s"), err)
38+
return nil, status.Newf(codes.InvalidArgument, tr("Error parsing fqbn: %s"), err)
3939
}
4040

4141
boardPackage, boardPlatform, board, boardProperties, boardRefPlatform, err := pm.ResolveFQBN(fqbn)
4242

4343
if err != nil {
44-
return nil, fmt.Errorf(tr("loading board data: %s"), err)
44+
return nil, status.Newf(codes.FailedPrecondition, tr("Error loading board data: %s"), err)
4545
}
4646

4747
details := &rpc.BoardDetailsResponse{}

commands/board/list.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/pkg/errors"
3434
"github.com/segmentio/stats/v4"
3535
"github.com/sirupsen/logrus"
36+
"google.golang.org/grpc/codes"
37+
"google.golang.org/grpc/status"
3638
)
3739

3840
var (
@@ -173,7 +175,7 @@ func identify(pm *packagemanager.PackageManager, port *discovery.Port) ([]*rpc.B
173175
}
174176

175177
// List FIXMEDOC
176-
func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
178+
func List(instanceID int32) (r []*rpc.DetectedPort, e *status.Status) {
177179
m.Lock()
178180
defer m.Unlock()
179181

@@ -190,19 +192,19 @@ func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
190192

191193
pm := commands.GetPackageManager(instanceID)
192194
if pm == nil {
193-
return nil, errors.New(tr("invalid instance"))
195+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
194196
}
195197

196198
ports, err := commands.ListBoards(pm)
197199
if err != nil {
198-
return nil, errors.Wrap(err, fmt.Sprintf(tr("error getting port list from %s"), "serial-discovery"))
200+
return nil, status.New(codes.FailedPrecondition, errors.Wrap(err, tr("Error getting port list from serial-discovery")).Error())
199201
}
200202

201203
retVal := []*rpc.DetectedPort{}
202204
for _, port := range ports {
203205
boards, err := identify(pm, port)
204206
if err != nil {
205-
return nil, err
207+
return nil, status.New(codes.Internal, err.Error())
206208
}
207209

208210
// boards slice can be empty at this point if neither the cores nor the
@@ -222,11 +224,11 @@ func List(instanceID int32) (r []*rpc.DetectedPort, e error) {
222224

223225
// Watch returns a channel that receives boards connection and disconnection events.
224226
// The discovery process can be interrupted by sending a message to the interrupt channel.
225-
func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchResponse, error) {
227+
func Watch(instanceID int32, interrupt <-chan bool) (<-chan *rpc.BoardListWatchResponse, *status.Status) {
226228
pm := commands.GetPackageManager(instanceID)
227229
eventsChan, err := commands.WatchListBoards(pm)
228230
if err != nil {
229-
return nil, err
231+
return nil, status.New(codes.FailedPrecondition, err.Error())
230232
}
231233

232234
outChan := make(chan *rpc.BoardListWatchResponse)

commands/board/listall.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@ package board
1717

1818
import (
1919
"context"
20-
"errors"
2120
"strings"
2221

2322
"github.com/arduino/arduino-cli/arduino/utils"
2423
"github.com/arduino/arduino-cli/commands"
2524
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
25+
"google.golang.org/grpc/codes"
26+
"google.golang.org/grpc/status"
2627
)
2728

2829
// maximumSearchDistance is the maximum Levenshtein distance accepted when using fuzzy search.
2930
// This value is completely arbitrary and picked randomly.
3031
const maximumSearchDistance = 20
3132

3233
// ListAll FIXMEDOC
33-
func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListAllResponse, error) {
34+
func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListAllResponse, *status.Status) {
3435
pm := commands.GetPackageManager(req.GetInstance().GetId())
3536
if pm == nil {
36-
return nil, errors.New(tr("invalid instance"))
37+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
3738
}
3839

3940
searchArgs := []string{}
@@ -101,7 +102,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
101102
toTest := append(toTest, board.Name())
102103
toTest = append(toTest, board.FQBN())
103104
if ok, err := match(toTest); err != nil {
104-
return nil, err
105+
return nil, status.New(codes.InvalidArgument, err.Error())
105106
} else if !ok {
106107
continue
107108
}

commands/board/search.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@ package board
1717

1818
import (
1919
"context"
20-
"errors"
2120
"sort"
2221
"strings"
2322

2423
"github.com/arduino/arduino-cli/arduino/utils"
2524
"github.com/arduino/arduino-cli/commands"
2625
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26+
"google.golang.org/grpc/codes"
27+
"google.golang.org/grpc/status"
2728
)
2829

2930
// Search returns all boards that match the search arg.
3031
// Boards are searched in all platforms, including those in the index that are not yet
3132
// installed. Note that platforms that are not installed don't include boards' FQBNs.
3233
// If no search argument is used all boards are returned.
33-
func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchResponse, error) {
34+
func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchResponse, *status.Status) {
3435
pm := commands.GetPackageManager(req.GetInstance().GetId())
3536
if pm == nil {
36-
return nil, errors.New(tr("invalid instance"))
37+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
3738
}
3839

3940
searchArgs := strings.Split(strings.Trim(req.SearchArgs, " "), " ")
@@ -91,7 +92,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
9192

9293
toTest := append(strings.Split(board.Name(), " "), board.Name(), board.FQBN())
9394
if ok, err := match(toTest); err != nil {
94-
return nil, err
95+
return nil, status.New(codes.InvalidArgument, err.Error())
9596
} else if !ok {
9697
continue
9798
}
@@ -107,7 +108,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
107108
for _, board := range latestPlatformRelease.BoardsManifest {
108109
toTest := append(strings.Split(board.Name, " "), board.Name)
109110
if ok, err := match(toTest); err != nil {
110-
return nil, err
111+
return nil, status.New(codes.InvalidArgument, err.Error())
111112
} else if !ok {
112113
continue
113114
}

commands/daemon/daemon.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ var tr = i18n.Tr
4343

4444
// BoardDetails FIXMEDOC
4545
func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.BoardDetailsRequest) (*rpc.BoardDetailsResponse, error) {
46-
return board.Details(ctx, req)
46+
resp, err := board.Details(ctx, req)
47+
if err != nil {
48+
return nil, err.Err()
49+
}
50+
51+
return resp, nil
4752
}
4853

4954
// BoardList FIXMEDOC
5055
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
5156
ports, err := board.List(req.GetInstance().GetId())
5257
if err != nil {
53-
return nil, err
58+
return nil, err.Err()
5459
}
5560

5661
return &rpc.BoardListResponse{
@@ -60,12 +65,22 @@ func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardLis
6065

6166
// BoardListAll FIXMEDOC
6267
func (s *ArduinoCoreServerImpl) BoardListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListAllResponse, error) {
63-
return board.ListAll(ctx, req)
68+
resp, err := board.ListAll(ctx, req)
69+
if err != nil {
70+
return nil, err.Err()
71+
}
72+
73+
return resp, nil
6474
}
6575

6676
// BoardSearch exposes to the gRPC interface the board search command
6777
func (s *ArduinoCoreServerImpl) BoardSearch(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchResponse, error) {
68-
return board.Search(ctx, req)
78+
resp, err := board.Search(ctx, req)
79+
if err != nil {
80+
return nil, err.Err()
81+
}
82+
83+
return resp, nil
6984
}
7085

7186
// BoardListWatch FIXMEDOC
@@ -112,9 +127,9 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
112127
}
113128
}()
114129

115-
eventsChan, err := board.Watch(msg.Instance.Id, interrupt)
116-
if err != nil {
117-
return err
130+
eventsChan, stat := board.Watch(msg.Instance.Id, interrupt)
131+
if stat != nil {
132+
return stat.Err()
118133
}
119134

120135
for event := range eventsChan {
@@ -134,7 +149,7 @@ func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachRequest, stream
134149
func(p *rpc.TaskProgress) { stream.Send(&rpc.BoardAttachResponse{TaskProgress: p}) },
135150
)
136151
if err != nil {
137-
return err
152+
return err.Err()
138153
}
139154
return stream.Send(resp)
140155
}

0 commit comments

Comments
 (0)