mirror of
https://github.com/moby/moby.git
synced 2026-08-03 22:51:03 +00:00
Remove string checking in API error handling
Use strongly typed errors to set HTTP status codes. Error interfaces are defined in the api/errors package and errors returned from controllers are checked against these interfaces. Errors can be wraeped in a pkg/errors.Causer, as long as somewhere in the line of causes one of the interfaces is implemented. The special error interfaces take precedence over Causer, meaning if both Causer and one of the new error interfaces are implemented, the Causer is not traversed. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
25
reference/errors.go
Normal file
25
reference/errors.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package reference
|
||||
|
||||
type notFoundError string
|
||||
|
||||
func (e notFoundError) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
func (notFoundError) NotFound() {}
|
||||
|
||||
type invalidTagError string
|
||||
|
||||
func (e invalidTagError) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
func (invalidTagError) InvalidParameter() {}
|
||||
|
||||
type conflictingTagError string
|
||||
|
||||
func (e conflictingTagError) Error() string {
|
||||
return string(e)
|
||||
}
|
||||
|
||||
func (conflictingTagError) Conflict() {}
|
||||
@@ -2,7 +2,6 @@ package reference
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -12,12 +11,13 @@ import (
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/docker/docker/pkg/ioutils"
|
||||
"github.com/opencontainers/go-digest"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrDoesNotExist is returned if a reference is not found in the
|
||||
// store.
|
||||
ErrDoesNotExist = errors.New("reference does not exist")
|
||||
ErrDoesNotExist notFoundError = "reference does not exist"
|
||||
)
|
||||
|
||||
// An Association is a tuple associating a reference with an image ID.
|
||||
@@ -100,7 +100,7 @@ func NewReferenceStore(jsonPath, platform string) (Store, error) {
|
||||
// references can be overwritten. This only works for tags, not digests.
|
||||
func (store *store) AddTag(ref reference.Named, id digest.Digest, force bool) error {
|
||||
if _, isCanonical := ref.(reference.Canonical); isCanonical {
|
||||
return errors.New("refusing to create a tag with a digest reference")
|
||||
return errors.WithStack(invalidTagError("refusing to create a tag with a digest reference"))
|
||||
}
|
||||
return store.addReference(reference.TagNameOnly(ref), id, force)
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (store *store) addReference(ref reference.Named, id digest.Digest, force bo
|
||||
refStr := reference.FamiliarString(ref)
|
||||
|
||||
if refName == string(digest.Canonical) {
|
||||
return errors.New("refusing to create an ambiguous tag using digest algorithm as name")
|
||||
return errors.WithStack(invalidTagError("refusing to create an ambiguous tag using digest algorithm as name"))
|
||||
}
|
||||
|
||||
store.mu.Lock()
|
||||
@@ -155,11 +155,15 @@ func (store *store) addReference(ref reference.Named, id digest.Digest, force bo
|
||||
if exists {
|
||||
// force only works for tags
|
||||
if digested, isDigest := ref.(reference.Canonical); isDigest {
|
||||
return fmt.Errorf("Cannot overwrite digest %s", digested.Digest().String())
|
||||
return errors.WithStack(conflictingTagError("Cannot overwrite digest " + digested.Digest().String()))
|
||||
}
|
||||
|
||||
if !force {
|
||||
return fmt.Errorf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option", refStr, oldID.String())
|
||||
return errors.WithStack(
|
||||
conflictingTagError(
|
||||
fmt.Sprintf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use the force option", refStr, oldID.String()),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
if store.referencesByIDCache[oldID] != nil {
|
||||
|
||||
Reference in New Issue
Block a user