client/request: use containerd errdefs checks

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-05-19 14:14:14 +02:00
committed by Sebastiaan van Stijn
parent 7e8b26ecb9
commit df96159df0
2 changed files with 6 additions and 10 deletions

View File

@@ -116,10 +116,8 @@ func (cli *Client) sendRequest(ctx context.Context, method, path string, query u
resp, err := cli.doRequest(req)
switch {
case errors.Is(err, context.Canceled):
return nil, errdefs.Cancelled(err)
case errors.Is(err, context.DeadlineExceeded):
return nil, errdefs.Deadline(err)
case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded):
return nil, err
case err == nil:
return resp, cli.checkResponseErr(resp)
default:

View File

@@ -12,9 +12,9 @@ import (
"testing"
"time"
cerrdefs "github.com/containerd/errdefs"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
@@ -91,7 +91,7 @@ func TestPlainTextError(t *testing.T) {
client: newMockClient(plainTextErrorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.ContainerList(context.Background(), container.ListOptions{})
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
// TestResponseErrors tests handling of error responses returned by the API.
@@ -221,7 +221,7 @@ func TestResponseErrors(t *testing.T) {
}
_, err := client.Ping(context.Background())
assert.Check(t, is.Error(err, tc.expected))
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
})
}
}
@@ -240,7 +240,7 @@ func TestInfiniteError(t *testing.T) {
}
_, err := client.Ping(context.Background())
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
assert.Check(t, is.ErrorContains(err, "request returned Internal Server Error"))
}
@@ -258,7 +258,6 @@ func TestCanceledContext(t *testing.T) {
cancel()
_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorType(err, errdefs.IsCancelled))
assert.Check(t, is.ErrorIs(err, context.Canceled))
}
@@ -278,6 +277,5 @@ func TestDeadlineExceededContext(t *testing.T) {
<-ctx.Done()
_, err := client.sendRequest(ctx, http.MethodGet, testEndpoint, nil, nil, nil)
assert.Check(t, is.ErrorType(err, errdefs.IsDeadline))
assert.Check(t, is.ErrorIs(err, context.DeadlineExceeded))
}