Skip to content

Commit

Permalink
feat: make EntityNotFoundError a first-class error
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Choudhary <[email protected]>
  • Loading branch information
vishal-chdhry committed Sep 10, 2023
1 parent 3a9098e commit 207124c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/cosign/cli/sign/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func SignCmd(ro *options.RootOptions, ko options.KeyOpts, signOpts options.SignO

if digest, ok := ref.(name.Digest); ok && !signOpts.Recursive {
se, err := ociremote.SignedEntity(ref, opts...)
if ociremote.IsEntityNotFoundError(err) {
if _, isEntityNotFoundErr := err.(*ociremote.EntityNotFoundError); isEntityNotFoundErr {
se = ociremote.SignedUnknown(digest)
} else if err != nil {
return fmt.Errorf("accessing image: %w", err)
Expand Down
21 changes: 12 additions & 9 deletions pkg/oci/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand All @@ -37,18 +36,22 @@ var (
remoteIndex = remote.Index
remoteGet = remote.Get
remoteWrite = remote.Write

// ErrEntityNotFound is the error that SignedEntity returns when the
// provided ref does not exist.
ErrEntityNotFound = "cosign remoteGet: entity not found in registry"
)

func NewEntityNotFoundError(err error) error {
return fmt.Errorf("%s error: %w", ErrEntityNotFound, err)
// EntityNotFoundError is the error that SignedEntity returns when the
// provided ref does not exist.
type EntityNotFoundError struct {
baseErr error
}

func (e *EntityNotFoundError) Error() string {
return fmt.Sprintf("entity not found in registry, error: %v", e.baseErr)
}

func IsEntityNotFoundError(err error) bool {
return strings.Contains(err.Error(), ErrEntityNotFound)
func NewEntityNotFoundError(err error) error {
return &EntityNotFoundError{
baseErr: err,
}
}

// SignedEntity provides access to a remote reference, and its signatures.
Expand Down

0 comments on commit 207124c

Please sign in to comment.