From 8efb4f189d9fbda674e1df2fecd0106f322b7cd1 Mon Sep 17 00:00:00 2001 From: Jin Xu Date: Sat, 2 Jul 2016 16:59:56 +0800 Subject: [PATCH] Add host to the connection error message When running docker client against a swarm cluster and connection to a node failed, it's not obvious to figure out the targeting host from the error message. This is related to issue: https://github.com/docker/swarm/issues/2393 Signed-off-by: Jin Xu v2: remove socket in the error message --- client/client.go | 3 +++ client/errors.go | 5 +++++ client/request.go | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/client/client.go b/client/client.go index f3ad2cf..02a70ba 100644 --- a/client/client.go +++ b/client/client.go @@ -18,6 +18,8 @@ const DefaultVersion string = "1.23" // Client is the API client that performs all operations // against a docker server. type Client struct { + // host holds the server address to connect to + host string // proto holds the client protocol i.e. unix. proto string // addr holds the client address. @@ -90,6 +92,7 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map } return &Client{ + host: host, proto: proto, addr: addr, basePath: basePath, diff --git a/client/errors.go b/client/errors.go index e026320..71e25a7 100644 --- a/client/errors.go +++ b/client/errors.go @@ -8,6 +8,11 @@ import ( // 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?") +// 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) +} + type notFound interface { error NotFound() bool // Is the error a NotFound error diff --git a/client/request.go b/client/request.go index 9cbb2b9..26e8769 100644 --- a/client/request.go +++ b/client/request.go @@ -123,11 +123,11 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q if err, ok := err.(net.Error); ok { if err.Timeout() { - return serverResp, ErrConnectionFailed + return serverResp, ErrorConnectionFailed(cli.host) } if !err.Temporary() { if strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") { - return serverResp, ErrConnectionFailed + return serverResp, ErrorConnectionFailed(cli.host) } } }