Skip to content

Commit 906bd5d

Browse files
per1234cmaglie
authored andcommitted
Update gRPC API of remaining commands to return status.Status instead of error
1 parent cba602d commit 906bd5d

28 files changed

+282
-258
lines changed

Diff for: cli/compile/compile.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func run(cmd *cobra.Command, args []string) {
183183
compileErr := new(bytes.Buffer)
184184
verboseCompile := configuration.Settings.GetString("logging.level") == "debug"
185185
var compileRes *rpc.CompileResponse
186-
var err error
186+
var err *status.Status
187187
if output.OutputFormat == "json" {
188188
compileRes, err = compile.Compile(context.Background(), compileRequest, compileOut, compileErr, verboseCompile)
189189
} else {
@@ -201,7 +201,6 @@ func run(cmd *cobra.Command, args []string) {
201201
ImportDir: buildPath,
202202
Programmer: programmer,
203203
}
204-
var err *status.Status
205204
if output.OutputFormat == "json" {
206205
// TODO: do not print upload output in json mode
207206
uploadOut := new(bytes.Buffer)

Diff for: cli/core/upgrade.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func runUpgradeCommand(cmd *cobra.Command, args []string) {
9595
}
9696

9797
_, err := core.PlatformUpgrade(context.Background(), r, output.ProgressBar(), output.TaskProgress())
98-
if err == core.ErrAlreadyLatest {
98+
if err.Message() == core.ErrAlreadyLatest.Error() {
9999
feedback.Printf(tr("Platform %s is already at the latest version"), platformRef)
100100
} else if err != nil {
101101
feedback.Errorf(tr("Error during upgrade: %v"), err)

Diff for: cli/debug/debug.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/fatih/color"
3535
"github.com/sirupsen/logrus"
3636
"github.com/spf13/cobra"
37-
"google.golang.org/grpc/status"
3837
)
3938

4039
var (
@@ -92,12 +91,8 @@ func run(command *cobra.Command, args []string) {
9291
if printInfo {
9392

9493
if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
95-
if status, ok := status.FromError(err); ok {
96-
feedback.Errorf(tr("Error getting Debug info: %v"), status.Message())
97-
errorcodes.ExitWithGrpcStatus(status)
98-
}
99-
feedback.Errorf(tr("Error getting Debug info: %v"), err)
100-
os.Exit(errorcodes.ErrGeneric)
94+
feedback.Errorf(tr("Error getting Debug info: %v"), err.Message())
95+
errorcodes.ExitWithGrpcStatus(err)
10196
} else {
10297
feedback.PrintResult(&debugInfoResult{res})
10398
}

Diff for: cli/instance/instance.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/arduino/arduino-cli/i18n"
2828
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2929
"github.com/arduino/go-paths-helper"
30-
"google.golang.org/grpc/codes"
3130
"google.golang.org/grpc/status"
3231
)
3332

@@ -120,7 +119,7 @@ func FirstUpdate(instance *rpc.Instance) *status.Status {
120119
output.ProgressBar(),
121120
)
122121
if err != nil {
123-
return status.Newf(codes.FailedPrecondition, err.Error())
122+
return err
124123
}
125124
}
126125

@@ -135,7 +134,7 @@ func FirstUpdate(instance *rpc.Instance) *status.Status {
135134
output.ProgressBar(),
136135
)
137136
if err != nil {
138-
return status.Newf(codes.FailedPrecondition, err.Error())
137+
return err
139138
}
140139
}
141140

Diff for: cli/lib/args.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ func ParseLibraryReferenceArgs(args []string) ([]*LibraryReferenceArg, error) {
7575
// ParseLibraryReferenceArgAndAdjustCase parse a command line argument that reference a
7676
// library and possibly adjust the case of the name to match a library in the index
7777
func ParseLibraryReferenceArgAndAdjustCase(instance *rpc.Instance, arg string) (*LibraryReferenceArg, error) {
78-
libRef, err := ParseLibraryReferenceArg(arg)
78+
libRef, _ := ParseLibraryReferenceArg(arg)
7979
res, err := lib.LibrarySearch(context.Background(), &rpc.LibrarySearchRequest{
8080
Instance: instance,
8181
Query: libRef.Name,
8282
})
8383
if err != nil {
84-
return nil, err
84+
return nil, err.Err()
8585
}
8686

8787
candidates := []*rpc.SearchedLibrary{}

Diff for: cli/lib/check_deps.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ func runDepsCommand(cmd *cobra.Command, args []string) {
5151
os.Exit(errorcodes.ErrBadArgument)
5252
}
5353

54-
deps, err := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesRequest{
54+
deps, stat := lib.LibraryResolveDependencies(context.Background(), &rpc.LibraryResolveDependenciesRequest{
5555
Instance: instance,
5656
Name: libRef.Name,
5757
Version: libRef.Version,
5858
})
59-
if err != nil {
60-
feedback.Errorf(tr("Error resolving dependencies for %[1]s: %[2]s"), libRef, err)
59+
if stat != nil {
60+
feedback.Errorf(tr("Error resolving dependencies for %[1]s: %[2]s", libRef, err))
6161
}
6262

6363
feedback.PrintResult(&checkDepResult{deps: deps})

Diff for: commands/compile/compile.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package compile
1717

1818
import (
1919
"context"
20-
"fmt"
2120
"io"
2221
"path/filepath"
2322
"sort"
@@ -41,12 +40,14 @@ import (
4140
"github.com/pkg/errors"
4241
"github.com/segmentio/stats/v4"
4342
"github.com/sirupsen/logrus"
43+
"google.golang.org/grpc/codes"
44+
"google.golang.org/grpc/status"
4445
)
4546

4647
var tr = i18n.Tr
4748

4849
// Compile FIXMEDOC
49-
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResponse, e error) {
50+
func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResponse, e *status.Status) {
5051

5152
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
5253
// since we want this binding to work also for the gRPC interface we must read it here in this
@@ -90,29 +91,29 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
9091

9192
pm := commands.GetPackageManager(req.GetInstance().GetId())
9293
if pm == nil {
93-
return nil, errors.New(tr("invalid instance"))
94+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
9495
}
9596

9697
logrus.Tracef("Compile %s for %s started", req.GetSketchPath(), req.GetFqbn())
9798
if req.GetSketchPath() == "" {
98-
return nil, fmt.Errorf(tr("missing sketchPath"))
99+
return nil, status.New(codes.InvalidArgument, tr("Missing sketch path"))
99100
}
100101
sketchPath := paths.New(req.GetSketchPath())
101102
sk, err := sketch.New(sketchPath)
102103
if err != nil {
103-
return nil, fmt.Errorf(tr("opening sketch: %s"), err)
104+
return nil, status.Newf(codes.NotFound, tr("Error opening sketch: %s"), err)
104105
}
105106

106107
fqbnIn := req.GetFqbn()
107108
if fqbnIn == "" && sk != nil && sk.Metadata != nil {
108109
fqbnIn = sk.Metadata.CPU.Fqbn
109110
}
110111
if fqbnIn == "" {
111-
return nil, fmt.Errorf(tr("no FQBN provided"))
112+
return nil, status.New(codes.InvalidArgument, tr("No FQBN (Fully Qualified Board Name) provided"))
112113
}
113114
fqbn, err := cores.ParseFQBN(fqbnIn)
114115
if err != nil {
115-
return nil, fmt.Errorf(tr("incorrect FQBN: %s"), err)
116+
return nil, status.Newf(codes.InvalidArgument, tr("Invalid FQBN: %s"), err)
116117
}
117118

118119
targetPlatform := pm.FindPlatform(&packagemanager.PlatformReference{
@@ -125,7 +126,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
125126
// "\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
126127
// version.GetAppName()+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
127128
// feedback.Error(errorMessage)
128-
return nil, fmt.Errorf(tr("platform not installed"))
129+
return nil, status.New(codes.NotFound, tr("Platform not installed"))
129130
}
130131

131132
builderCtx := &types.Context{}
@@ -148,7 +149,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
148149
builderCtx.BuildPath = paths.New(req.GetBuildPath())
149150
}
150151
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
151-
return nil, fmt.Errorf(tr("cannot create build directory: %s"), err)
152+
return nil, status.Newf(codes.PermissionDenied, tr("Cannot create build directory: %s"), err)
152153
}
153154
builderCtx.CompilationDatabase = bldr.NewCompilationDatabase(
154155
builderCtx.BuildPath.Join("compile_commands.json"),
@@ -178,7 +179,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
178179
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
179180
err = builderCtx.BuildCachePath.MkdirAll()
180181
if err != nil {
181-
return nil, fmt.Errorf(tr("cannot create build cache directory: %s"), err)
182+
return nil, status.Newf(codes.PermissionDenied, tr("Cannot create build cache directory: %s"), err)
182183
}
183184
}
184185

@@ -221,14 +222,14 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
221222

222223
// if --preprocess or --show-properties were passed, we can stop here
223224
if req.GetShowProperties() {
224-
return r, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
225+
return r, status.Convert(builder.RunParseHardwareAndDumpBuildProperties(builderCtx))
225226
} else if req.GetPreprocess() {
226-
return r, builder.RunPreprocess(builderCtx)
227+
return r, status.Convert(builder.RunPreprocess(builderCtx))
227228
}
228229

229230
// if it's a regular build, go on...
230231
if err := builder.RunBuilder(builderCtx); err != nil {
231-
return r, err
232+
return r, status.Convert(err)
232233
}
233234

234235
// If the export directory is set we assume you want to export the binaries
@@ -250,17 +251,17 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
250251
}
251252
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
252253
if err := exportPath.MkdirAll(); err != nil {
253-
return r, errors.Wrap(err, tr("creating output dir"))
254+
return r, status.New(codes.PermissionDenied, errors.Wrap(err, tr("Error creating output dir")).Error())
254255
}
255256

256257
// Copy all "sketch.ino.*" artifacts to the export directory
257258
baseName, ok := builderCtx.BuildProperties.GetOk("build.project_name") // == "sketch.ino"
258259
if !ok {
259-
return r, errors.New(tr("missing 'build.project_name' build property"))
260+
return r, status.New(codes.Internal, tr("Missing 'build.project_name' build property"))
260261
}
261262
buildFiles, err := builderCtx.BuildPath.ReadDir()
262263
if err != nil {
263-
return r, errors.Errorf(tr("reading build directory: %s"), err)
264+
return r, status.Newf(codes.PermissionDenied, tr("Error reading build directory: %s"), err)
264265
}
265266
buildFiles.FilterPrefix(baseName)
266267
for _, buildFile := range buildFiles {
@@ -270,7 +271,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
270271
WithField("dest", exportedFile).
271272
Trace("Copying artifact.")
272273
if err = buildFile.CopyTo(exportedFile); err != nil {
273-
return r, errors.Wrapf(err, tr("copying output file %s"), buildFile)
274+
return r, status.New(codes.PermissionDenied, tr("Error copying output file %[1]s: %[2]s", buildFile, err))
274275
}
275276
}
276277
}
@@ -279,7 +280,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
279280
for _, lib := range builderCtx.ImportedLibraries {
280281
rpcLib, err := lib.ToRPCLibrary()
281282
if err != nil {
282-
return r, fmt.Errorf(tr("converting library %[1]s to rpc struct: %[2]w"), lib.Name, err)
283+
return r, status.Newf(codes.PermissionDenied, tr("Error converting library %[1]s to rpc struct: %[2]s", lib.Name, err))
283284
}
284285
importedLibs = append(importedLibs, rpcLib)
285286
}

Diff for: commands/core/download.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,29 @@ package core
1717

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

2322
"github.com/arduino/arduino-cli/arduino/cores"
2423
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2524
"github.com/arduino/arduino-cli/commands"
2625
"github.com/arduino/arduino-cli/i18n"
2726
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
27+
"google.golang.org/grpc/codes"
28+
"google.golang.org/grpc/status"
2829
)
2930

3031
var tr = i18n.Tr
3132

3233
// PlatformDownload FIXMEDOC
33-
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, error) {
34+
func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, downloadCB commands.DownloadProgressCB) (*rpc.PlatformDownloadResponse, *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
version, err := commands.ParseVersion(req)
4041
if err != nil {
41-
return nil, fmt.Errorf(tr("invalid version: %s"), err)
42+
return nil, status.Newf(codes.InvalidArgument, tr("Invalid version: %s"), err)
4243
}
4344

4445
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
@@ -47,18 +48,18 @@ func PlatformDownload(ctx context.Context, req *rpc.PlatformDownloadRequest, dow
4748
PlatformVersion: version,
4849
})
4950
if err != nil {
50-
return nil, fmt.Errorf(tr("find platform dependencies: %s"), err)
51+
return nil, status.Newf(codes.InvalidArgument, tr("Error finding platform dependencies: %s"), err)
5152
}
5253

5354
err = downloadPlatform(pm, platform, downloadCB)
5455
if err != nil {
55-
return nil, err
56+
return nil, status.New(codes.Unavailable, err.Error())
5657
}
5758

5859
for _, tool := range tools {
5960
err := downloadTool(pm, tool, downloadCB)
6061
if err != nil {
61-
return nil, fmt.Errorf(tr("downloading tool %[1]s: %[2]s"), tool, err)
62+
return nil, status.Newf(codes.Unavailable, tr("Error downloading tool %[1]s: %[2]s"), tool, err)
6263
}
6364
}
6465

Diff for: commands/core/install.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@ import (
2323
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2424
"github.com/arduino/arduino-cli/commands"
2525
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
26-
"github.com/pkg/errors"
26+
"google.golang.org/grpc/codes"
27+
"google.golang.org/grpc/status"
2728
)
2829

2930
// PlatformInstall FIXMEDOC
3031
func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
31-
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, error) {
32+
downloadCB commands.DownloadProgressCB, taskCB commands.TaskProgressCB) (*rpc.PlatformInstallResponse, *status.Status) {
3233

3334
pm := commands.GetPackageManager(req.GetInstance().GetId())
3435
if pm == nil {
35-
return nil, errors.New(tr("invalid instance"))
36+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
3637
}
3738

3839
version, err := commands.ParseVersion(req)
3940
if err != nil {
40-
return nil, fmt.Errorf(tr("invalid version: %s"), err)
41+
return nil, status.Newf(codes.InvalidArgument, tr("Invalid version: %s"), err)
4142
}
4243

4344
platform, tools, err := pm.FindPlatformReleaseDependencies(&packagemanager.PlatformReference{
@@ -46,17 +47,17 @@ func PlatformInstall(ctx context.Context, req *rpc.PlatformInstallRequest,
4647
PlatformVersion: version,
4748
})
4849
if err != nil {
49-
return nil, fmt.Errorf(tr("finding platform dependencies: %s"), err)
50+
return nil, status.Newf(codes.InvalidArgument, tr("Error finding platform dependencies: %s"), err)
5051
}
5152

5253
err = installPlatform(pm, platform, tools, downloadCB, taskCB, req.GetSkipPostInstall())
5354
if err != nil {
54-
return nil, err
55+
return nil, status.Convert(err)
5556
}
5657

5758
status := commands.Init(&rpc.InitRequest{Instance: req.Instance}, nil)
5859
if status != nil {
59-
return nil, status.Err()
60+
return nil, status
6061
}
6162

6263
return &rpc.PlatformInstallResponse{}, nil

Diff for: commands/core/list.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@ import (
2121

2222
"github.com/arduino/arduino-cli/commands"
2323
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
24-
"github.com/pkg/errors"
24+
"google.golang.org/grpc/codes"
25+
"google.golang.org/grpc/status"
2526
)
2627

2728
// GetPlatforms returns a list of installed platforms, optionally filtered by
2829
// those requiring an update.
29-
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, error) {
30+
func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, *status.Status) {
3031
instanceID := req.Instance.Id
3132
i := commands.GetInstance(instanceID)
3233
if i == nil {
33-
return nil, errors.Errorf(tr("unable to find an instance with ID: %d"), instanceID)
34+
return nil, status.Newf(codes.InvalidArgument, tr("Invalid instance"))
3435
}
3536

3637
packageManager := i.PackageManager
3738
if packageManager == nil {
38-
return nil, errors.New(tr("invalid instance"))
39+
return nil, status.New(codes.InvalidArgument, tr("Invalid instance"))
3940
}
4041

4142
res := []*rpc.Platform{}

0 commit comments

Comments
 (0)