Move ErrorCode logic to new errcode package

Make HTTP status codes match the ErrorCode by looking it up in the Descriptors

Signed-off-by: Doug Davis <dug@us.ibm.com>
This commit is contained in:
Doug Davis 2015-05-14 18:21:39 -07:00
parent 5f553b3cfc
commit f565d6abb7
16 changed files with 444 additions and 405 deletions

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/docker/distribution"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
"github.com/docker/distribution/testutil"
)
@ -161,14 +162,14 @@ func TestUploadReadFrom(t *testing.T) {
if err == nil {
t.Fatalf("Expected error when not found")
}
if uploadErr, ok := err.(*v2.Errors); !ok {
if uploadErr, ok := err.(*errcode.Errors); !ok {
t.Fatalf("Wrong error type %T: %s", err, err)
} else if len(uploadErr.Errors) != 1 {
t.Fatalf("Unexpected number of errors: %d, expected 1", len(uploadErr.Errors))
} else {
v2Err := uploadErr.Errors[0]
if v2Err.Code != v2.ErrorCodeBlobUploadInvalid {
t.Fatalf("Unexpected error code: %s, expected %s", v2Err.Code.String(), v2.ErrorCodeBlobUploadInvalid.String())
t.Fatalf("Unexpected error code: %s, expected %d", v2Err.Code.String(), v2.ErrorCodeBlobUploadInvalid)
}
if expected := "invalid upload identifier"; v2Err.Message != expected {
t.Fatalf("Unexpected error message: %s, expected %s", v2Err.Message, expected)

View file

@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"github.com/docker/distribution/registry/api/errcode"
"github.com/docker/distribution/registry/api/v2"
)
@ -32,7 +33,7 @@ func (e *UnexpectedHTTPResponseError) Error() string {
}
func parseHTTPErrorResponse(r io.Reader) error {
var errors v2.Errors
var errors errcode.Errors
body, err := ioutil.ReadAll(r)
if err != nil {
return err
@ -51,7 +52,7 @@ func handleErrorResponse(resp *http.Response) error {
if resp.StatusCode == 401 {
err := parseHTTPErrorResponse(resp.Body)
if uErr, ok := err.(*UnexpectedHTTPResponseError); ok {
return &v2.Error{
return &errcode.Error{
Code: v2.ErrorCodeUnauthorized,
Message: "401 Unauthorized",
Detail: uErr.Response,

View file

@ -18,6 +18,7 @@ import (
"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/docker/distribution/testutil"
)
@ -668,7 +669,7 @@ func TestManifestUnauthorized(t *testing.T) {
if err == nil {
t.Fatal("Expected error fetching manifest")
}
v2Err, ok := err.(*v2.Error)
v2Err, ok := err.(*errcode.Error)
if !ok {
t.Fatalf("Unexpected error type: %#v", err)
}