Skip to content

Commit 8ab6c9b

Browse files
committed
Refactoring 'commands' commands
1 parent 0bc3bb3 commit 8ab6c9b

File tree

6 files changed

+172
-116
lines changed

6 files changed

+172
-116
lines changed

Diff for: cli/instance/instance.go

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

1818
import (
1919
"context"
20+
"errors"
2021
"os"
2122

2223
"github.com/arduino/arduino-cli/cli/errorcodes"
@@ -27,7 +28,6 @@ import (
2728
"github.com/arduino/arduino-cli/i18n"
2829
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2930
"github.com/arduino/go-paths-helper"
30-
"google.golang.org/grpc/status"
3131
)
3232

3333
var tr = i18n.Tr
@@ -49,7 +49,7 @@ func CreateAndInit() *rpc.Instance {
4949
}
5050

5151
// Create and return a new Instance.
52-
func Create() (*rpc.Instance, *status.Status) {
52+
func Create() (*rpc.Instance, error) {
5353
res, err := commands.Create(&rpc.CreateRequest{})
5454
if err != nil {
5555
return nil, err
@@ -58,12 +58,12 @@ func Create() (*rpc.Instance, *status.Status) {
5858
}
5959

6060
// Init initializes instance by loading installed libraries and platforms.
61-
// In case of loading failures return a list gRPC Status errors for each
61+
// In case of loading failures return a list of errors for each
6262
// platform or library that we failed to load.
6363
// Package and library indexes files are automatically updated if the
6464
// CLI is run for the first time.
65-
func Init(instance *rpc.Instance) []*status.Status {
66-
errs := []*status.Status{}
65+
func Init(instance *rpc.Instance) []error {
66+
errs := []error{}
6767

6868
// In case the CLI is executed for the first time
6969
if err := FirstUpdate(instance); err != nil {
@@ -76,8 +76,8 @@ func Init(instance *rpc.Instance) []*status.Status {
7676
err := commands.Init(&rpc.InitRequest{
7777
Instance: instance,
7878
}, func(res *rpc.InitResponse) {
79-
if err := res.GetError(); err != nil {
80-
errs = append(errs, status.FromProto(err))
79+
if st := res.GetError(); st != nil {
80+
errs = append(errs, errors.New(st.Message))
8181
}
8282

8383
if progress := res.GetInitProgress(); progress != nil {
@@ -98,7 +98,7 @@ func Init(instance *rpc.Instance) []*status.Status {
9898

9999
// FirstUpdate downloads libraries and packages indexes if they don't exist.
100100
// This ideally is only executed the first time the CLI is run.
101-
func FirstUpdate(instance *rpc.Instance) *status.Status {
101+
func FirstUpdate(instance *rpc.Instance) error {
102102
// Gets the data directory to verify if library_index.json and package_index.json exist
103103
dataDir := paths.New(configuration.Settings.GetString("directories.data"))
104104

Diff for: commands/core/install.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
5454
return nil, err
5555
}
5656

57-
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
58-
if status != nil {
59-
return nil, status.Err()
57+
if err := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil); err != nil {
58+
return nil, err
6059
}
6160

6261
return &rpc.PlatformInstallResponse{}, nil

Diff for: commands/daemon/daemon.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (s *ArduinoCoreServerImpl) BoardAttach(req *rpc.BoardAttachRequest, stream
153153
// Destroy FIXMEDOC
154154
func (s *ArduinoCoreServerImpl) Destroy(ctx context.Context, req *rpc.DestroyRequest) (*rpc.DestroyResponse, error) {
155155
resp, err := commands.Destroy(ctx, req)
156-
return resp, err.Err()
156+
return resp, convertErrorToRPCStatus(err)
157157
}
158158

159159
// UpdateIndex FIXMEDOC
@@ -162,7 +162,7 @@ func (s *ArduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
162162
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateIndexResponse{DownloadProgress: p}) },
163163
)
164164
if err != nil {
165-
return err.Err()
165+
return convertErrorToRPCStatus(err)
166166
}
167167
return stream.Send(resp)
168168
}
@@ -173,7 +173,7 @@ func (s *ArduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
173173
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateLibrariesIndexResponse{DownloadProgress: p}) },
174174
)
175175
if err != nil {
176-
return err.Err()
176+
return convertErrorToRPCStatus(err)
177177
}
178178
return stream.Send(&rpc.UpdateLibrariesIndexResponse{})
179179
}
@@ -184,15 +184,15 @@ func (s *ArduinoCoreServerImpl) UpdateCoreLibrariesIndex(req *rpc.UpdateCoreLibr
184184
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateCoreLibrariesIndexResponse{DownloadProgress: p}) },
185185
)
186186
if err != nil {
187-
return err.Err()
187+
return convertErrorToRPCStatus(err)
188188
}
189189
return stream.Send(&rpc.UpdateCoreLibrariesIndexResponse{})
190190
}
191191

192192
// Outdated FIXMEDOC
193193
func (s *ArduinoCoreServerImpl) Outdated(ctx context.Context, req *rpc.OutdatedRequest) (*rpc.OutdatedResponse, error) {
194194
resp, err := commands.Outdated(ctx, req)
195-
return resp, err.Err()
195+
return resp, convertErrorToRPCStatus(err)
196196
}
197197

198198
// Upgrade FIXMEDOC
@@ -210,29 +210,23 @@ func (s *ArduinoCoreServerImpl) Upgrade(req *rpc.UpgradeRequest, stream rpc.Ardu
210210
},
211211
)
212212
if err != nil {
213-
return err.Err()
213+
return convertErrorToRPCStatus(err)
214214
}
215215
return stream.Send(&rpc.UpgradeResponse{})
216216
}
217217

218218
// Create FIXMEDOC
219219
func (s *ArduinoCoreServerImpl) Create(_ context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
220-
res, status := commands.Create(req)
221-
if status != nil {
222-
return nil, status.Err()
223-
}
224-
return res, nil
220+
res, err := commands.Create(req)
221+
return res, convertErrorToRPCStatus(err)
225222
}
226223

227224
// Init FIXMEDOC
228225
func (s *ArduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCoreService_InitServer) error {
229226
err := commands.Init(req, func(message *rpc.InitResponse) {
230227
stream.Send(message)
231228
})
232-
if err != nil {
233-
return err.Err()
234-
}
235-
return nil
229+
return convertErrorToRPCStatus(err)
236230
}
237231

238232
// Version FIXMEDOC
@@ -243,7 +237,7 @@ func (s *ArduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq
243237
// LoadSketch FIXMEDOC
244238
func (s *ArduinoCoreServerImpl) LoadSketch(ctx context.Context, req *rpc.LoadSketchRequest) (*rpc.LoadSketchResponse, error) {
245239
resp, err := commands.LoadSketch(ctx, req)
246-
return resp, err.Err()
240+
return resp, convertErrorToRPCStatus(err)
247241
}
248242

249243
// Compile FIXMEDOC

Diff for: commands/download.go

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

1818
import (
19-
"errors"
2019
"time"
2120

2221
"github.com/arduino/arduino-cli/httpclient"
@@ -27,16 +26,13 @@ import (
2726
// GetDownloaderConfig returns the downloader configuration based on
2827
// current settings.
2928
func GetDownloaderConfig() (*downloader.Config, error) {
30-
3129
httpClient, err := httpclient.New()
3230
if err != nil {
33-
return nil, err
31+
return nil, &InvalidArgumentError{Message: tr("Could not connect via HTTP"), Cause: err}
3432
}
35-
36-
res := &downloader.Config{
33+
return &downloader.Config{
3734
HttpClient: *httpClient,
38-
}
39-
return res, nil
35+
}, nil
4036
}
4137

4238
// Download performs a download loop using the provided downloader.Downloader.
@@ -63,7 +59,7 @@ func Download(d *downloader.Downloader, label string, downloadCB DownloadProgres
6359
}
6460
// The URL is not reachable for some reason
6561
if d.Resp.StatusCode >= 400 && d.Resp.StatusCode <= 599 {
66-
return errors.New(d.Resp.Status)
62+
return &FailedDownloadError{Message: tr("Server responded with: %s", d.Resp.Status)}
6763
}
6864
downloadCB(&rpc.DownloadProgress{Completed: true})
6965
return nil

Diff for: commands/errors.go

+69
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ func (e *InvalidFQBNError) Unwrap() error {
6363
return e.Cause
6464
}
6565

66+
// InvalidURLError is returned when the URL has syntax errors
67+
type InvalidURLError struct {
68+
Cause error
69+
}
70+
71+
func (e *InvalidURLError) Error() string {
72+
return composeErrorMsg(tr("Invalid URL"), e.Cause)
73+
}
74+
75+
func (e *InvalidURLError) ToRPCStatus() *status.Status {
76+
return status.New(codes.InvalidArgument, e.Error())
77+
}
78+
79+
func (e *InvalidURLError) Unwrap() error {
80+
return e.Cause
81+
}
82+
6683
// InvalidLibraryError is returned when the library has syntax errors
6784
type InvalidLibraryError struct {
6885
Cause error
@@ -500,3 +517,55 @@ func (e *UnavailableError) Unwrap() error {
500517
func (e *UnavailableError) ToRPCStatus() *status.Status {
501518
return status.New(codes.Unavailable, e.Error())
502519
}
520+
521+
// TempDirCreationFailedError is returned if a temp dir could not be created
522+
type TempDirCreationFailedError struct {
523+
Cause error
524+
}
525+
526+
func (e *TempDirCreationFailedError) Error() string {
527+
return composeErrorMsg(tr("Cannot create temp dir"), e.Cause)
528+
}
529+
530+
func (e *TempDirCreationFailedError) Unwrap() error {
531+
return e.Cause
532+
}
533+
534+
func (e *TempDirCreationFailedError) ToRPCStatus() *status.Status {
535+
return status.New(codes.Unavailable, e.Error())
536+
}
537+
538+
// TempFileCreationFailedError is returned if a temp file could not be created
539+
type TempFileCreationFailedError struct {
540+
Cause error
541+
}
542+
543+
func (e *TempFileCreationFailedError) Error() string {
544+
return composeErrorMsg(tr("Cannot create temp file"), e.Cause)
545+
}
546+
547+
func (e *TempFileCreationFailedError) Unwrap() error {
548+
return e.Cause
549+
}
550+
551+
func (e *TempFileCreationFailedError) ToRPCStatus() *status.Status {
552+
return status.New(codes.Unavailable, e.Error())
553+
}
554+
555+
// SignatureVerificationFailedError is returned if a signature verification fails
556+
type SignatureVerificationFailedError struct {
557+
File string
558+
Cause error
559+
}
560+
561+
func (e *SignatureVerificationFailedError) Error() string {
562+
return composeErrorMsg(tr("'%s' has an invalid signature", e.File), e.Cause)
563+
}
564+
565+
func (e *SignatureVerificationFailedError) Unwrap() error {
566+
return e.Cause
567+
}
568+
569+
func (e *SignatureVerificationFailedError) ToRPCStatus() *status.Status {
570+
return status.New(codes.Unavailable, e.Error())
571+
}

0 commit comments

Comments
 (0)