Skip to content

Commit 034a193

Browse files
committed
Use normal errors from 'command'
Normal errors are automatically wrapped into GRPC errors with code equals to "Unknown".
1 parent c13d9fd commit 034a193

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

commands/board/details.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ package board
1919

2020
import (
2121
"context"
22-
23-
"google.golang.org/grpc/codes"
24-
"google.golang.org/grpc/status"
22+
"fmt"
2523

2624
"github.com/arduino/arduino-cli/arduino/cores"
2725
"github.com/arduino/arduino-cli/commands"
@@ -32,12 +30,12 @@ func BoardDetails(ctx context.Context, req *rpc.BoardDetailsReq) (*rpc.BoardDeta
3230
pm := commands.GetPackageManager(req)
3331
fqbn, err := cores.ParseFQBN(req.GetFqbn())
3432
if err != nil {
35-
return nil, status.Error(codes.InvalidArgument, "Error parsing fqbn")
33+
return nil, fmt.Errorf("parsing fqbn: %s", err)
3634
}
3735

3836
_, _, board, _, _, err := pm.ResolveFQBN(fqbn)
3937
if err != nil {
40-
return nil, status.Errorf(codes.Internal, "Error loading board data: %s", err)
38+
return nil, fmt.Errorf("loading board data: %s", err)
4139
}
4240

4341
details := &rpc.BoardDetailsResp{}

commands/compile/compile.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
"sort"
88
"strings"
99

10-
"google.golang.org/grpc/codes"
11-
"google.golang.org/grpc/status"
12-
1310
builder "github.com/arduino/arduino-builder"
1411
"github.com/arduino/arduino-builder/types"
1512
"github.com/arduino/arduino-cli/arduino/cores"
@@ -31,19 +28,19 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
3128
}
3229
sketch, err := cli.InitSketch(sketchPath)
3330
if err != nil {
34-
return nil, status.Errorf(codes.Internal, "Error opening sketch: %s", err)
31+
return nil, fmt.Errorf("opening sketch: %s", err)
3532
}
3633

3734
fqbnIn := req.GetFqbn()
3835
if fqbnIn == "" && sketch != nil && sketch.Metadata != nil {
3936
fqbnIn = sketch.Metadata.CPU.Fqbn
4037
}
4138
if fqbnIn == "" {
42-
return nil, status.Error(codes.InvalidArgument, "No Fully Qualified Board Name provided")
39+
return nil, fmt.Errorf("No Fully Qualified Board Name provided")
4340
}
4441
fqbn, err := cores.ParseFQBN(fqbnIn)
4542
if err != nil {
46-
return nil, status.Error(codes.InvalidArgument, "Fully Qualified Board Name has incorrect format")
43+
return nil, fmt.Errorf("incorrect FQBN: %s", err)
4744
}
4845

4946
pm, _ := cli.InitPackageAndLibraryManager()
@@ -57,12 +54,12 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
5754
core.InstallToolRelease(pm, ctags)
5855

5956
if err := pm.LoadHardware(cli.Config); err != nil {
60-
return nil, status.Errorf(codes.Internal, "Could not load hardware packages: %s", err)
57+
return nil, fmt.Errorf("loading hardware packages: %s", err)
6158
}
6259
ctags, _ = getBuiltinCtagsTool(pm)
6360
if !ctags.IsInstalled() {
6461
formatter.PrintErrorMessage("Missing ctags tool.")
65-
return nil, status.Error(codes.Internal, "Missing ctags tool")
62+
return nil, fmt.Errorf("Missing ctags tool")
6663
}
6764
}
6865

@@ -75,7 +72,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
7572
"\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
7673
cli.AppName+" core install %[1]s:%[2]s\".", fqbn.Package, fqbn.PlatformArch)
7774
formatter.PrintErrorMessage(errorMessage)
78-
return nil, status.Errorf(codes.Internal, "Platform not installed.")
75+
return nil, fmt.Errorf("Platform not installed")
7976
}
8077

8178
builderCtx := &types.Context{}
@@ -87,13 +84,13 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
8784
if packagesDir, err := cli.Config.HardwareDirectories(); err == nil {
8885
builderCtx.HardwareDirs = packagesDir
8986
} else {
90-
return nil, status.Errorf(codes.Internal, "Cannot get hardware directories: %s", err)
87+
return nil, fmt.Errorf("cannot get hardware directories: %s", err)
9188
}
9289

9390
if toolsDir, err := cli.Config.BundleToolsDirectories(); err == nil {
9491
builderCtx.ToolsDirs = toolsDir
9592
} else {
96-
return nil, status.Errorf(codes.Internal, "Cannot get bundled tools directories: %s", err)
93+
return nil, fmt.Errorf("cannot get bundled tools directories: %s", err)
9794
}
9895

9996
builderCtx.OtherLibrariesDirs = paths.NewPathList()
@@ -103,7 +100,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
103100
builderCtx.BuildPath = paths.New(req.GetBuildPath())
104101
err = builderCtx.BuildPath.MkdirAll()
105102
if err != nil {
106-
return nil, status.Errorf(codes.Internal, "Cannot create the build directory: %s", err)
103+
return nil, fmt.Errorf("cannot create build directory: %s", err)
107104
}
108105
}
109106

@@ -126,7 +123,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
126123
builderCtx.BuildCachePath = paths.New(req.GetBuildCachePath())
127124
err = builderCtx.BuildCachePath.MkdirAll()
128125
if err != nil {
129-
return nil, status.Errorf(codes.Internal, "Cannot create the build cache directory: %s", err)
126+
return nil, fmt.Errorf("cannot create build cache directory: %s", err)
130127
}
131128
}
132129

@@ -160,7 +157,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
160157
}
161158

162159
if err != nil {
163-
return nil, status.Errorf(codes.Internal, "Build failed: %s", err)
160+
return nil, fmt.Errorf("build failed: %s", err)
164161
}
165162

166163
// FIXME: Make a function to obtain these info...
@@ -190,7 +187,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
190187
dstHex := exportPath.Join(exportFile + ext)
191188
logrus.WithField("from", srcHex).WithField("to", dstHex).Print("copying sketch build output")
192189
if err = srcHex.CopyTo(dstHex); err != nil {
193-
return nil, status.Errorf(codes.Internal, "Error copying output file: %s", err)
190+
return nil, fmt.Errorf("copying output file: %s", err)
194191
}
195192

196193
// Copy .elf file to sketch directory
@@ -199,7 +196,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq) (*rpc.CompileResp, error)
199196
logrus.WithField("from", srcElf).WithField("to", dstElf).Print("copying sketch build output")
200197
if err = srcElf.CopyTo(dstElf); err != nil {
201198
formatter.PrintError(err, "Error copying elf file.")
202-
return nil, status.Errorf(codes.Internal, "Error copying elf file: %s", err)
199+
return nil, fmt.Errorf("copying elf file: %s", err)
203200
}
204201

205202
return &rpc.CompileResp{}, nil

0 commit comments

Comments
 (0)