Enhance error handling

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-15 01:11:56 -04:00
parent 0dcc12dad0
commit d28a78cf28
4 changed files with 42 additions and 53 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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"
}