Skip to content

Commit 75a60c4

Browse files
per1234cmaglie
authored andcommitted
Update gRPC API of board command to return status.Status instead of error
1 parent 3532efa commit 75a60c4

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"
@@ -30,21 +29,24 @@ import (
3029
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3130
discovery "github.com/arduino/board-discovery"
3231
"github.com/arduino/go-paths-helper"
32+
"google.golang.org/grpc/codes"
33+
"google.golang.org/grpc/status"
3334
)
3435

3536
// Attach FIXMEDOC
36-
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResponse, error) {
37+
func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.TaskProgressCB) (*rpc.BoardAttachResponse, *status.Status) {
3738
pm := commands.GetPackageManager(req.GetInstance().GetId())
3839
if pm == nil {
39-
return nil, errors.New("invalid instance")
40+
41+
return nil, status.New(codes.InvalidArgument, "invalid instance")
4042
}
4143
var sketchPath *paths.Path
4244
if req.GetSketchPath() != "" {
4345
sketchPath = paths.New(req.GetSketchPath())
4446
}
4547
sketch, err := sketches.NewSketchFromPath(sketchPath)
4648
if err != nil {
47-
return nil, fmt.Errorf("opening sketch: %s", err)
49+
return nil, status.Newf(codes.FailedPrecondition, "opening sketch: %s", err)
4850
}
4951

5052
boardURI := req.GetBoardUri()
@@ -60,7 +62,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
6062
} else {
6163
deviceURI, err := url.Parse(boardURI)
6264
if err != nil {
63-
return nil, fmt.Errorf("invalid Device URL format: %s", err)
65+
return nil, status.Newf(codes.InvalidArgument, "invalid Device URL format: %s", err)
6466
}
6567

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

7678
duration, err := time.ParseDuration(req.GetSearchTimeout())
@@ -86,7 +88,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
8688
// TODO: Handle the case when no board is found.
8789
board := findBoardFunc(pm, monitor, deviceURI)
8890
if board == nil {
89-
return nil, fmt.Errorf("no supported board found at %s", deviceURI.String())
91+
return nil, status.Newf(codes.NotFound, "no supported board found at %s", deviceURI.String())
9092
}
9193
taskCB(&rpc.TaskProgress{Name: "Board found: " + board.Name()})
9294

@@ -101,7 +103,7 @@ func Attach(ctx context.Context, req *rpc.BoardAttachRequest, taskCB commands.Ta
101103

102104
err = sketch.ExportMetadata()
103105
if err != nil {
104-
return nil, fmt.Errorf("cannot export sketch metadata: %s", err)
106+
return nil, status.Newf(codes.PermissionDenied, "cannot export sketch metadata: %s", err)
105107
}
106108
taskCB(&rpc.TaskProgress{Name: "Selected fqbn: " + sketch.Metadata.CPU.Fqbn, Completed: true})
107109
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("invalid instance")
33+
return nil, status.New(codes.InvalidArgument, "invalid instance")
3434
}
3535

3636
fqbn, err := cores.ParseFQBN(req.GetFqbn())
3737
if err != nil {
38-
return nil, fmt.Errorf("parsing fqbn: %s", err)
38+
return nil, status.Newf(codes.InvalidArgument, "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("loading board data: %s", err)
44+
return nil, status.Newf(codes.FailedPrecondition, "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("invalid instance")
195+
return nil, status.New(codes.InvalidArgument, "invalid instance")
194196
}
195197

196198
ports, err := commands.ListBoards(pm)
197199
if err != nil {
198-
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
200+
return nil, status.New(codes.FailedPrecondition, errors.Wrap(err, "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("invalid instance")
37+
return nil, status.New(codes.InvalidArgument, "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("invalid instance")
37+
return nil, status.New(codes.InvalidArgument, "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
@@ -40,14 +40,19 @@ type ArduinoCoreServerImpl struct {
4040

4141
// BoardDetails FIXMEDOC
4242
func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.BoardDetailsRequest) (*rpc.BoardDetailsResponse, error) {
43-
return board.Details(ctx, req)
43+
resp, err := board.Details(ctx, req)
44+
if err != nil {
45+
return nil, err.Err()
46+
}
47+
48+
return resp, nil
4449
}
4550

4651
// BoardList FIXMEDOC
4752
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListRequest) (*rpc.BoardListResponse, error) {
4853
ports, err := board.List(req.GetInstance().GetId())
4954
if err != nil {
50-
return nil, err
55+
return nil, err.Err()
5156
}
5257

5358
return &rpc.BoardListResponse{
@@ -57,12 +62,22 @@ func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardLis
5762

5863
// BoardListAll FIXMEDOC
5964
func (s *ArduinoCoreServerImpl) BoardListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListAllResponse, error) {
60-
return board.ListAll(ctx, req)
65+
resp, err := board.ListAll(ctx, req)
66+
if err != nil {
67+
return nil, err.Err()
68+
}
69+
70+
return resp, nil
6171
}
6272

6373
// BoardSearch exposes to the gRPC interface the board search command
6474
func (s *ArduinoCoreServerImpl) BoardSearch(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchResponse, error) {
65-
return board.Search(ctx, req)
75+
resp, err := board.Search(ctx, req)
76+
if err != nil {
77+
return nil, err.Err()
78+
}
79+
80+
return resp, nil
6681
}
6782

6883
// BoardListWatch FIXMEDOC
@@ -109,9 +124,9 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
109124
}
110125
}()
111126

112-
eventsChan, err := board.Watch(msg.Instance.Id, interrupt)
113-
if err != nil {
114-
return err
127+
eventsChan, stat := board.Watch(msg.Instance.Id, interrupt)
128+
if stat != nil {
129+
return stat.Err()
115130
}
116131

117132
for event := range eventsChan {
@@ -131,7 +146,7 @@ func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachRequest, stream
131146
func(p *rpc.TaskProgress) { stream.Send(&rpc.BoardAttachResponse{TaskProgress: p}) },
132147
)
133148
if err != nil {
134-
return err
149+
return err.Err()
135150
}
136151
return stream.Send(resp)
137152
}

0 commit comments

Comments
 (0)