Skip to content

Commit ca51b2b

Browse files
xuzhenglunvieux
authored andcommitted
fix incorrect ErrConnectFailed comparison
Signed-off-by: Reficul <xuzhenglun@gmail.com> (cherry picked from commit d5dc9b8) Signed-off-by: Victor Vieux <victorvieux@gmail.com>
1 parent 0403add commit ca51b2b

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

cli/command/container/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func getExecExitCode(ctx context.Context, client apiclient.ContainerAPIClient, e
170170
resp, err := client.ContainerExecInspect(ctx, execID)
171171
if err != nil {
172172
// If we can't connect, then the daemon probably died.
173-
if err != apiclient.ErrConnectionFailed {
173+
if !apiclient.IsErrConnectionFailed(err) {
174174
return false, -1, err
175175
}
176176
return false, -1, nil

cli/command/container/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func getExitCode(dockerCli *command.DockerCli, ctx context.Context, containerID
102102
c, err := dockerCli.Client().ContainerInspect(ctx, containerID)
103103
if err != nil {
104104
// If we can't connect, then the daemon probably died.
105-
if err != clientapi.ErrConnectionFailed {
105+
if !clientapi.IsErrConnectionFailed(err) {
106106
return false, -1, err
107107
}
108108
return false, -1, nil

client/errors.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
package client
22

33
import (
4-
"errors"
54
"fmt"
65

76
"github.com/docker/docker/api/types/versions"
7+
"github.com/pkg/errors"
88
)
99

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+
}
1228

1329
// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
1430
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}
1632
}
1733

1834
type notFound interface {

0 commit comments

Comments
 (0)