Browse code

client: Client.doRequest: add special handling for "not found" errors

Before this change, a generic "Cannot connect to the docker daemon" error
was produced which, while helpful, instructed the user to check if the daemon
was running, but didn't provide context on the reason we failed (i.e., the
socket was not found).

This patch adds a dedicated check for cases where the socket was not found,
and preserves the original error.

Before this patch:

DOCKER_HOST=unix:///var/run/no.sock docker version
Cannot connect to the Docker daemon at unix:///var/run/no.sock. Is the docker daemon running?

With this patch:

DOCKER_HOST=unix:///var/run/no.sock docker version
failed to connect to the docker API at unix:///var/run/no.sock; check if the path is correct and the daemon is running: dial unix /var/run/no.sock: connect: no such file or directory

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/06/26 23:41:40
Showing 2 changed files
... ...
@@ -158,6 +158,12 @@ func (cli *Client) doRequest(req *http.Request) (*http.Response, error) {
158 158
 		// which are irrelevant if we weren't able to connect.
159 159
 		return nil, errConnectionFailed{fmt.Errorf("permission denied while trying to connect to the docker API at %v", cli.host)}
160 160
 	}
161
+	if errors.Is(err, os.ErrNotExist) {
162
+		// Unwrap the error to remove request errors ("Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/version"),
163
+		// which are irrelevant if we weren't able to connect.
164
+		err = errors.Unwrap(err)
165
+		return nil, errConnectionFailed{errors.Wrapf(err, "failed to connect to the docker API at %v; check if the path is correct and if the daemon is running", cli.host)}
166
+	}
161 167
 
162 168
 	var nErr net.Error
163 169
 	if errors.As(err, &nErr) {
... ...
@@ -158,6 +158,12 @@ func (cli *Client) doRequest(req *http.Request) (*http.Response, error) {
158 158
 		// which are irrelevant if we weren't able to connect.
159 159
 		return nil, errConnectionFailed{fmt.Errorf("permission denied while trying to connect to the docker API at %v", cli.host)}
160 160
 	}
161
+	if errors.Is(err, os.ErrNotExist) {
162
+		// Unwrap the error to remove request errors ("Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/version"),
163
+		// which are irrelevant if we weren't able to connect.
164
+		err = errors.Unwrap(err)
165
+		return nil, errConnectionFailed{errors.Wrapf(err, "failed to connect to the docker API at %v; check if the path is correct and if the daemon is running", cli.host)}
166
+	}
161 167
 
162 168
 	var nErr net.Error
163 169
 	if errors.As(err, &nErr) {