Skip to content

Commit

Permalink
Add reference details to errors
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <[email protected]>
  • Loading branch information
sajayantony committed Oct 25, 2023
1 parent ace4255 commit 4658df5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
12 changes: 6 additions & 6 deletions cmd/oras/internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"oras.land/oras-go/v2/registry"
)

// NewErrInvalidReference creates a new error based on the reference string.
func NewErrInvalidReference(ref registry.Reference) error {
return NewErrInvalidReferenceStr(ref.String())
// NewErrEmptyTagOrDigest creates a new error based on the reference string.
func NewErrEmptyTagOrDigest(ref registry.Reference) error {
return NewErrEmptyTagOrDigestStr(ref.String())
}

// NewErrInvalidReferenceStr creates a new error based on the reference string.
func NewErrInvalidReferenceStr(ref string) error {
return fmt.Errorf("%s: invalid image reference, expecting <name:tag|name@digest>", ref)
// NewErrEmptyTagOrDigestStr creates a new error based on the reference string.
func NewErrEmptyTagOrDigestStr(ref string) error {
return fmt.Errorf("%q: no tag or digest when expecting <name:tag|name@digest>", ref)
}
5 changes: 5 additions & 0 deletions cmd/oras/internal/option/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package option
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
Expand All @@ -30,6 +31,7 @@ import (
credentials "github.com/oras-project/oras-credentials-go"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras-go/v2/registry/remote/retry"
Expand Down Expand Up @@ -290,6 +292,9 @@ func (opts *Remote) NewRegistry(registry string, common Common, logger logrus.Fi
func (opts *Remote) NewRepository(reference string, common Common, logger logrus.FieldLogger) (repo *remote.Repository, err error) {
repo, err = remote.NewRepository(reference)
if err != nil {
if errors.Unwrap(err) == errdef.ErrInvalidReference {
return nil, fmt.Errorf("%q: %v", reference, err)
}
return nil, err
}
registry := repo.Reference.Registry
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/internal/option/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (opts *Target) NewReadonlyTarget(ctx context.Context, common Common, logger
// EnsureReferenceNotEmpty ensures whether the tag or digest is empty.
func (opts *Target) EnsureReferenceNotEmpty() error {
if opts.Reference == "" {
return oerrors.NewErrInvalidReferenceStr(opts.RawReference)
return oerrors.NewErrEmptyTagOrDigestStr(opts.RawReference)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/manifest/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func deleteManifest(ctx context.Context, opts deleteOptions) error {
}

if repo.Reference.Reference == "" {
return oerrors.NewErrInvalidReference(repo.Reference)
return oerrors.NewErrEmptyTagOrDigest(repo.Reference)
}

// add both pull and delete scope hints for dst repository to save potential delete-scope token requests during deleting
Expand Down

0 comments on commit 4658df5

Please sign in to comment.