Merge pull request #548 from duglin/MoveErrors
Move ErrorCode logic to new errcode package
This commit is contained in:
commit
728ca4b391
18 changed files with 746 additions and 652 deletions
|
@ -19,6 +19,7 @@ import (
|
|||
"github.com/docker/distribution/configuration"
|
||||
"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/docker/distribution/registry/storage/driver/inmemory"
|
||||
"github.com/docker/distribution/testutil"
|
||||
|
@ -373,7 +374,7 @@ func TestManifestAPI(t *testing.T) {
|
|||
_, p, counts := checkBodyHasErrorCodes(t, "getting unknown manifest tags", resp,
|
||||
v2.ErrorCodeManifestUnverified, v2.ErrorCodeBlobUnknown, v2.ErrorCodeDigestInvalid)
|
||||
|
||||
expectedCounts := map[v2.ErrorCode]int{
|
||||
expectedCounts := map[errcode.ErrorCode]int{
|
||||
v2.ErrorCodeManifestUnverified: 1,
|
||||
v2.ErrorCodeBlobUnknown: 2,
|
||||
v2.ErrorCodeDigestInvalid: 2,
|
||||
|
@ -748,18 +749,18 @@ func checkResponse(t *testing.T, msg string, resp *http.Response, expectedStatus
|
|||
// checkBodyHasErrorCodes ensures the body is an error body and has the
|
||||
// expected error codes, returning the error structure, the json slice and a
|
||||
// count of the errors by code.
|
||||
func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, errorCodes ...v2.ErrorCode) (v2.Errors, []byte, map[v2.ErrorCode]int) {
|
||||
func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, errorCodes ...errcode.ErrorCode) (errcode.Errors, []byte, map[errcode.ErrorCode]int) {
|
||||
p, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error reading body %s: %v", msg, err)
|
||||
}
|
||||
|
||||
var errs v2.Errors
|
||||
var errs errcode.Errors
|
||||
if err := json.Unmarshal(p, &errs); err != nil {
|
||||
t.Fatalf("unexpected error decoding error response: %v", err)
|
||||
}
|
||||
|
||||
if len(errs.Errors) == 0 {
|
||||
if len(errs) == 0 {
|
||||
t.Fatalf("expected errors in response")
|
||||
}
|
||||
|
||||
|
@ -770,8 +771,8 @@ func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, error
|
|||
// resp.Header.Get("Content-Type"))
|
||||
// }
|
||||
|
||||
expected := map[v2.ErrorCode]struct{}{}
|
||||
counts := map[v2.ErrorCode]int{}
|
||||
expected := map[errcode.ErrorCode]struct{}{}
|
||||
counts := map[errcode.ErrorCode]int{}
|
||||
|
||||
// Initialize map with zeros for expected
|
||||
for _, code := range errorCodes {
|
||||
|
@ -779,11 +780,15 @@ func checkBodyHasErrorCodes(t *testing.T, msg string, resp *http.Response, error
|
|||
counts[code] = 0
|
||||
}
|
||||
|
||||
for _, err := range errs.Errors {
|
||||
if _, ok := expected[err.Code]; !ok {
|
||||
t.Fatalf("unexpected error code %v encountered during %s: %s ", err.Code, msg, string(p))
|
||||
for _, e := range errs {
|
||||
err, ok := e.(errcode.ErrorCoder)
|
||||
if !ok {
|
||||
t.Fatalf("not an ErrorCoder: %#v", e)
|
||||
}
|
||||
counts[err.Code]++
|
||||
if _, ok := expected[err.ErrorCode()]; !ok {
|
||||
t.Fatalf("unexpected error code %v encountered during %s: %s ", err.ErrorCode(), msg, string(p))
|
||||
}
|
||||
counts[err.ErrorCode()]++
|
||||
}
|
||||
|
||||
// Ensure that counts of expected errors were all non-zero
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/docker/distribution/configuration"
|
||||
ctxu "github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/notifications"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/distribution/registry/api/v2"
|
||||
"github.com/docker/distribution/registry/auth"
|
||||
registrymiddleware "github.com/docker/distribution/registry/middleware/registry"
|
||||
|
@ -372,12 +373,11 @@ 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, v2.ErrorCodeNameUnknown.WithDetail(err))
|
||||
case distribution.ErrRepositoryNameInvalid:
|
||||
context.Errors.Push(v2.ErrorCodeNameInvalid, err)
|
||||
context.Errors = append(context.Errors, v2.ErrorCodeNameInvalid.WithDetail(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
serveJSON(w, context.Errors)
|
||||
return
|
||||
}
|
||||
|
@ -390,8 +390,8 @@ 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(v2.ErrorCodeUnknown, err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
context.Errors = append(context.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
|
||||
serveJSON(w, context.Errors)
|
||||
return
|
||||
}
|
||||
|
@ -402,23 +402,33 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler {
|
|||
// own errors if they need different behavior (such as range errors
|
||||
// for layer upload).
|
||||
if context.Errors.Len() > 0 {
|
||||
if context.Value("http.response.status") == 0 {
|
||||
// TODO(stevvooe): Getting this value from the context is a
|
||||
// bit of a hack. We can further address with some of our
|
||||
// future refactoring.
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
app.logError(context, context.Errors)
|
||||
|
||||
serveJSON(w, context.Errors)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (app *App) logError(context context.Context, errors v2.Errors) {
|
||||
for _, e := range errors.Errors {
|
||||
c := ctxu.WithValue(context, "err.code", e.Code)
|
||||
c = ctxu.WithValue(c, "err.message", e.Message)
|
||||
c = ctxu.WithValue(c, "err.detail", e.Detail)
|
||||
func (app *App) logError(context context.Context, errors errcode.Errors) {
|
||||
for _, e1 := range errors {
|
||||
var c ctxu.Context
|
||||
|
||||
switch e1.(type) {
|
||||
case errcode.Error:
|
||||
e, _ := e1.(errcode.Error)
|
||||
c = ctxu.WithValue(context, "err.code", e.Code)
|
||||
c = ctxu.WithValue(c, "err.message", e.Code.Message())
|
||||
c = ctxu.WithValue(c, "err.detail", e.Detail)
|
||||
case errcode.ErrorCode:
|
||||
e, _ := e1.(errcode.ErrorCode)
|
||||
c = ctxu.WithValue(context, "err.code", e)
|
||||
c = ctxu.WithValue(c, "err.message", e.Message())
|
||||
default:
|
||||
// just normal go 'error'
|
||||
c = ctxu.WithValue(context, "err.code", errcode.ErrorCodeUnknown)
|
||||
c = ctxu.WithValue(c, "err.message", e1.Error())
|
||||
}
|
||||
|
||||
c = ctxu.WithLogger(c, ctxu.GetLogger(c,
|
||||
"err.code",
|
||||
"err.message",
|
||||
|
@ -471,11 +481,10 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont
|
|||
// base route is accessed. This section prevents us from making
|
||||
// that mistake elsewhere in the code, allowing any operation to
|
||||
// proceed.
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
|
||||
var errs v2.Errors
|
||||
errs.Push(v2.ErrorCodeUnauthorized)
|
||||
var errs errcode.Errors
|
||||
errs = append(errs, v2.ErrorCodeUnauthorized)
|
||||
|
||||
serveJSON(w, errs)
|
||||
return fmt.Errorf("forbidden: no repository name")
|
||||
}
|
||||
|
@ -485,11 +494,20 @@ func (app *App) authorized(w http.ResponseWriter, r *http.Request, context *Cont
|
|||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case auth.Challenge:
|
||||
// NOTE(duglin):
|
||||
// Since err.ServeHTTP will set the HTTP status code for us
|
||||
// we need to set the content-type here. The serveJSON
|
||||
// func will try to do it but it'll be too late at that point.
|
||||
// I would have have preferred to just have the auth.Challenge
|
||||
// ServerHTTP func just add the WWW-Authenticate header and let
|
||||
// serveJSON set the HTTP status code and content-type but I wasn't
|
||||
// sure if that's an ok design change. STEVVOOE ?
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
err.ServeHTTP(w, r)
|
||||
|
||||
var errs v2.Errors
|
||||
errs.Push(v2.ErrorCodeUnauthorized, accessRecords)
|
||||
var errs errcode.Errors
|
||||
errs = append(errs, v2.ErrorCodeUnauthorized.WithDetail(accessRecords))
|
||||
serveJSON(w, errs)
|
||||
default:
|
||||
// This condition is a potential security problem either in
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/docker/distribution/configuration"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/distribution/registry/api/v2"
|
||||
"github.com/docker/distribution/registry/auth"
|
||||
_ "github.com/docker/distribution/registry/auth/silly"
|
||||
|
@ -194,14 +195,18 @@ func TestNewApp(t *testing.T) {
|
|||
t.Fatalf("unexpected WWW-Authenticate header: %q != %q", e, a)
|
||||
}
|
||||
|
||||
var errs v2.Errors
|
||||
var errs errcode.Errors
|
||||
dec := json.NewDecoder(req.Body)
|
||||
if err := dec.Decode(&errs); err != nil {
|
||||
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)
|
||||
err2, ok := errs[0].(errcode.ErrorCoder)
|
||||
if !ok {
|
||||
t.Fatalf("not an ErrorCoder: %#v", errs[0])
|
||||
}
|
||||
if err2.ErrorCode() != v2.ErrorCodeUnauthorized {
|
||||
t.Fatalf("unexpected error code: %v != %v", err2.ErrorCode(), v2.ErrorCodeUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/docker/distribution"
|
||||
"github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/digest"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/distribution/registry/api/v2"
|
||||
"github.com/gorilla/handlers"
|
||||
)
|
||||
|
@ -17,13 +18,12 @@ func blobDispatcher(ctx *Context, r *http.Request) http.Handler {
|
|||
|
||||
if err == errDigestNotAvailable {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
||||
ctx.Errors = append(ctx.Errors, v2.ErrorCodeDigestInvalid.WithDetail(err))
|
||||
})
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
||||
ctx.Errors = append(ctx.Errors, v2.ErrorCodeDigestInvalid.WithDetail(err))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -53,17 +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 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
bh.Errors.Push(v2.ErrorCodeBlobUnknown, bh.Digest)
|
||||
bh.Errors = append(bh.Errors, v2.ErrorCodeBlobUnknown.WithDetail(bh.Digest))
|
||||
} else {
|
||||
bh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
bh.Errors = append(bh.Errors, errcode.ErrorCodeUnknown.WithDetail(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(v2.ErrorCodeUnknown, err)
|
||||
bh.Errors = append(bh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/docker/distribution"
|
||||
ctxu "github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/digest"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/distribution/registry/api/v2"
|
||||
"github.com/gorilla/handlers"
|
||||
)
|
||||
|
@ -36,8 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(err))
|
||||
})
|
||||
}
|
||||
buh.State = state
|
||||
|
@ -45,16 +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())
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(err))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -64,14 +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) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadUnknown.WithDetail(err))
|
||||
})
|
||||
}
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
})
|
||||
}
|
||||
buh.Upload = upload
|
||||
|
@ -85,16 +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) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(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) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(err))
|
||||
upload.Cancel(buh)
|
||||
})
|
||||
}
|
||||
|
@ -125,8 +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 {
|
||||
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -134,8 +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 {
|
||||
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -146,8 +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 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadUnknown)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -155,8 +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 {
|
||||
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -167,14 +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 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadUnknown)
|
||||
return
|
||||
}
|
||||
|
||||
ct := r.Header.Get("Content-Type")
|
||||
if ct != "" && ct != "application/octet-stream" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(fmt.Errorf("Bad Content-Type")))
|
||||
// TODO(dmcgowan): encode error
|
||||
return
|
||||
}
|
||||
|
@ -184,14 +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)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
if err := buh.blobUploadResponse(w, r, false); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError) // Error conditions here?
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -205,8 +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 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadUnknown)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -214,24 +200,21 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
|
|||
|
||||
if dgstStr == "" {
|
||||
// no digest? return error, but allow retry.
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeDigestInvalid, "digest missing")
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeDigestInvalid.WithDetail("digest missing"))
|
||||
return
|
||||
}
|
||||
|
||||
dgst, err := digest.ParseDigest(dgstStr)
|
||||
if err != nil {
|
||||
// no digest? return error, but allow retry.
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeDigestInvalid, "digest parsing failed")
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeDigestInvalid.WithDetail("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)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -246,17 +229,14 @@ func (buh *blobUploadHandler) PutBlobUploadComplete(w http.ResponseWriter, r *ht
|
|||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case distribution.ErrBlobInvalidDigest:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeDigestInvalid.WithDetail(err))
|
||||
default:
|
||||
switch err {
|
||||
case distribution.ErrBlobInvalidLength, distribution.ErrBlobDigestUnsupported:
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadInvalid, err)
|
||||
buh.Errors = append(buh.Errors, v2.ErrorCodeBlobUploadInvalid.WithDetail(err))
|
||||
default:
|
||||
ctxu.GetLogger(buh).Errorf("unknown error completing upload: %#v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors.Push(v2.ErrorCodeUnknown, err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -273,8 +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(v2.ErrorCodeUnknown, err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -287,16 +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 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
buh.Errors.Push(v2.ErrorCodeBlobUploadUnknown)
|
||||
buh.Errors = append(buh.Errors, 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)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
buh.Errors.PushErr(err)
|
||||
buh.Errors = append(buh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/docker/distribution"
|
||||
ctxu "github.com/docker/distribution/context"
|
||||
"github.com/docker/distribution/digest"
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
"github.com/docker/distribution/registry/api/v2"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
@ -27,7 +28,7 @@ type Context struct {
|
|||
// Errors is a collection of errors encountered during the request to be
|
||||
// returned to the client API. If errors are added to the collection, the
|
||||
// handler *must not* start the response via http.ResponseWriter.
|
||||
Errors v2.Errors
|
||||
Errors errcode.Errors
|
||||
|
||||
urlBuilder *v2.URLBuilder
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/docker/distribution/registry/api/errcode"
|
||||
)
|
||||
|
||||
// serveJSON marshals v and sets the content-type header to
|
||||
|
@ -11,6 +13,22 @@ import (
|
|||
// ResponseWriter.WriteHeader before this function.
|
||||
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 && len(errs) > 0 {
|
||||
if err, ok := errs[0].(errcode.ErrorCoder); ok {
|
||||
if sc2 := err.ErrorCode().Descriptor().HTTPStatusCode; sc2 != 0 {
|
||||
sc = sc2
|
||||
}
|
||||
}
|
||||
} else if err, ok := v.(errcode.ErrorCoder); ok {
|
||||
if sc2 := err.ErrorCode().Descriptor().HTTPStatusCode; sc2 != 0 {
|
||||
sc = sc2
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(sc)
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
|
||||
if err := enc.Encode(v); err != nil {
|
||||
|
|
|
@ -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,8 +64,7 @@ func (imh *imageManifestHandler) GetImageManifest(w http.ResponseWriter, r *http
|
|||
}
|
||||
|
||||
if err != nil {
|
||||
imh.Errors.Push(v2.ErrorCodeManifestUnknown, err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -72,8 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -94,15 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestInvalid.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
dgst, err := digestManifest(imh, &manifest)
|
||||
if err != nil {
|
||||
imh.Errors.Push(v2.ErrorCodeDigestInvalid, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid.WithDetail(err))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -110,8 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeTagInvalid)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -119,13 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
imh.Errors.Push(v2.ErrorCodeTagInvalid, "no tag or digest specified")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeTagInvalid.WithDetail("no tag or digest specified"))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -137,22 +131,21 @@ 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, v2.ErrorCodeBlobUnknown.WithDetail(verificationError.Digest))
|
||||
case distribution.ErrManifestUnverified:
|
||||
imh.Errors.Push(v2.ErrorCodeManifestUnverified)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeManifestUnverified)
|
||||
default:
|
||||
if verificationError == digest.ErrDigestInvalidFormat {
|
||||
imh.Errors.Push(v2.ErrorCodeDigestInvalid)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeDigestInvalid)
|
||||
} else {
|
||||
imh.Errors.PushErr(verificationError)
|
||||
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown, verificationError)
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
imh.Errors.PushErr(err)
|
||||
imh.Errors = append(imh.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -179,8 +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)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
imh.Errors = append(imh.Errors, v2.ErrorCodeUnsupported)
|
||||
}
|
||||
|
||||
// digestManifest takes a digest of the given manifest. This belongs somewhere
|
||||
|
|
|
@ -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,10 +40,9 @@ func (th *tagsHandler) GetTags(w http.ResponseWriter, r *http.Request) {
|
|||
if err != nil {
|
||||
switch err := err.(type) {
|
||||
case distribution.ErrRepositoryUnknown:
|
||||
w.WriteHeader(404)
|
||||
th.Errors.Push(v2.ErrorCodeNameUnknown, map[string]string{"name": th.Repository.Name()})
|
||||
th.Errors = append(th.Errors, v2.ErrorCodeNameUnknown.WithDetail(map[string]string{"name": th.Repository.Name()}))
|
||||
default:
|
||||
th.Errors.PushErr(err)
|
||||
th.Errors = append(th.Errors, errcode.ErrorCodeUnknown.WithDetail(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -54,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.ErrorCodeUnknown.WithDetail(err))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue