From b35205ed1284dcc2237e48e4f2a4d915e779b848 Mon Sep 17 00:00:00 2001 From: Reficul Date: Tue, 22 Nov 2016 10:42:55 +0800 Subject: [PATCH] fix incorrect ErrConnectFailed comparison Signed-off-by: Reficul --- errors.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index 94c22a728a..854516669c 100644 --- a/errors.go +++ b/errors.go @@ -1,18 +1,34 @@ package client import ( - "errors" "fmt" "github.com/docker/docker/api/types/versions" + "github.com/pkg/errors" ) -// ErrConnectionFailed is an error raised when the connection between the client and the server failed. -var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?") +// errConnectionFailed implements an error returned when connection failed. +type errConnectionFailed struct { + host string +} + +// Error returns a string representation of an errConnectionFailed +func (err errConnectionFailed) Error() string { + if err.host == "" { + return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?" + } + return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host) +} + +// IsErrConnectionFailed returns true if the error is caused by connection failed. +func IsErrConnectionFailed(err error) bool { + _, ok := errors.Cause(err).(errConnectionFailed) + return ok +} // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed. func ErrorConnectionFailed(host string) error { - return fmt.Errorf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", host) + return errConnectionFailed{host: host} } type notFound interface {