Make Errors a []Error

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-05-26 17:18:32 -07:00
parent f565d6abb7
commit 00b1e8fca0
13 changed files with 98 additions and 116 deletions

View file

@ -760,7 +760,7 @@ func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, error
t.Fatalf("unexpected error decoding error response: %v", err)
}
if len(errs.Errors) == 0 {
if len(errs) == 0 {
t.Fatalf("expected errors in response")
}
@ -780,7 +780,7 @@ func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, error
counts[code] = 0
}
for _, err := range errs.Errors {
for _, err := range errs {
if _, ok := expected[err.Code]; !ok {
t.Fatalf("unexpected error code %v encountered during %s: %s ", err.Code, msg, string(p))
}

View file

@ -346,9 +346,9 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
switch err := err.(type) {
case distribution.ErrRepositoryUnknown:
context.Errors.Push(v2.ErrorCodeNameUnknown, err)
context.Errors = append(context.Errors, errcode.NewError(v2.ErrorCodeNameUnknown, err))
case distribution.ErrRepositoryNameInvalid:
context.Errors.Push(v2.ErrorCodeNameInvalid, err)
context.Errors = append(context.Errors, errcode.NewError(v2.ErrorCodeNameInvalid, err))
}
serveJSON(w, context.Errors)
@ -363,7 +363,7 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
context.Repository, err = applyRepoMiddleware(context.Repository, app.Config.Middleware["repository"])
if err != nil {
ctxu.GetLogger(context).Errorf("error initializing repository middleware: %v", err)
context.Errors.Push(errcode.ErrorCodeUnknown, err)
context.Errors = append(context.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
serveJSON(w, context.Errors)
return
@ -383,9 +383,9 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
}
func (app *App) logError(context context.Context, errors errcode.Errors) {
for _, e := range errors.Errors {
for _, e := range errors {
c := ctxu.WithValue(context, "err.code", e.Code)
c = ctxu.WithValue(c, "err.message", e.Message)
c = ctxu.WithValue(c, "err.message", e.Code.Message())
c = ctxu.WithValue(c, "err.detail", e.Detail)
c = ctxu.WithLogger(c, ctxu.GetLogger(c,
"err.code",
@ -441,7 +441,7 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont
// proceed.
var errs errcode.Errors
errs.Push(v2.ErrorCodeUnauthorized)
errs = append(errs, errcode.NewError(v2.ErrorCodeUnauthorized))
serveJSON(w, errs)
return fmt.Errorf("forbidden: no repository name")
@ -464,7 +464,7 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont
err.ServeHTTP(w, r)
var errs errcode.Errors
errs.Push(v2.ErrorCodeUnauthorized, accessRecords)
errs = append(errs, errcode.NewError(v2.ErrorCodeUnauthorized, accessRecords))
serveJSON(w, errs)
default:
// This condition is a potential security problem either in

View file

@ -203,8 +203,8 @@ func TestNewApp(t *testing.T) {
t.Fatalf("error decoding error response: %v", err)
}
if errs.Errors[0].Code != v2.ErrorCodeUnauthorized {
t.Fatalf("unexpected error code: %v != %v", errs.Errors[0].Code, v2.ErrorCodeUnauthorized)
if errs[0].Code != v2.ErrorCodeUnauthorized {
t.Fatalf("unexpected error code: %v != %v", errs[0].Code, v2.ErrorCodeUnauthorized)
}
}

View file

@ -18,12 +18,12 @@ func blobDispatcher(ctx *Context, r *http.Request) http.Handler {
if err == errDigestNotAvailable {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
ctx.Errors = append(ctx.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, err))
})
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
ctx.Errors = append(ctx.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, err))
})
}
@ -53,16 +53,16 @@ func (bh *blobHandler) GetBlob(w http.ResponseWriter, r *http.Request) {
desc, err := blobs.Stat(bh, bh.Digest)
if err != nil {
if err == distribution.ErrBlobUnknown {
bh.Errors.Push(v2.ErrorCodeBlobUnknown, bh.Digest)
bh.Errors = append(bh.Errors, errcode.NewError(v2.ErrorCodeBlobUnknown, bh.Digest))
} else {
bh.Errors.Push(errcode.ErrorCodeUnknown, err)
bh.Errors = append(bh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
}
return
}
if err := blobs.ServeBlob(bh, w, r, desc.Digest); err != nil {
context.GetLogger(bh).Debugf("unexpected error getting blob HTTP handler: %v", err)
bh.Errors.Push(errcode.ErrorCodeUnknown, err)
bh.Errors = append(bh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
}

View file

@ -37,7 +37,7 @@ func blobUploadDispatcher(ctx *Context, r *http.Request) http.Handler {
if err != nil {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxu.GetLogger(ctx).Infof("error resolving upload: %v", err)
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
})
}
buh.State = state
@ -45,14 +45,14 @@ func blobUploadDispatcher(ctx *Context, r *http.Request) http.Handler {
if state.Name != ctx.Repository.Name() {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxu.GetLogger(ctx).Infof("mismatched repository name in upload state: %q != %q", state.Name, buh.Repository.Name())
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
})
}
if state.UUID != buh.UUID {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctxu.GetLogger(ctx).Infof("mismatched uuid in upload state: %q != %q", state.UUID, buh.UUID)
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
})
}
@ -62,12 +62,12 @@ func blobUploadDispatcher(ctx *Context, r *http.Request) http.Handler {
ctxu.GetLogger(ctx).Errorf("error resolving upload: %v", err)
if err == distribution.ErrBlobUploadUnknown {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadUnknown, err))
})
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
})
}
buh.Upload = upload
@ -81,14 +81,14 @@ func blobUploadDispatcher(ctx *Context, r *http.Request) http.Handler {
defer upload.Close()
ctxu.GetLogger(ctx).Infof("error seeking blob upload: %v", err)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
upload.Cancel(buh)
})
} else if nn != buh.State.Offset {
defer upload.Close()
ctxu.GetLogger(ctx).Infof("seek to wrong offest: %d != %d", nn, buh.State.Offset)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
upload.Cancel(buh)
})
}
@ -119,7 +119,7 @@ func (buh *blobUploadHandler) StartBlobUpload(w http.ResponseWriter, r *http.Req
blobs := buh.Repository.Blobs(buh)
upload, err := blobs.Create(buh)
if err != nil {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -127,7 +127,7 @@ func (buh *blobUploadHandler) StartBlobUpload(w http.ResponseWriter, r *http.Req
defer buh.Upload.Close()
if err := buh.blobUploadResponse(w, r, true); err != nil {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -138,7 +138,7 @@ func (buh *blobUploadHandler) StartBlobUpload(w http.ResponseWriter, r *http.Req
// GetUploadStatus returns the status of a given upload, identified by id.
func (buh *blobUploadHandler) GetUploadStatus(w http.ResponseWriter, r *http.Request) {
if buh.Upload == nil {
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadUnknown))
return
}
@ -146,7 +146,7 @@ func (buh *blobUploadHandler) GetUploadStatus(w http.ResponseWriter, r *http.Req
// resumable upload is supported. This will enable returning a non-zero
// range for clients to begin uploading at an offset.
if err := buh.blobUploadResponse(w, r, true); err != nil {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -157,13 +157,13 @@ func (buh *blobUploadHandler) GetUploadStatus(w http.ResponseWriter, r *http.Req
// PatchBlobData writes data to an upload.
func (buh *blobUploadHandler) PatchBlobData(w http.ResponseWriter, r *http.Request) {
if buh.Upload == nil {
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadUnknown))
return
}
ct := r.Header.Get("Content-Type")
if ct != "" && ct != "application/octet-stream" {
buh.Errors.Push(errcode.ErrorCodeUnknown, fmt.Errorf("Bad Content-Type"))
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, fmt.Errorf("Bad Content-Type")))
// TODO(dmcgowan): encode error
return
}
@ -173,12 +173,12 @@ func (buh *blobUploadHandler) PatchBlobData(w http.ResponseWriter, r *http.Reque
// Copy the data
if _, err := io.Copy(buh.Upload, r.Body); err != nil {
ctxu.GetLogger(buh).Errorf("unknown error copying into upload: %v", err)
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
if err := buh.blobUploadResponse(w, r, false); err != nil {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -192,7 +192,7 @@ func (buh *blobUploadHandler) PatchBlobData(w http.ResponseWriter, r *http.Reque
// url of the blob.
func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *http.Request) {
if buh.Upload == nil {
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadUnknown))
return
}
@ -200,21 +200,21 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
if dgstStr == "" {
// no digest? return error, but allow retry.
buh.Errors.Push(v2.ErrorCodeDigestInvalid, "digest missing")
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, "digest missing"))
return
}
dgst, err := digest.ParseDigest(dgstStr)
if err != nil {
// no digest? return error, but allow retry.
buh.Errors.Push(v2.ErrorCodeDigestInvalid, "digest parsing failed")
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, "digest parsing failed"))
return
}
// Read in the data, if any.
if _, err := io.Copy(buh.Upload, r.Body); err != nil {
ctxu.GetLogger(buh).Errorf("unknown error copying into upload: %v", err)
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -229,14 +229,14 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
if err != nil {
switch err := err.(type) {
case distribution.ErrBlobInvalidDigest:
buh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, err))
default:
switch err {
case distribution.ErrBlobInvalidLength, distribution.ErrBlobDigestUnsupported:
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadInvalid, err))
default:
ctxu.GetLogger(buh).Errorf("unknown error completing upload: %#v", err)
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
}
}
@ -253,7 +253,7 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
// Build our canonical blob url
blobURL, err := buh.urlBuilder.BuildBlobURL(buh.Repository.Name(), desc.Digest)
if err != nil {
buh.Errors.Push(errcode.ErrorCodeUnknown, err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
@ -266,14 +266,14 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
// CancelBlobUpload cancels an in-progress upload of a blob.
func (buh *blobUploadHandler) CancelBlobUpload(w http.ResponseWriter, r *http.Request) {
if buh.Upload == nil {
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
buh.Errors = append(buh.Errors, errcode.NewError(v2.ErrorCodeBlobUploadUnknown))
return
}
w.Header().Set("Docker-Upload-UUID", buh.UUID)
if err := buh.Upload.Cancel(buh); err != nil {
ctxu.GetLogger(buh).Errorf("error encountered canceling upload: %v", err)
buh.Errors.PushErr(err)
buh.Errors = append(buh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
}
w.WriteHeader(http.StatusNoContent)

View file

@ -14,8 +14,8 @@ func serveJSON(w http.ResponseWriter, v interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
sc := http.StatusInternalServerError
if errs, ok := v.(errcode.Errors); ok && errs.Len() > 0 {
sc = errs.Errors[0].Code.Descriptor().HTTPStatusCode
if errs, ok := v.(errcode.Errors); ok && len(errs) > 0 {
sc = errs[0].Code.Descriptor().HTTPStatusCode
if sc == 0 {
sc = http.StatusInternalServerError
}

View file

@ -10,6 +10,7 @@ import (
ctxu "github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
"github.com/gorilla/handlers"
"golang.org/x/net/context"
@ -63,7 +64,7 @@ func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http
}
if err != nil {
imh.Errors.Push(v2.ErrorCodeManifestUnknown, err)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeManifestUnknown, err))
return
}
@ -71,7 +72,7 @@ func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http
if imh.Digest == "" {
dgst, err := digestManifest(imh, sm)
if err != nil {
imh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, err))
return
}
@ -92,13 +93,13 @@ func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http
var manifest manifest.SignedManifest
if err := dec.Decode(&manifest); err != nil {
imh.Errors.Push(v2.ErrorCodeManifestInvalid, err)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeManifestInvalid, err))
return
}
dgst, err := digestManifest(imh, &manifest)
if err != nil {
imh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid, err))
return
}
@ -106,7 +107,7 @@ func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http
if imh.Tag != "" {
if manifest.Tag != imh.Tag {
ctxu.GetLogger(imh).Errorf("invalid tag on manifest payload: %q != %q", manifest.Tag, imh.Tag)
imh.Errors.Push(v2.ErrorCodeTagInvalid)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeTagInvalid))
return
}
@ -114,11 +115,11 @@ func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http
} else if imh.Digest != "" {
if dgst != imh.Digest {
ctxu.GetLogger(imh).Errorf("payload digest does match: %q != %q", dgst, imh.Digest)
imh.Errors.Push(v2.ErrorCodeDigestInvalid)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid))
return
}
} else {
imh.Errors.Push(v2.ErrorCodeTagInvalid, "no tag or digest specified")
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeTagInvalid, "no tag or digest specified"))
return
}
@ -130,19 +131,19 @@ func (imh *imageManifestHandler) PutImageManifest(w http.ResponseWriter, r *http
for _, verificationError := range err {
switch verificationError := verificationError.(type) {
case distribution.ErrManifestBlobUnknown:
imh.Errors.Push(v2.ErrorCodeBlobUnknown, verificationError.Digest)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeBlobUnknown, verificationError.Digest))
case distribution.ErrManifestUnverified:
imh.Errors.Push(v2.ErrorCodeManifestUnverified)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeManifestUnverified))
default:
if verificationError == digest.ErrDigestInvalidFormat {
imh.Errors.Push(v2.ErrorCodeDigestInvalid)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeDigestInvalid))
} else {
imh.Errors.PushErr(verificationError)
imh.Errors = append(imh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, verificationError))
}
}
}
default:
imh.Errors.PushErr(err)
imh.Errors = append(imh.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
}
return
@ -171,7 +172,7 @@ func (imh *imageManifestHandler) DeleteImageManifest(w http.ResponseWriter, r *h
// tag index entries a serious problem in eventually consistent storage.
// Once we work out schema version 2, the full deletion system will be
// worked out and we can add support back.
imh.Errors.Push(v2.ErrorCodeUnsupported)
imh.Errors = append(imh.Errors, errcode.NewError(v2.ErrorCodeUnsupported))
}
// digestManifest takes a digest of the given manifest. This belongs somewhere

View file

@ -5,6 +5,7 @@ import (
"net/http"
"github.com/docker/distribution"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
"github.com/gorilla/handlers"
)
@ -39,9 +40,9 @@ func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) {
if err != nil {
switch err := err.(type) {
case distribution.ErrRepositoryUnknown:
th.Errors.Push(v2.ErrorCodeNameUnknown, map[string]string{"name": th.Repository.Name()})
th.Errors = append(th.Errors, errcode.NewError(v2.ErrorCodeNameUnknown, map[string]string{"name": th.Repository.Name()}))
default:
th.Errors.PushErr(err)
th.Errors = append(th.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
}
return
}
@ -53,7 +54,7 @@ func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) {
Name: th.Repository.Name(),
Tags: tags,
}); err != nil {
th.Errors.PushErr(err)
th.Errors = append(th.Errors, errcode.NewError(errcode.ErrorCodeUnknown, err))
return
}
}