handler non-200 status code for standardAuthHandler

This commit is contained in:
Wenkai Yin 2016-04-15 17:01:59 +08:00
parent 39beda438c
commit 0cd7175a6b
4 changed files with 50 additions and 10 deletions

View File

@ -29,6 +29,7 @@ import (
"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry"
"github.com/vmware/harbor/utils/registry/auth"
"github.com/vmware/harbor/utils/registry/errors"
)
// RepositoryAPI handles request to /api/repositories /api/repositories/tags /api/repositories/manifests, the parm has to be put
@ -163,7 +164,7 @@ func (ra *RepositoryAPI) Delete() {
if len(tag) == 0 {
tagList, err := ra.registry.ListTag(repoName)
if err != nil {
e, ok := registry.ParseError(err)
e, ok := errors.ParseError(err)
if ok {
log.Info(e)
ra.CustomAbort(e.StatusCode, e.Message)
@ -181,7 +182,7 @@ func (ra *RepositoryAPI) Delete() {
for _, t := range tags {
if err := ra.registry.DeleteTag(repoName, t); err != nil {
e, ok := registry.ParseError(err)
e, ok := errors.ParseError(err)
if ok {
ra.CustomAbort(e.StatusCode, e.Message)
} else {

View File

@ -26,7 +26,7 @@ import (
token_util "github.com/vmware/harbor/service/token"
"github.com/vmware/harbor/utils/log"
"github.com/vmware/harbor/utils/registry/errors"
registry_errors "github.com/vmware/harbor/utils/registry/errors"
)
// Handler authorizes the request when encounters a 401 error
@ -119,7 +119,7 @@ func (t *standardTokenHandler) AuthorizeRequest(req *http.Request, params map[st
}
if resp.StatusCode != http.StatusOK {
return error.Error{
return registry_errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}

View File

@ -0,0 +1,38 @@
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package errors
import (
"fmt"
)
// Error : if response's status code is not 200 or does not meet requirement,
// an Error instance will be returned
type Error struct {
StatusCode int
Message string
}
// Error ...
func (e Error) Error() string {
return fmt.Sprintf("%d %s", e.StatusCode, e.Message)
}
// ParseError parses err, if err is type Error, convert it to Error
func ParseError(err error) (Error, bool) {
e, ok := err.(Error)
return e, ok
}

View File

@ -23,6 +23,7 @@ import (
"net/url"
"github.com/docker/distribution/manifest/schema2"
"github.com/vmware/harbor/utils/registry/errors"
)
// Registry holds information of a registry entiry
@ -86,7 +87,7 @@ func (r *Registry) ListTag(name string) ([]string, error) {
return tags, nil
}
return tags, Error{
return tags, errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}
@ -126,7 +127,7 @@ func (r *Registry) ManifestExist(name, reference string) (digest string, exist b
return
}
err = Error{
err = errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}
@ -162,7 +163,7 @@ func (r *Registry) PullManifest(name, reference string) (digest, mediaType strin
return
}
err = Error{
err = errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}
@ -193,7 +194,7 @@ func (r *Registry) DeleteManifest(name, digest string) error {
return err
}
return Error{
return errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}
@ -207,7 +208,7 @@ func (r *Registry) DeleteTag(name, tag string) error {
}
if !exist {
return Error{
return errors.Error{
StatusCode: http.StatusNotFound,
}
}
@ -238,7 +239,7 @@ func (r *Registry) DeleteBlob(name, digest string) error {
return err
}
return Error{
return errors.Error{
StatusCode: resp.StatusCode,
Message: string(b),
}