Browse code

fix incorrect ErrConnectFailed comparison

Signed-off-by: Reficul <xuzhenglun@gmail.com>

Reficul authored on 2016/11/22 11:42:55
Showing 3 changed files
... ...
@@ -170,7 +170,7 @@ func getExecExitCode(ctx context.Context, client apiclient.ContainerAPIClient, e
170 170
 	resp, err := client.ContainerExecInspect(ctx, execID)
171 171
 	if err != nil {
172 172
 		// If we can't connect, then the daemon probably died.
173
-		if err != apiclient.ErrConnectionFailed {
173
+		if !apiclient.IsErrConnectionFailed(err) {
174 174
 			return false, -1, err
175 175
 		}
176 176
 		return false, -1, nil
... ...
@@ -91,7 +91,7 @@ func getExitCode(ctx context.Context, dockerCli *command.DockerCli, containerID
91 91
 	c, err := dockerCli.Client().ContainerInspect(ctx, containerID)
92 92
 	if err != nil {
93 93
 		// If we can't connect, then the daemon probably died.
94
-		if err != clientapi.ErrConnectionFailed {
94
+		if !clientapi.IsErrConnectionFailed(err) {
95 95
 			return false, -1, err
96 96
 		}
97 97
 		return false, -1, nil
... ...
@@ -1,18 +1,34 @@
1 1
 package client
2 2
 
3 3
 import (
4
-	"errors"
5 4
 	"fmt"
6 5
 
7 6
 	"github.com/docker/docker/api/types/versions"
7
+	"github.com/pkg/errors"
8 8
 )
9 9
 
10
-// ErrConnectionFailed is an error raised when the connection between the client and the server failed.
11
-var ErrConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
10
+// errConnectionFailed implements an error returned when connection failed.
11
+type errConnectionFailed struct {
12
+	host string
13
+}
14
+
15
+// Error returns a string representation of an errConnectionFailed
16
+func (err errConnectionFailed) Error() string {
17
+	if err.host == "" {
18
+		return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
19
+	}
20
+	return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
21
+}
22
+
23
+// IsErrConnectionFailed returns true if the error is caused by connection failed.
24
+func IsErrConnectionFailed(err error) bool {
25
+	_, ok := errors.Cause(err).(errConnectionFailed)
26
+	return ok
27
+}
12 28
 
13 29
 // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
14 30
 func ErrorConnectionFailed(host string) error {
15
-	return fmt.Errorf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", host)
31
+	return errConnectionFailed{host: host}
16 32
 }
17 33
 
18 34
 type notFound interface {