Browse code

Merge pull request #15370 from cpuguy83/better_error_on_client_connect

Better/more specific error messages on connect

David Calavera authored on 2015/08/08 05:00:44
Showing 3 changed files
... ...
@@ -30,7 +30,7 @@ import (
30 30
 )
31 31
 
32 32
 var (
33
-	errConnectionRefused = errors.New("Cannot connect to the Docker daemon. Is 'docker -d' running on this host?")
33
+	errConnectionFailed = errors.New("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
34 34
 )
35 35
 
36 36
 type serverResponse struct {
... ...
@@ -94,13 +94,14 @@ func (cli *DockerCli) clientRequest(method, path string, in io.Reader, headers m
94 94
 	if resp != nil {
95 95
 		serverResp.statusCode = resp.StatusCode
96 96
 	}
97
+
97 98
 	if err != nil {
98
-		if strings.Contains(err.Error(), "connection refused") {
99
-			return serverResp, errConnectionRefused
99
+		if types.IsTimeout(err) || strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial unix") {
100
+			return serverResp, errConnectionFailed
100 101
 		}
101 102
 
102
-		if cli.tlsConfig == nil {
103
-			return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?\n* Is your docker daemon up and running?", err)
103
+		if cli.tlsConfig == nil && strings.Contains(err.Error(), "malformed HTTP response") {
104
+			return serverResp, fmt.Errorf("%v.\n* Are you trying to connect to a TLS-enabled daemon without TLS?", err)
104 105
 		}
105 106
 		if cli.tlsConfig != nil && strings.Contains(err.Error(), "remote error: bad certificate") {
106 107
 			return serverResp, fmt.Errorf("The server probably has client authentication (--tlsverify) enabled. Please check your TLS client certification settings: %v", err)
... ...
@@ -280,7 +281,7 @@ func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
280 280
 	serverResp, err := cli.call("GET", "/containers/"+containerID+"/json", nil, nil)
281 281
 	if err != nil {
282 282
 		// If we can't connect, then the daemon probably died.
283
-		if err != errConnectionRefused {
283
+		if err != errConnectionFailed {
284 284
 			return false, -1, err
285 285
 		}
286 286
 		return false, -1, nil
... ...
@@ -302,7 +303,7 @@ func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
302 302
 	serverResp, err := cli.call("GET", "/exec/"+execID+"/json", nil, nil)
303 303
 	if err != nil {
304 304
 		// If we can't connect, then the daemon probably died.
305
-		if err != errConnectionRefused {
305
+		if err != errConnectionFailed {
306 306
 			return false, -1, err
307 307
 		}
308 308
 		return false, -1, nil
309 309
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package types
1
+
2
+import (
3
+	"net"
4
+	"net/url"
5
+)
6
+
7
+// IsTimeout takes an error returned from (generally) the http package and determines if it is a timeout error.
8
+func IsTimeout(err error) bool {
9
+	switch e := err.(type) {
10
+	case net.Error:
11
+		return e.Timeout()
12
+	case *url.Error:
13
+		if t, ok := e.Err.(net.Error); ok {
14
+			return t.Timeout()
15
+		}
16
+		return false
17
+	default:
18
+		return false
19
+	}
20
+}
... ...
@@ -20,6 +20,7 @@ import (
20 20
 	"time"
21 21
 
22 22
 	"github.com/Sirupsen/logrus"
23
+	"github.com/docker/docker/api/types"
23 24
 	"github.com/docker/docker/cliconfig"
24 25
 	"github.com/docker/docker/pkg/httputils"
25 26
 	"github.com/docker/docker/pkg/ioutils"
... ...
@@ -424,7 +425,7 @@ func (r *Session) GetRepositoryData(remote string) (*RepositoryData, error) {
424 424
 		// and return a non-obtuse error message for users
425 425
 		// "Get https://index.docker.io/v1/repositories/library/busybox/images: i/o timeout"
426 426
 		// was a top search on the docker user forum
427
-		if strings.HasSuffix(err.Error(), "i/o timeout") {
427
+		if types.IsTimeout(err) {
428 428
 			return nil, fmt.Errorf("Network timed out while trying to connect to %s. You may want to check your internet connection or if you are behind a proxy.", repositoryTarget)
429 429
 		}
430 430
 		return nil, fmt.Errorf("Error while pulling image: %v", err)