Skip to content

Commit

Permalink
chore: add reference details to registry errors (#1152)
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <sajaya@microsoft.com>
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
Co-authored-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
sajayantony and shizhMSFT authored Oct 26, 2023
1 parent f2ed16a commit 264df60
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 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
2 changes: 1 addition & 1 deletion test/e2e/suite/command/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("ORAS beginners:", func() {
})

It("should fail when no tag or digest found in provided subject reference", func() {
ORAS("discover", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "invalid image reference").Exec()
ORAS("discover", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "no tag or digest").Exec()
})
})
})
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/suite/command/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ var _ = Describe("1.1 registry users:", func() {
})

It("should fail if no manifest tag or digest is provided", func() {
ORAS("manifest", "fetch", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "invalid image reference").Exec()
ORAS("manifest", "fetch", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "no tag or digest").Exec()
})
})

Expand Down Expand Up @@ -331,7 +331,7 @@ var _ = Describe("1.1 registry users:", func() {
MatchContent(multi_arch.LinuxAMD64ConfigDesc).Exec()
})
It("should fail if no manifest tag or digest is provided", func() {
ORAS("manifest", "fetch-config", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "invalid image reference").Exec()
ORAS("manifest", "fetch-config", RegistryRef(ZOTHost, ImageRepo, "")).ExpectFailure().MatchErrKeyWords("Error:", "no tag or digest").Exec()
})
})

Expand Down Expand Up @@ -446,7 +446,7 @@ var _ = Describe("OCI image layout users:", func() {
It("should fail if no manifest tag or digest is provided", func() {
root := PrepareTempOCI(ImageRepo)
ORAS("manifest", "fetch", Flags.Layout, root).ExpectFailure().
MatchErrKeyWords("Error:", "invalid image reference").Exec()
MatchErrKeyWords("Error:", "no tag or digest").Exec()
})
})

Expand Down Expand Up @@ -491,7 +491,7 @@ var _ = Describe("OCI image layout users:", func() {
})
It("should fail if no manifest tag or digest is provided", func() {
root := prepare(foobar.Tag)
ORAS("manifest", "fetch-config", Flags.Layout, root).ExpectFailure().MatchErrKeyWords("Error:", "invalid image reference").Exec()
ORAS("manifest", "fetch-config", Flags.Layout, root).ExpectFailure().MatchErrKeyWords("Error:", "no tag or digest").Exec()
})
})
})
Expand Down

0 comments on commit 264df60

Please sign in to comment.