From d28a78cf28f571481452fb26d4d34f0109691859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Mej=C3=ADas=20Rodr=C3=ADguez?= Date: Thu, 15 Aug 2019 01:11:56 -0400 Subject: [PATCH] Enhance error handling --- cmd/stackDeploy.go | 14 +++++------ cmd/stackList.go | 7 +++--- cmd/stackRemove.go | 14 +++++------ common/utils.go | 60 +++++++++++++++++++++------------------------- 4 files changed, 42 insertions(+), 53 deletions(-) diff --git a/cmd/stackDeploy.go b/cmd/stackDeploy.go index 629562b..90d2eb9 100644 --- a/cmd/stackDeploy.go +++ b/cmd/stackDeploy.go @@ -48,12 +48,11 @@ var stackDeployCmd = &cobra.Command{ } endpointSwarmClusterId, selectionErr := common.GetEndpointSwarmClusterId(endpointId) - switch selectionErr.(type) { - case nil: + if selectionErr == nil { // It's a swarm cluster - case *common.StackClusterNotFoundError: + } else if selectionErr == common.ErrStackClusterNotFound { // It's not a swarm cluster - default: + } else { // Something else happened common.CheckError(selectionErr) } @@ -63,8 +62,7 @@ var stackDeployCmd = &cobra.Command{ "endpoint": endpointId, }).Debug("Getting stack") retrievedStack, stackRetrievalErr := common.GetStackByName(stackName, endpointSwarmClusterId, endpointId) - switch stackRetrievalErr.(type) { - case nil: + if stackRetrievalErr == nil { // We are updating an existing stack logrus.WithFields(logrus.Fields{ "stack": retrievedStack.Name, @@ -110,7 +108,7 @@ var stackDeployCmd = &cobra.Command{ }).Info("Updating stack") err := portainerClient.UpdateStack(retrievedStack, newEnvironmentVariables, stackFileContent, viper.GetBool("stack.deploy.prune"), endpointId) common.CheckError(err) - case *common.StackNotFoundError: + } else if stackRetrievalErr == common.ErrStackNotFound { // We are deploying a new stack logrus.WithFields(logrus.Fields{ "stack": stackName, @@ -149,7 +147,7 @@ var stackDeployCmd = &cobra.Command{ "id": stack.ID, }).Info("Stack created") } - default: + } else { // Something else happened common.CheckError(stackRetrievalErr) } diff --git a/cmd/stackList.go b/cmd/stackList.go index 4faf93e..3fd140d 100644 --- a/cmd/stackList.go +++ b/cmd/stackList.go @@ -39,22 +39,21 @@ var stackListCmd = &cobra.Command{ if endpointId != 0 { var selectionErr error endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId) - switch selectionErr.(type) { - case nil: + if selectionErr == nil { // It's a swarm cluster logrus.WithFields(logrus.Fields{ "endpoint": endpointId, }).Debug("Getting stacks") stacks, err = portainerClient.GetStacks(endpointSwarmClusterId, endpointId) common.CheckError(err) - case *common.StackClusterNotFoundError: + } else if selectionErr == common.ErrStackClusterNotFound { // It's not a swarm cluster logrus.WithFields(logrus.Fields{ "endpoint": endpointId, }).Debug("Getting stacks") stacks, err = portainerClient.GetStacks("", endpointId) common.CheckError(err) - default: + } else { // Something else happened common.CheckError(selectionErr) } diff --git a/cmd/stackRemove.go b/cmd/stackRemove.go index d50af7f..512d735 100644 --- a/cmd/stackRemove.go +++ b/cmd/stackRemove.go @@ -41,28 +41,26 @@ var stackRemoveCmd = &cobra.Command{ var selectionErr, stackRetrievalErr error endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId) - switch selectionErr.(type) { - case nil: + if selectionErr == nil { // It's a swarm cluster logrus.WithFields(logrus.Fields{ "stack": stackName, "endpoint": endpointId, }).Debug("Getting stack") stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, endpointId) - case *common.StackClusterNotFoundError: + } else if selectionErr == common.ErrStackClusterNotFound { // It's not a swarm cluster logrus.WithFields(logrus.Fields{ "stack": stackName, "endpoint": endpointId, }).Debug("Getting stack") stack, stackRetrievalErr = common.GetStackByName(stackName, "", endpointId) - default: + } else { // Something else happened common.CheckError(selectionErr) } - switch stackRetrievalErr.(type) { - case nil: + if stackRetrievalErr == nil { // The stack exists stackId := stack.ID @@ -76,7 +74,7 @@ var stackRemoveCmd = &cobra.Command{ "stack": stack.Name, "endpoint": stack.EndpointID, }).Info("Stack removed") - case *common.StackNotFoundError: + } else if stackRetrievalErr == common.ErrStackNotFound { // The stack does not exist logrus.WithFields(logrus.Fields{ "stack": stackName, @@ -89,7 +87,7 @@ var stackRemoveCmd = &cobra.Command{ "suggestions": fmt.Sprintf("try with a different endpoint: psu stack rm %s --endpoint ENDPOINT_ID", stackName), }).Fatal("stack does not exist") } - default: + } else { // Something else happened common.CheckError(stackRetrievalErr) } diff --git a/common/utils.go b/common/utils.go index b65113f..93ec78b 100644 --- a/common/utils.go +++ b/common/utils.go @@ -1,7 +1,6 @@ package common import ( - "errors" "fmt" "reflect" @@ -9,6 +8,25 @@ import ( "github.com/sirupsen/logrus" ) +const ( + ErrStackNotFound = Error("Stack not found") + ErrStackClusterNotFound = Error("Stack cluster not found") + ErrSeveralEndpointsAvailable = Error("Several endpoints available") + ErrNoEndpointsAvailable = Error("No endpoints available") +) + +const ( + valueNotFoundError = Error("Value not found") +) + +// Error represents an application error. +type Error string + +// Error returns the error message. +func (e Error) Error() string { + return string(e) +} + func GetDefaultEndpoint() (endpoint portainer.Endpoint, err error) { portainerClient, err := GetClient() if err != nil { @@ -22,10 +40,10 @@ func GetDefaultEndpoint() (endpoint portainer.Endpoint, err error) { } if len(endpoints) == 0 { - err = errors.New("No endpoints available") + err = ErrNoEndpointsAvailable return } else if len(endpoints) > 1 { - err = errors.New("Several endpoints available") + err = ErrSeveralEndpointsAvailable return } endpoint = endpoints[0] @@ -49,9 +67,7 @@ func GetStackByName(name string, swarmId string, endpointId portainer.EndpointID return stack, nil } } - err = &StackNotFoundError{ - StackName: name, - } + err = ErrStackNotFound return } @@ -72,12 +88,11 @@ func GetEndpointSwarmClusterId(endpointId portainer.EndpointID) (endpointSwarmCl // Get swarm (if any) information for endpoint id, selectionErr := selectValue(result, []string{"Swarm", "Cluster", "ID"}) - switch selectionErr.(type) { - case nil: + if selectionErr == nil { endpointSwarmClusterId = id.(string) - case *valueNotFoundError: - err = &StackClusterNotFoundError{} - default: + } else if selectionErr == valueNotFoundError { + err = ErrStackClusterNotFound + } else { err = selectionErr } @@ -87,7 +102,7 @@ func GetEndpointSwarmClusterId(endpointId portainer.EndpointID) (endpointSwarmCl func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{}, error) { value := jsonMap[jsonPath[0]] if value == nil { - return nil, &valueNotFoundError{} + return nil, valueNotFoundError } else if len(jsonPath) > 1 { return selectValue(value.(map[string]interface{}), jsonPath[1:]) } else { @@ -122,24 +137,3 @@ func repr(t reflect.Type, margin, beforeMargin string) (r string) { } return } - -// Custom customerrors -type StackNotFoundError struct { - StackName string -} - -func (e *StackNotFoundError) Error() string { - return fmt.Sprintf("Stack %s not found", e.StackName) -} - -type valueNotFoundError struct{} - -func (e *valueNotFoundError) Error() string { - return "Value not found" -} - -type StackClusterNotFoundError struct{} - -func (e *StackClusterNotFoundError) Error() string { - return "Stack cluster not found" -}