mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Enhance error handling
This commit is contained in:
@ -48,12 +48,11 @@ var stackDeployCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpointSwarmClusterId, selectionErr := common.GetEndpointSwarmClusterId(endpointId)
|
endpointSwarmClusterId, selectionErr := common.GetEndpointSwarmClusterId(endpointId)
|
||||||
switch selectionErr.(type) {
|
if selectionErr == nil {
|
||||||
case nil:
|
|
||||||
// It's a swarm cluster
|
// It's a swarm cluster
|
||||||
case *common.StackClusterNotFoundError:
|
} else if selectionErr == common.ErrStackClusterNotFound {
|
||||||
// It's not a swarm cluster
|
// It's not a swarm cluster
|
||||||
default:
|
} else {
|
||||||
// Something else happened
|
// Something else happened
|
||||||
common.CheckError(selectionErr)
|
common.CheckError(selectionErr)
|
||||||
}
|
}
|
||||||
@ -63,8 +62,7 @@ var stackDeployCmd = &cobra.Command{
|
|||||||
"endpoint": endpointId,
|
"endpoint": endpointId,
|
||||||
}).Debug("Getting stack")
|
}).Debug("Getting stack")
|
||||||
retrievedStack, stackRetrievalErr := common.GetStackByName(stackName, endpointSwarmClusterId, endpointId)
|
retrievedStack, stackRetrievalErr := common.GetStackByName(stackName, endpointSwarmClusterId, endpointId)
|
||||||
switch stackRetrievalErr.(type) {
|
if stackRetrievalErr == nil {
|
||||||
case nil:
|
|
||||||
// We are updating an existing stack
|
// We are updating an existing stack
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stack": retrievedStack.Name,
|
"stack": retrievedStack.Name,
|
||||||
@ -110,7 +108,7 @@ var stackDeployCmd = &cobra.Command{
|
|||||||
}).Info("Updating stack")
|
}).Info("Updating stack")
|
||||||
err := portainerClient.UpdateStack(retrievedStack, newEnvironmentVariables, stackFileContent, viper.GetBool("stack.deploy.prune"), endpointId)
|
err := portainerClient.UpdateStack(retrievedStack, newEnvironmentVariables, stackFileContent, viper.GetBool("stack.deploy.prune"), endpointId)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
case *common.StackNotFoundError:
|
} else if stackRetrievalErr == common.ErrStackNotFound {
|
||||||
// We are deploying a new stack
|
// We are deploying a new stack
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stack": stackName,
|
"stack": stackName,
|
||||||
@ -149,7 +147,7 @@ var stackDeployCmd = &cobra.Command{
|
|||||||
"id": stack.ID,
|
"id": stack.ID,
|
||||||
}).Info("Stack created")
|
}).Info("Stack created")
|
||||||
}
|
}
|
||||||
default:
|
} else {
|
||||||
// Something else happened
|
// Something else happened
|
||||||
common.CheckError(stackRetrievalErr)
|
common.CheckError(stackRetrievalErr)
|
||||||
}
|
}
|
||||||
|
@ -39,22 +39,21 @@ var stackListCmd = &cobra.Command{
|
|||||||
if endpointId != 0 {
|
if endpointId != 0 {
|
||||||
var selectionErr error
|
var selectionErr error
|
||||||
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId)
|
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId)
|
||||||
switch selectionErr.(type) {
|
if selectionErr == nil {
|
||||||
case nil:
|
|
||||||
// It's a swarm cluster
|
// It's a swarm cluster
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"endpoint": endpointId,
|
"endpoint": endpointId,
|
||||||
}).Debug("Getting stacks")
|
}).Debug("Getting stacks")
|
||||||
stacks, err = portainerClient.GetStacks(endpointSwarmClusterId, endpointId)
|
stacks, err = portainerClient.GetStacks(endpointSwarmClusterId, endpointId)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
case *common.StackClusterNotFoundError:
|
} else if selectionErr == common.ErrStackClusterNotFound {
|
||||||
// It's not a swarm cluster
|
// It's not a swarm cluster
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"endpoint": endpointId,
|
"endpoint": endpointId,
|
||||||
}).Debug("Getting stacks")
|
}).Debug("Getting stacks")
|
||||||
stacks, err = portainerClient.GetStacks("", endpointId)
|
stacks, err = portainerClient.GetStacks("", endpointId)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
default:
|
} else {
|
||||||
// Something else happened
|
// Something else happened
|
||||||
common.CheckError(selectionErr)
|
common.CheckError(selectionErr)
|
||||||
}
|
}
|
||||||
|
@ -41,28 +41,26 @@ var stackRemoveCmd = &cobra.Command{
|
|||||||
|
|
||||||
var selectionErr, stackRetrievalErr error
|
var selectionErr, stackRetrievalErr error
|
||||||
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId)
|
endpointSwarmClusterId, selectionErr = common.GetEndpointSwarmClusterId(endpointId)
|
||||||
switch selectionErr.(type) {
|
if selectionErr == nil {
|
||||||
case nil:
|
|
||||||
// It's a swarm cluster
|
// It's a swarm cluster
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stack": stackName,
|
"stack": stackName,
|
||||||
"endpoint": endpointId,
|
"endpoint": endpointId,
|
||||||
}).Debug("Getting stack")
|
}).Debug("Getting stack")
|
||||||
stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, endpointId)
|
stack, stackRetrievalErr = common.GetStackByName(stackName, endpointSwarmClusterId, endpointId)
|
||||||
case *common.StackClusterNotFoundError:
|
} else if selectionErr == common.ErrStackClusterNotFound {
|
||||||
// It's not a swarm cluster
|
// It's not a swarm cluster
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stack": stackName,
|
"stack": stackName,
|
||||||
"endpoint": endpointId,
|
"endpoint": endpointId,
|
||||||
}).Debug("Getting stack")
|
}).Debug("Getting stack")
|
||||||
stack, stackRetrievalErr = common.GetStackByName(stackName, "", endpointId)
|
stack, stackRetrievalErr = common.GetStackByName(stackName, "", endpointId)
|
||||||
default:
|
} else {
|
||||||
// Something else happened
|
// Something else happened
|
||||||
common.CheckError(selectionErr)
|
common.CheckError(selectionErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch stackRetrievalErr.(type) {
|
if stackRetrievalErr == nil {
|
||||||
case nil:
|
|
||||||
// The stack exists
|
// The stack exists
|
||||||
stackId := stack.ID
|
stackId := stack.ID
|
||||||
|
|
||||||
@ -76,7 +74,7 @@ var stackRemoveCmd = &cobra.Command{
|
|||||||
"stack": stack.Name,
|
"stack": stack.Name,
|
||||||
"endpoint": stack.EndpointID,
|
"endpoint": stack.EndpointID,
|
||||||
}).Info("Stack removed")
|
}).Info("Stack removed")
|
||||||
case *common.StackNotFoundError:
|
} else if stackRetrievalErr == common.ErrStackNotFound {
|
||||||
// The stack does not exist
|
// The stack does not exist
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stack": stackName,
|
"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),
|
"suggestions": fmt.Sprintf("try with a different endpoint: psu stack rm %s --endpoint ENDPOINT_ID", stackName),
|
||||||
}).Fatal("stack does not exist")
|
}).Fatal("stack does not exist")
|
||||||
}
|
}
|
||||||
default:
|
} else {
|
||||||
// Something else happened
|
// Something else happened
|
||||||
common.CheckError(stackRetrievalErr)
|
common.CheckError(stackRetrievalErr)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
@ -9,6 +8,25 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"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) {
|
func GetDefaultEndpoint() (endpoint portainer.Endpoint, err error) {
|
||||||
portainerClient, err := GetClient()
|
portainerClient, err := GetClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -22,10 +40,10 @@ func GetDefaultEndpoint() (endpoint portainer.Endpoint, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(endpoints) == 0 {
|
if len(endpoints) == 0 {
|
||||||
err = errors.New("No endpoints available")
|
err = ErrNoEndpointsAvailable
|
||||||
return
|
return
|
||||||
} else if len(endpoints) > 1 {
|
} else if len(endpoints) > 1 {
|
||||||
err = errors.New("Several endpoints available")
|
err = ErrSeveralEndpointsAvailable
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
endpoint = endpoints[0]
|
endpoint = endpoints[0]
|
||||||
@ -49,9 +67,7 @@ func GetStackByName(name string, swarmId string, endpointId portainer.EndpointID
|
|||||||
return stack, nil
|
return stack, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = &StackNotFoundError{
|
err = ErrStackNotFound
|
||||||
StackName: name,
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,12 +88,11 @@ func GetEndpointSwarmClusterId(endpointId portainer.EndpointID) (endpointSwarmCl
|
|||||||
|
|
||||||
// Get swarm (if any) information for endpoint
|
// Get swarm (if any) information for endpoint
|
||||||
id, selectionErr := selectValue(result, []string{"Swarm", "Cluster", "ID"})
|
id, selectionErr := selectValue(result, []string{"Swarm", "Cluster", "ID"})
|
||||||
switch selectionErr.(type) {
|
if selectionErr == nil {
|
||||||
case nil:
|
|
||||||
endpointSwarmClusterId = id.(string)
|
endpointSwarmClusterId = id.(string)
|
||||||
case *valueNotFoundError:
|
} else if selectionErr == valueNotFoundError {
|
||||||
err = &StackClusterNotFoundError{}
|
err = ErrStackClusterNotFound
|
||||||
default:
|
} else {
|
||||||
err = selectionErr
|
err = selectionErr
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +102,7 @@ func GetEndpointSwarmClusterId(endpointId portainer.EndpointID) (endpointSwarmCl
|
|||||||
func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{}, error) {
|
func selectValue(jsonMap map[string]interface{}, jsonPath []string) (interface{}, error) {
|
||||||
value := jsonMap[jsonPath[0]]
|
value := jsonMap[jsonPath[0]]
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return nil, &valueNotFoundError{}
|
return nil, valueNotFoundError
|
||||||
} else if len(jsonPath) > 1 {
|
} else if len(jsonPath) > 1 {
|
||||||
return selectValue(value.(map[string]interface{}), jsonPath[1:])
|
return selectValue(value.(map[string]interface{}), jsonPath[1:])
|
||||||
} else {
|
} else {
|
||||||
@ -122,24 +137,3 @@ func repr(t reflect.Type, margin, beforeMargin string) (r string) {
|
|||||||
}
|
}
|
||||||
return
|
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"
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user