Use a typed error for consumers of libmachine

Signed-off-by: David Gageot <david@gageot.net>
This commit is contained in:
David Gageot 2016-03-02 22:35:44 +01:00
parent e3ff59bd92
commit 3aaf16db51
2 changed files with 17 additions and 3 deletions

View File

@ -2,14 +2,13 @@ package host
import ( import (
"errors" "errors"
"fmt"
"regexp" "regexp"
"strings"
"github.com/docker/machine/libmachine/auth" "github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/engine" "github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/mcnutils" "github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/provision" "github.com/docker/machine/libmachine/provision"
"github.com/docker/machine/libmachine/provision/pkgaction" "github.com/docker/machine/libmachine/provision/pkgaction"
@ -94,7 +93,10 @@ func (creator *StandardSSHClientCreator) CreateSSHClient(d drivers.Driver) (ssh.
func (h *Host) runActionForState(action func() error, desiredState state.State) error { func (h *Host) runActionForState(action func() error, desiredState state.State) error {
if drivers.MachineInState(h.Driver, desiredState)() { if drivers.MachineInState(h.Driver, desiredState)() {
return fmt.Errorf("Machine %q is already %s.", h.Name, strings.ToLower(desiredState.String())) return mcnerror.ErrHostAlreadyInState{
Name: h.Name,
State: desiredState,
}
} }
if err := action(); err != nil { if err := action(); err != nil {

View File

@ -3,6 +3,9 @@ package mcnerror
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings"
"github.com/docker/machine/libmachine/state"
) )
var ( var (
@ -32,3 +35,12 @@ type ErrDuringPreCreate struct {
func (e ErrDuringPreCreate) Error() string { func (e ErrDuringPreCreate) Error() string {
return fmt.Sprintf("Error with pre-create check: %q", e.Cause) return fmt.Sprintf("Error with pre-create check: %q", e.Cause)
} }
type ErrHostAlreadyInState struct {
Name string
State state.State
}
func (e ErrHostAlreadyInState) Error() string {
return fmt.Sprintf("Machine %q is already %s.", e.Name, strings.ToLower(e.State.String()))
}