Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use joined errors #1610

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion internal/cmd/skupper/common/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,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
21 changes: 21 additions & 0 deletions internal/cmd/skupper/common/testutils/validate_input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testutils

import (
"testing"

"gotest.tools/v3/assert"
)

type inputValidatingCommand interface {
ValidateInput(args []string) error
}

func CheckValidateInput(t *testing.T, command inputValidatingCommand, expectedError string, args []string) {
actualError := command.ValidateInput(args)

if expectedError == "" {
assert.NilError(t, actualError)
} else {
assert.Error(t, actualError, expectedError)
}
}
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"
"k8s.io/apimachinery/pkg/api/meta"
"strconv"
Expand All @@ -16,7 +17,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 @@ -47,14 +48,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 @@ -95,7 +96,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 @@ -214,7 +215,7 @@ func (cmd *CmdConnectorCreate) ValidateInput(args []string) []error {
}
}

return validationErrors
return errors.Join(validationErrors...)
}

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