Skip to content

Commit beadb78

Browse files
committed
Refactoring 'debug' commands
1 parent 5a2b8af commit beadb78

File tree

7 files changed

+53
-62
lines changed

7 files changed

+53
-62
lines changed

cli/debug/debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ func run(command *cobra.Command, args []string) {
100100
if printInfo {
101101

102102
if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
103-
feedback.Errorf(tr("Error getting Debug info: %v"), err.Message())
104-
errorcodes.ExitWithGrpcStatus(err)
103+
feedback.Errorf(tr("Error getting Debug info: %v"), err)
104+
os.Exit(errorcodes.ErrBadArgument)
105105
} else {
106106
feedback.PrintResult(&debugInfoResult{res})
107107
}

cli/errorcodes/errorcodes.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@
1515

1616
package errorcodes
1717

18-
import (
19-
"os"
20-
21-
"google.golang.org/grpc/codes"
22-
"google.golang.org/grpc/status"
23-
)
24-
2518
// Error codes to be used for os.Exit().
2619
const (
2720
_ = iota // 0 is not a valid exit error code
@@ -36,14 +29,3 @@ const (
3629
ErrCoreConfig
3730
ErrBadArgument
3831
)
39-
40-
// ExitWithGrpcStatus will terminate the current process by returing the correct
41-
// error code closest matching the gRPC status.
42-
func ExitWithGrpcStatus(s *status.Status) {
43-
switch s.Code() {
44-
case codes.Unimplemented:
45-
os.Exit(ErrBadArgument)
46-
default:
47-
os.Exit(ErrGeneric)
48-
}
49-
}

commands/daemon/debug.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (s *DebugService) Debug(stream dbg.DebugService_DebugServer) error {
5050
// Launch debug recipe attaching stdin and out to grpc streaming
5151
signalChan := make(chan os.Signal)
5252
defer close(signalChan)
53-
resp, stat := cmd.Debug(stream.Context(), req,
53+
resp, debugErr := cmd.Debug(stream.Context(), req,
5454
utils.ConsumeStreamFrom(func() ([]byte, error) {
5555
command, err := stream.Recv()
5656
if command.GetSendInterrupt() {
@@ -62,14 +62,13 @@ func (s *DebugService) Debug(stream dbg.DebugService_DebugServer) error {
6262
stream.Send(&dbg.DebugResponse{Data: data})
6363
}),
6464
signalChan)
65-
if stat != nil {
66-
return stat.Err()
65+
if debugErr != nil {
66+
return debugErr
6767
}
6868
return stream.Send(resp)
6969
}
7070

7171
// GetDebugConfig return metadata about a debug session
7272
func (s *DebugService) GetDebugConfig(ctx context.Context, req *dbg.DebugConfigRequest) (*dbg.GetDebugConfigResponse, error) {
73-
resp, err := cmd.GetDebugConfig(ctx, req)
74-
return resp, err.Err()
73+
return cmd.GetDebugConfig(ctx, req)
7574
}

commands/debug/debug.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ import (
3030
"github.com/arduino/arduino-cli/i18n"
3131
dbg "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
3232
"github.com/arduino/go-paths-helper"
33-
"github.com/pkg/errors"
3433
"github.com/sirupsen/logrus"
35-
"google.golang.org/grpc/codes"
36-
"google.golang.org/grpc/status"
3734
)
3835

3936
var tr = i18n.Tr
@@ -44,13 +41,13 @@ var tr = i18n.Tr
4441
// grpc Out <- tool stdOut
4542
// grpc Out <- tool stdErr
4643
// It also implements tool process lifecycle management
47-
func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader, out io.Writer, interrupt <-chan os.Signal) (*dbg.DebugResponse, *status.Status) {
44+
func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader, out io.Writer, interrupt <-chan os.Signal) (*dbg.DebugResponse, error) {
4845

4946
// Get debugging command line to run debugger
5047
pm := commands.GetPackageManager(req.GetInstance().GetId())
5148
commandLine, err := getCommandLine(req, pm)
5249
if err != nil {
53-
return nil, status.New(codes.FailedPrecondition, tr("Cannot get command line for tool: %s", err))
50+
return nil, err
5451
}
5552

5653
for i, arg := range commandLine {
@@ -66,7 +63,7 @@ func Debug(ctx context.Context, req *dbg.DebugConfigRequest, inStream io.Reader,
6663

6764
cmd, err := executils.NewProcess(commandLine...)
6865
if err != nil {
69-
return nil, status.New(codes.FailedPrecondition, tr("Cannot execute debug tool: %s", err))
66+
return nil, &commands.FailedDebugError{Message: tr("Cannot execute debug tool"), Cause: err}
7067
}
7168

7269
// Get stdIn pipe from tool
@@ -133,7 +130,7 @@ func getCommandLine(req *dbg.DebugConfigRequest, pm *packagemanager.PackageManag
133130
}
134131
gdbPath = paths.New(debugInfo.ToolchainPath).Join(gdbexecutable)
135132
default:
136-
return nil, errors.Errorf(tr("unsupported toolchain '%s'"), debugInfo.GetToolchain())
133+
return nil, &commands.FailedDebugError{Message: tr("Toolchain '%s' is not supported", debugInfo.GetToolchain())}
137134
}
138135
add(gdbPath.String())
139136

@@ -172,7 +169,7 @@ func getCommandLine(req *dbg.DebugConfigRequest, pm *packagemanager.PackageManag
172169
add(serverCmd)
173170

174171
default:
175-
return nil, errors.Errorf(tr("unsupported gdb server '%s'"), debugInfo.GetServer())
172+
return nil, &commands.FailedDebugError{Message: tr("GDB server '%s' is not supported", debugInfo.GetServer())}
176173
}
177174

178175
// Add executable

commands/debug/debug_info.go

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

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

2322
"github.com/arduino/arduino-cli/arduino/cores"
@@ -27,30 +26,25 @@ import (
2726
"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
2827
"github.com/arduino/go-paths-helper"
2928
"github.com/arduino/go-properties-orderedmap"
30-
"github.com/pkg/errors"
3129
"github.com/sirupsen/logrus"
32-
"google.golang.org/grpc/codes"
33-
"google.golang.org/grpc/status"
3430
)
3531

3632
// GetDebugConfig returns metadata to start debugging with the specified board
37-
func GetDebugConfig(ctx context.Context, req *debug.DebugConfigRequest) (*debug.GetDebugConfigResponse, *status.Status) {
33+
func GetDebugConfig(ctx context.Context, req *debug.DebugConfigRequest) (*debug.GetDebugConfigResponse, error) {
3834
pm := commands.GetPackageManager(req.GetInstance().GetId())
39-
40-
resp, err := getDebugProperties(req, pm)
41-
return resp, status.Convert(err)
35+
return getDebugProperties(req, pm)
4236
}
4337

4438
func getDebugProperties(req *debug.DebugConfigRequest, pm *packagemanager.PackageManager) (*debug.GetDebugConfigResponse, error) {
4539
// TODO: make a generic function to extract sketch from request
4640
// and remove duplication in commands/compile.go
4741
if req.GetSketchPath() == "" {
48-
return nil, fmt.Errorf(tr("missing sketchPath"))
42+
return nil, &commands.MissingSketchPathError{}
4943
}
5044
sketchPath := paths.New(req.GetSketchPath())
5145
sk, err := sketch.New(sketchPath)
5246
if err != nil {
53-
return nil, errors.Wrap(err, tr("opening sketch"))
47+
return nil, &commands.SketchNotFoundError{Cause: err}
5448
}
5549

5650
// XXX Remove this code duplication!!
@@ -59,17 +53,17 @@ func getDebugProperties(req *debug.DebugConfigRequest, pm *packagemanager.Packag
5953
fqbnIn = sk.Metadata.CPU.Fqbn
6054
}
6155
if fqbnIn == "" {
62-
return nil, fmt.Errorf(tr("no Fully Qualified Board Name provided"))
56+
return nil, &commands.MissingFQBNError{}
6357
}
6458
fqbn, err := cores.ParseFQBN(fqbnIn)
6559
if err != nil {
66-
return nil, errors.Wrap(err, tr("error parsing FQBN"))
60+
return nil, &commands.InvalidFQBNError{Cause: err}
6761
}
6862

6963
// Find target board and board properties
7064
_, platformRelease, board, boardProperties, referencedPlatformRelease, err := pm.ResolveFQBN(fqbn)
7165
if err != nil {
72-
return nil, errors.Wrap(err, tr("error resolving FQBN"))
66+
return nil, &commands.UnknownFQBNError{Cause: err}
7367
}
7468

7569
// Build configuration for debug
@@ -112,7 +106,7 @@ func getDebugProperties(req *debug.DebugConfigRequest, pm *packagemanager.Packag
112106
} else if refP, ok := referencedPlatformRelease.Programmers[req.GetProgrammer()]; ok {
113107
toolProperties.Merge(refP.Properties)
114108
} else {
115-
return nil, fmt.Errorf(tr("programmer '%s' not found"), req.GetProgrammer())
109+
return nil, &commands.ProgrammerNotFoundError{Programmer: req.GetProgrammer()}
116110
}
117111
}
118112

@@ -121,10 +115,10 @@ func getDebugProperties(req *debug.DebugConfigRequest, pm *packagemanager.Packag
121115
importPath = paths.New(importDir)
122116
}
123117
if !importPath.Exist() {
124-
return nil, fmt.Errorf(tr("compiled sketch not found in %s"), importPath)
118+
return nil, &commands.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
125119
}
126120
if !importPath.IsDir() {
127-
return nil, fmt.Errorf(tr("expected compiled sketch in directory %s, but is a file instead"), importPath)
121+
return nil, &commands.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
128122
}
129123
toolProperties.SetPath("build.path", importPath)
130124
toolProperties.Set("build.project_name", sk.Name+".ino")
@@ -144,7 +138,7 @@ func getDebugProperties(req *debug.DebugConfigRequest, pm *packagemanager.Packag
144138
}
145139

146140
if !debugProperties.ContainsKey("executable") {
147-
return nil, status.Error(codes.Unimplemented, fmt.Sprintf(tr("debugging not supported for board %s"), req.GetFqbn()))
141+
return nil, &commands.FailedDebugError{Message: tr("Debugging not supported for board %s", req.GetFqbn())}
148142
}
149143

150144
server := debugProperties.Get("server")

commands/errors.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,21 @@ func (e *ProgreammerRequiredForUploadError) ToRPCStatus() *status.Status {
144144
return st
145145
}
146146

147-
// UnknownProgrammerError is returned when the programmer is not found
148-
type UnknownProgrammerError struct {
149-
Cause error
147+
// ProgrammerNotFoundError is returned when the programmer is not found
148+
type ProgrammerNotFoundError struct {
149+
Programmer string
150+
Cause error
150151
}
151152

152-
func (e *UnknownProgrammerError) Error() string {
153-
return composeErrorMsg(tr("Unknown programmer"), e.Cause)
153+
func (e *ProgrammerNotFoundError) Error() string {
154+
return composeErrorMsg(tr("Programmer '%s' not found", e.Programmer), e.Cause)
154155
}
155156

156-
func (e *UnknownProgrammerError) Unwrap() error {
157+
func (e *ProgrammerNotFoundError) Unwrap() error {
157158
return e.Cause
158159
}
159160

160-
func (e *UnknownProgrammerError) ToRPCStatus() *status.Status {
161+
func (e *ProgrammerNotFoundError) ToRPCStatus() *status.Status {
161162
return status.New(codes.NotFound, e.Error())
162163
}
163164

@@ -212,7 +213,7 @@ type PlatformAlreadyAtTheLatestVersionError struct {
212213
}
213214

214215
func (e *PlatformAlreadyAtTheLatestVersionError) Error() string {
215-
return tr("Platform is already at the latest version")
216+
return tr("Platform '%s' is already at the latest version", e.Platform)
216217
}
217218

218219
func (e *PlatformAlreadyAtTheLatestVersionError) ToRPCStatus() *status.Status {
@@ -322,6 +323,24 @@ func (e *FailedUploadError) ToRPCStatus() *status.Status {
322323
return status.New(codes.Internal, e.Error())
323324
}
324325

326+
// FailedDebugError is returned when the debug fails
327+
type FailedDebugError struct {
328+
Message string
329+
Cause error
330+
}
331+
332+
func (e *FailedDebugError) Error() string {
333+
return composeErrorMsg(e.Message, e.Cause)
334+
}
335+
336+
func (e *FailedDebugError) Unwrap() error {
337+
return e.Cause
338+
}
339+
340+
func (e *FailedDebugError) ToRPCStatus() *status.Status {
341+
return status.New(codes.Internal, e.Error())
342+
}
343+
325344
// CompileFailedError is returned when the compile fails
326345
type CompileFailedError struct {
327346
Message string

commands/upload/upload.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func runProgramAction(pm *packagemanager.PackageManager,
218218
programmer = buildPlatform.Programmers[programmerID]
219219
}
220220
if programmer == nil {
221-
return &commands.UnknownProgrammerError{Cause: fmt.Errorf(tr("programmer '%s' not available"), programmerID)}
221+
return &commands.ProgrammerNotFoundError{Programmer: programmerID}
222222
}
223223
}
224224

@@ -352,13 +352,13 @@ func runProgramAction(pm *packagemanager.PackageManager,
352352
if !burnBootloader {
353353
importPath, sketchName, err := determineBuildPathAndSketchName(importFile, importDir, sk, fqbn)
354354
if err != nil {
355-
return &commands.FailedUploadError{Message: tr("Error finding build artifacts"), Cause: err}
355+
return &commands.NotFoundError{Message: tr("Error finding build artifacts"), Cause: err}
356356
}
357357
if !importPath.Exist() {
358-
return &commands.FailedUploadError{Message: tr("Compiled sketch not found in %s", importPath)}
358+
return &commands.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
359359
}
360360
if !importPath.IsDir() {
361-
return &commands.FailedUploadError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
361+
return &commands.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
362362
}
363363
uploadProperties.SetPath("build.path", importPath)
364364
uploadProperties.Set("build.project_name", sketchName)

0 commit comments

Comments
 (0)