mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
registry: replace uses of errdefs package
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -3,7 +3,7 @@ package registry // import "github.com/docker/docker/registry"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/errdefs"
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
"gotest.tools/v3/assert"
|
||||
is "gotest.tools/v3/assert/cmp"
|
||||
)
|
||||
@@ -216,7 +216,7 @@ func TestLoadInsecureRegistries(t *testing.T) {
|
||||
t.Fatalf("expect error '%s', got no error", testCase.err)
|
||||
}
|
||||
assert.ErrorContains(t, err, testCase.err)
|
||||
assert.Check(t, errdefs.IsInvalidParameter(err))
|
||||
assert.Check(t, cerrdefs.IsInvalidArgument(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -263,7 +263,7 @@ func TestNewServiceConfig(t *testing.T) {
|
||||
_, err := newServiceConfig(tc.opts)
|
||||
if tc.errStr != "" {
|
||||
assert.Check(t, is.Error(err, tc.errStr))
|
||||
assert.Check(t, errdefs.IsInvalidParameter(err))
|
||||
assert.Check(t, cerrdefs.IsInvalidArgument(err))
|
||||
} else {
|
||||
assert.Check(t, err)
|
||||
}
|
||||
@@ -327,6 +327,6 @@ func TestValidateIndexNameWithError(t *testing.T) {
|
||||
for _, testCase := range invalid {
|
||||
_, err := ValidateIndexName(testCase.index)
|
||||
assert.Check(t, is.Error(err, testCase.err))
|
||||
assert.Check(t, errdefs.IsInvalidParameter(err))
|
||||
assert.Check(t, cerrdefs.IsInvalidArgument(err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/docker/errdefs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -15,7 +14,7 @@ func translateV2AuthError(err error) error {
|
||||
case errcode.Error:
|
||||
switch e2.Code {
|
||||
case errcode.ErrorCodeUnauthorized:
|
||||
return errdefs.Unauthorized(err)
|
||||
return unauthorizedErr{err}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,13 +23,49 @@ func translateV2AuthError(err error) error {
|
||||
}
|
||||
|
||||
func invalidParam(err error) error {
|
||||
return errdefs.InvalidParameter(err)
|
||||
return invalidParameterErr{err}
|
||||
}
|
||||
|
||||
func invalidParamf(format string, args ...interface{}) error {
|
||||
return errdefs.InvalidParameter(errors.Errorf(format, args...))
|
||||
return invalidParameterErr{errors.Errorf(format, args...)}
|
||||
}
|
||||
|
||||
func invalidParamWrapf(err error, format string, args ...interface{}) error {
|
||||
return errdefs.InvalidParameter(errors.Wrapf(err, format, args...))
|
||||
return invalidParameterErr{errors.Wrapf(err, format, args...)}
|
||||
}
|
||||
|
||||
type unauthorizedErr struct{ error }
|
||||
|
||||
func (unauthorizedErr) Unauthorized() {}
|
||||
|
||||
func (e unauthorizedErr) Cause() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
func (e unauthorizedErr) Unwrap() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
type invalidParameterErr struct{ error }
|
||||
|
||||
func (invalidParameterErr) InvalidParameter() {}
|
||||
|
||||
func (e invalidParameterErr) Unwrap() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
type systemErr struct{ error }
|
||||
|
||||
func (systemErr) System() {}
|
||||
|
||||
func (e systemErr) Unwrap() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
type errUnknown struct{ error }
|
||||
|
||||
func (errUnknown) Unknown() {}
|
||||
|
||||
func (e errUnknown) Unwrap() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/docker/distribution/registry/client/auth"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/docker/docker/errdefs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -48,7 +47,7 @@ func (s *Service) Search(ctx context.Context, searchFilters filters.Args, term s
|
||||
for _, hasStar := range hasStars {
|
||||
iHasStar, err := strconv.Atoi(hasStar)
|
||||
if err != nil {
|
||||
return nil, errdefs.InvalidParameter(errors.Wrapf(err, "invalid filter 'stars=%s'", hasStar))
|
||||
return nil, invalidParameterErr{errors.Wrapf(err, "invalid filter 'stars=%s'", hasStar)}
|
||||
}
|
||||
if iHasStar > hasStarFilter {
|
||||
hasStarFilter = iHasStar
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
|
||||
"github.com/containerd/log"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/docker/docker/errdefs"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -195,7 +194,7 @@ func authorizeClient(ctx context.Context, client *http.Client, authConfig *regis
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
return errdefs.System(errors.New("cookiejar.New is not supposed to return an error"))
|
||||
return systemErr{errors.New("cookiejar.New is not supposed to return an error")}
|
||||
}
|
||||
client.Jar = jar
|
||||
|
||||
@@ -231,17 +230,18 @@ func (r *session) searchRepositories(ctx context.Context, term string, limit int
|
||||
req.Header.Set("X-Docker-Token", "true")
|
||||
res, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, errdefs.System(err)
|
||||
return nil, systemErr{err}
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode != http.StatusOK {
|
||||
// TODO(thaJeztah): return upstream response body for errors (see https://github.com/moby/moby/issues/27286).
|
||||
return nil, errdefs.Unknown(fmt.Errorf("Unexpected status code %d", res.StatusCode))
|
||||
// TODO(thaJeztah): handle other status-codes to return correct error-type
|
||||
return nil, errUnknown{fmt.Errorf("Unexpected status code %d", res.StatusCode)}
|
||||
}
|
||||
result := ®istry.SearchResults{}
|
||||
err = json.NewDecoder(res.Body).Decode(result)
|
||||
if err != nil {
|
||||
return nil, errdefs.System(errors.Wrap(err, "error decoding registry search results"))
|
||||
return nil, systemErr{errors.Wrap(err, "error decoding registry search results")}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"net/http/httputil"
|
||||
"testing"
|
||||
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
"github.com/docker/distribution/registry/client/transport"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/docker/docker/errdefs"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
@@ -148,10 +148,10 @@ func TestSearchErrors(t *testing.T) {
|
||||
_, err = reg.Search(context.Background(), tc.filtersArgs, term, 0, nil, map[string][]string{})
|
||||
assert.ErrorContains(t, err, tc.expectedError)
|
||||
if tc.shouldReturnError {
|
||||
assert.Check(t, errdefs.IsUnknown(err), "got: %T: %v", err, err)
|
||||
assert.Check(t, cerrdefs.IsUnknown(err), "got: %T: %v", err, err)
|
||||
return
|
||||
}
|
||||
assert.Check(t, errdefs.IsInvalidParameter(err), "got: %T: %v", err, err)
|
||||
assert.Check(t, cerrdefs.IsInvalidArgument(err), "got: %T: %v", err, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ package registry // import "github.com/docker/docker/registry"
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
"github.com/containerd/log"
|
||||
"github.com/distribution/reference"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/docker/docker/errdefs"
|
||||
)
|
||||
|
||||
// Service is a registry service. It tracks configuration data such as a list
|
||||
@@ -77,7 +78,7 @@ func (s *Service) Auth(ctx context.Context, authConfig *registry.AuthConfig, use
|
||||
endpoints, err := s.lookupV2Endpoints(ctx, registryHostName, false)
|
||||
s.mu.RUnlock()
|
||||
if err != nil {
|
||||
if errdefs.IsContext(err) {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return "", "", err
|
||||
}
|
||||
return "", "", invalidParam(err)
|
||||
@@ -87,7 +88,7 @@ func (s *Service) Auth(ctx context.Context, authConfig *registry.AuthConfig, use
|
||||
for _, endpoint := range endpoints {
|
||||
authToken, err := loginV2(ctx, authConfig, endpoint, userAgent)
|
||||
if err != nil {
|
||||
if errdefs.IsContext(err) || errdefs.IsUnauthorized(err) {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || cerrdefs.IsUnauthorized(err) {
|
||||
// Failed to authenticate; don't continue with (non-TLS) endpoints.
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user