Skip to content

Commit

Permalink
fix: update error in SignedEntity to be more descriptive (#3233)
Browse files Browse the repository at this point in the history
* fix: add func to generate `entity not found` error

Signed-off-by: Vishal Choudhary <[email protected]>

* fix: update format specifier in fmt.Errorf

Signed-off-by: Vishal Choudhary <[email protected]>

* feat: make ErrEntityNotFound error message less common

Signed-off-by: Vishal Choudhary <[email protected]>

* feat: make EntityNotFoundError a first-class error

Signed-off-by: Vishal Choudhary <[email protected]>

---------

Signed-off-by: Vishal Choudhary <[email protected]>
  • Loading branch information
vishal-chdhry authored Sep 10, 2023
1 parent b09f72d commit 86252aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 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 err == ociremote.ErrEntityNotFound {
if _, isEntityNotFoundErr := err.(*ociremote.EntityNotFoundError); isEntityNotFoundErr {
se = ociremote.SignedUnknown(digest)
} else if err != nil {
return fmt.Errorf("accessing image: %w", err)
Expand Down
22 changes: 17 additions & 5 deletions pkg/oci/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,24 @@ 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 = errors.New("entity not found in registry")
)

// 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 NewEntityNotFoundError(err error) error {
return &EntityNotFoundError{
baseErr: err,
}
}

// SignedEntity provides access to a remote reference, and its signatures.
// The SignedEntity will be one of SignedImage or SignedImageIndex.
func SignedEntity(ref name.Reference, options ...Option) (oci.SignedEntity, error) {
Expand All @@ -50,7 +62,7 @@ func SignedEntity(ref name.Reference, options ...Option) (oci.SignedEntity, erro
got, err := remoteGet(ref, o.ROpt...)
var te *transport.Error
if errors.As(err, &te) && te.StatusCode == http.StatusNotFound {
return nil, ErrEntityNotFound
return nil, NewEntityNotFoundError(err)
} else if err != nil {
return nil, err
}
Expand Down

0 comments on commit 86252aa

Please sign in to comment.