Skip to content

Commit

Permalink
Use joined errors
Browse files Browse the repository at this point in the history
Go 1.20 added support for joined errors; using those instead of error
slices allows typical nil/non-nil semantics to be used, and allows all
error messages to be shown simultaneously when asserting.

This gets rid of HandleErrorList, and with it the implied
generic/validation error distinction. To handle that, HandleError
takes an additional error type parameter, making the distinction
explicit.

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt committed Nov 27, 2024
1 parent 65d75c3 commit 37c180f
Show file tree
Hide file tree
Showing 71 changed files with 945 additions and 970 deletions.
2 changes: 1 addition & 1 deletion cmd/skupper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ func main() {
rootCmd := root.NewSkupperRootCommand()

err := rootCmd.Execute()
utils.HandleError(err)
utils.HandleError(utils.GenericError, err)
}
5 changes: 3 additions & 2 deletions internal/cmd/generate-doc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
"os"

"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"

"github.com/skupperproject/skupper/internal/cmd/skupper/root"
"github.com/spf13/cobra/doc"
)
Expand All @@ -15,7 +16,7 @@ func main() {
fmt.Printf("%s\n\nUsage: generate-doc ./docsoutput\n", err)
os.Exit(1)
}
utils.HandleError(doc.GenMarkdownTree(root.NewSkupperRootCommand(), path))
utils.HandleError(utils.GenericError, doc.GenMarkdownTree(root.NewSkupperRootCommand(), path))

}

Expand Down
9 changes: 5 additions & 4 deletions internal/cmd/skupper/common/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package common

import (
"fmt"

"github.com/skupperproject/skupper/api/types"
"github.com/skupperproject/skupper/internal/cmd/skupper/common/utils"
"github.com/spf13/cobra"
)

type SkupperCommand interface {
NewClient(cobraCommand *cobra.Command, args []string)
ValidateInput(args []string) []error
ValidateInput(args []string) error
InputToOptions()
Run() error
WaitUntil() error
Expand Down Expand Up @@ -51,12 +52,12 @@ func ConfigureCobraCommand(configuredPlatform types.Platform, description Skuppe
return nil
},
Run: func(cmd *cobra.Command, args []string) {
utils.HandleErrorList(skupperCommand.ValidateInput(args))
utils.HandleError(utils.ValidationError, skupperCommand.ValidateInput(args))
skupperCommand.InputToOptions()
utils.HandleError(skupperCommand.Run())
utils.HandleError(utils.GenericError, skupperCommand.Run())
},
PostRun: func(cmd *cobra.Command, args []string) {
utils.HandleError(skupperCommand.WaitUntil())
utils.HandleError(utils.GenericError, skupperCommand.WaitUntil())
},
}

Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/skupper/common/command_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package common

import (
"testing"

"github.com/skupperproject/skupper/api/types"
"github.com/spf13/cobra"
"gotest.tools/assert"
"testing"
)

type MockSkupperCommand struct {
Expand All @@ -19,7 +20,7 @@ func (m *MockSkupperCommand) NewClient(cmd *cobra.Command, args []string) {
m.CalledNewClient = true
}

func (m *MockSkupperCommand) ValidateInput(args []string) []error {
func (m *MockSkupperCommand) ValidateInput(args []string) error {
m.CalledValidateInput = true
return nil
}
Expand Down
28 changes: 6 additions & 22 deletions internal/cmd/skupper/common/utils/handle_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,16 @@ import (
"syscall"
)

type ErrorType int

const (
GenericError = 1
ValidationError = 2
GenericError ErrorType = 1
ValidationError ErrorType = 2
)

func HandleError(err error) {
func HandleError(errType ErrorType, err error) {
if err != nil {
fmt.Println(err)
syscall.Exit(GenericError)
}
}

func HandleErrorList(errList []error) {
if errList != nil && len(errList) > 0 {
for _, err := range errList {
fmt.Println(err)
}

syscall.Exit(ValidationError)
}
}

func ErrorsToMessages(errs []error) []string {
messages := make([]string, len(errs))
for i, err := range errs {
messages[i] = err.Error()
syscall.Exit(int(errType))
}
return messages
}
11 changes: 6 additions & 5 deletions internal/cmd/skupper/connector/kube/connector_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kube

import (
"context"
"errors"
"fmt"
"strconv"
"time"
Expand All @@ -15,7 +16,7 @@ import (
pkgUtils "github.com/skupperproject/skupper/pkg/utils"
"github.com/skupperproject/skupper/pkg/utils/validator"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
k8serrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -45,14 +46,14 @@ func NewCmdConnectorCreate() *CmdConnectorCreate {

func (cmd *CmdConnectorCreate) NewClient(cobraCommand *cobra.Command, args []string) {
cli, err := client.NewClient(cobraCommand.Flag("namespace").Value.String(), cobraCommand.Flag("context").Value.String(), cobraCommand.Flag("kubeconfig").Value.String())
utils.HandleError(err)
utils.HandleError(utils.GenericError, err)

cmd.client = cli.GetSkupperClient().SkupperV2alpha1()
cmd.namespace = cli.Namespace
cmd.KubeClient = cli.Kube
}

func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
func (cmd *CmdConnectorCreate) ValidateInput(args []string) error {
var validationErrors []error
resourceStringValidator := validator.NewResourceStringValidator()
numberValidator := validator.NewNumberValidator()
Expand Down Expand Up @@ -92,7 +93,7 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
// Validate if there is already a Connector with this name in the namespace
if cmd.name != "" {
connector, err := cmd.client.Connectors(cmd.namespace).Get(context.TODO(), cmd.name, metav1.GetOptions{})
if connector != nil && !errors.IsNotFound(err) {
if connector != nil && !k8serrs.IsNotFound(err) {
validationErrors = append(validationErrors, fmt.Errorf("there is already a connector %s created for namespace %s", cmd.name, cmd.namespace))
}
}
Expand Down Expand Up @@ -203,7 +204,7 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
validationErrors = append(validationErrors, fmt.Errorf("output type is not valid: %s", err))
}
}
return validationErrors
return errors.Join(validationErrors...)
}

func (cmd *CmdConnectorCreate) InputToOptions() {
Expand Down
Loading

0 comments on commit 37c180f

Please sign in to comment.