Browse code

client: Client.doRequest: simplify permission check and unwrap error

Previously, we were using os.IsPermission, which doesn't unwrap errors;
change to use `errors.Is` to detect permission errors, and unwrap the
error to remove information about the request, which is irrelevant if
we weren't able to connect in the first place.

Also tweak the error slightly to not assume "docker socket", instead
mentioning "docker API".

Before this;

permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/version": dial unix /var/run/docker.sock: connect: permission denied

With this patch applied:

permission denied while trying to connect to the docker API at unix:///var/run/docker.sock: dial unix /var/run/docker.sock: connect: permission denied

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

Sebastiaan van Stijn authored on 2025/06/26 21:36:30
Showing 2 changed files
... ...
@@ -153,14 +153,10 @@ func (cli *Client) doRequest(req *http.Request) (*http.Response, error) {
153 153
 		return nil, err
154 154
 	}
155 155
 
156
-	var uErr *url.Error
157
-	if errors.As(err, &uErr) {
158
-		var nErr *net.OpError
159
-		if errors.As(uErr.Err, &nErr) {
160
-			if os.IsPermission(nErr.Err) {
161
-				return nil, errConnectionFailed{errors.Wrapf(err, "permission denied while trying to connect to the Docker daemon socket at %v", cli.host)}
162
-			}
163
-		}
156
+	if errors.Is(err, os.ErrPermission) {
157
+		// Don't include request errors ("Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/version"),
158
+		// which are irrelevant if we weren't able to connect.
159
+		return nil, errConnectionFailed{fmt.Errorf("permission denied while trying to connect to the docker API at %v", cli.host)}
164 160
 	}
165 161
 
166 162
 	var nErr net.Error
... ...
@@ -153,14 +153,10 @@ func (cli *Client) doRequest(req *http.Request) (*http.Response, error) {
153 153
 		return nil, err
154 154
 	}
155 155
 
156
-	var uErr *url.Error
157
-	if errors.As(err, &uErr) {
158
-		var nErr *net.OpError
159
-		if errors.As(uErr.Err, &nErr) {
160
-			if os.IsPermission(nErr.Err) {
161
-				return nil, errConnectionFailed{errors.Wrapf(err, "permission denied while trying to connect to the Docker daemon socket at %v", cli.host)}
162
-			}
163
-		}
156
+	if errors.Is(err, os.ErrPermission) {
157
+		// Don't include request errors ("Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.51/version"),
158
+		// which are irrelevant if we weren't able to connect.
159
+		return nil, errConnectionFailed{fmt.Errorf("permission denied while trying to connect to the docker API at %v", cli.host)}
164 160
 	}
165 161
 
166 162
 	var nErr net.Error