Skip to content

Commit

Permalink
Added specific exit-code for "missing programmer" kind of errors. (#2429
Browse files Browse the repository at this point in the history
)

* Added gRPC message errors

* Implemented specific exit-code for "programmer not found" error
  • Loading branch information
cmaglie authored Nov 16, 2023
1 parent e071333 commit ff4ed3a
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 146 deletions.
3 changes: 2 additions & 1 deletion arduino/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ func (e *MissingProgrammerError) Error() string {

// ToRPCStatus converts the error into a *status.Status
func (e *MissingProgrammerError) ToRPCStatus() *status.Status {
return status.New(codes.InvalidArgument, e.Error())
s, _ := status.New(codes.InvalidArgument, e.Error()).WithDetails(&rpc.MissingProgrammerError{})
return s
}

// ProgrammerRequiredForUploadError is returned when the upload can be done only using a programmer
Expand Down
3 changes: 2 additions & 1 deletion commands/daemon/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ func (s *ArduinoCoreServerImpl) Debug(stream rpc.ArduinoCoreService_DebugServer)

// GetDebugConfig return metadata about a debug session
func (s *ArduinoCoreServerImpl) GetDebugConfig(ctx context.Context, req *rpc.GetDebugConfigRequest) (*rpc.GetDebugConfigResponse, error) {
return cmd.GetDebugConfig(ctx, req)
res, err := cmd.GetDebugConfig(ctx, req)
return res, convertErrorToRPCStatus(err)
}
11 changes: 10 additions & 1 deletion internal/cli/burnbootloader/burnbootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package burnbootloader

import (
"context"
"errors"
"os"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/commands/upload"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/internal/cli/arguments"
Expand Down Expand Up @@ -82,7 +84,14 @@ func runBootloaderCommand(command *cobra.Command, args []string) {
Programmer: programmer.String(instance, fqbn.String()),
DryRun: dryRun,
}, stdOut, stdErr); err != nil {
feedback.Fatal(tr("Error during Upload: %v", err), feedback.ErrGeneric)
errcode := feedback.ErrGeneric
if errors.Is(err, &arduino.ProgrammerRequiredForUploadError{}) {
errcode = feedback.ErrMissingProgrammer
}
if errors.Is(err, &arduino.MissingProgrammerError{}) {
errcode = feedback.ErrMissingProgrammer
}
feedback.Fatal(tr("Error during Upload: %v", err), errcode)
}
feedback.PrintResult(res())
}
9 changes: 8 additions & 1 deletion internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
}

if res, err := upload.Upload(context.Background(), uploadRequest, stdOut, stdErr); err != nil {
feedback.Fatal(tr("Error during Upload: %v", err), feedback.ErrGeneric)
errcode := feedback.ErrGeneric
if errors.Is(err, &arduino.ProgrammerRequiredForUploadError{}) {
errcode = feedback.ErrMissingProgrammer
}
if errors.Is(err, &arduino.MissingProgrammerError{}) {
errcode = feedback.ErrMissingProgrammer
}
feedback.Fatal(tr("Error during Upload: %v", err), errcode)
} else {
uploadRes = res
}
Expand Down
14 changes: 12 additions & 2 deletions internal/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package debug
import (
"context"
"encoding/json"
"errors"
"os"
"os/signal"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/commands/debug"
"github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/i18n"
Expand Down Expand Up @@ -93,7 +95,11 @@ func runDebugCommand(command *cobra.Command, args []string) {
if printInfo {

if res, err := debug.GetDebugConfig(context.Background(), debugConfigRequested); err != nil {
feedback.Fatal(tr("Error getting Debug info: %v", err), feedback.ErrBadArgument)
errcode := feedback.ErrBadArgument
if errors.Is(err, &arduino.MissingProgrammerError{}) {
errcode = feedback.ErrMissingProgrammer
}
feedback.Fatal(tr("Error getting Debug info: %v", err), errcode)
} else {
feedback.PrintResult(newDebugInfoResult(res))
}
Expand All @@ -109,7 +115,11 @@ func runDebugCommand(command *cobra.Command, args []string) {
feedback.FatalError(err, feedback.ErrBadArgument)
}
if _, err := debug.Debug(context.Background(), debugConfigRequested, in, out, ctrlc); err != nil {
feedback.Fatal(tr("Error during Debug: %v", err), feedback.ErrGeneric)
errcode := feedback.ErrGeneric
if errors.Is(err, &arduino.MissingProgrammerError{}) {
errcode = feedback.ErrMissingProgrammer
}
feedback.Fatal(tr("Error during Debug: %v", err), errcode)
}

}
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/feedback/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ const (
// ErrInitializingInventory is returned when the inventory cannot be initialized,
// usually depends on a wrong configuration of the data dir (10)
ErrInitializingInventory

// ErrMissingProgrammer is returned when the programmer argument is missing (11)
ErrMissingProgrammer
)
9 changes: 8 additions & 1 deletion internal/cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,14 @@ func runUploadCommand(args []string, uploadFieldsArgs map[string]string) {
UserFields: fields,
}
if res, err := upload.Upload(context.Background(), req, stdOut, stdErr); err != nil {
feedback.FatalError(err, feedback.ErrGeneric)
errcode := feedback.ErrGeneric
if errors.Is(err, &arduino.ProgrammerRequiredForUploadError{}) {
errcode = feedback.ErrMissingProgrammer
}
if errors.Is(err, &arduino.MissingProgrammerError{}) {
errcode = feedback.ErrMissingProgrammer
}
feedback.FatalError(err, errcode)
} else {
io := stdIOResult()
feedback.PrintResult(&uploadResult{
Expand Down
Loading

0 comments on commit ff4ed3a

Please sign in to comment.