Skip to content

Commit

Permalink
cmd/utils, pkg/utils: Improve an error message for the image option
Browse files Browse the repository at this point in the history
  • Loading branch information
debarshiray committed Sep 2, 2022
1 parent 8454b31 commit f23a161
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ func createErrorInvalidImageForContainerName(container string) error {
return errors.New(errMsg)
}

func createErrorInvalidImageWithoutBasename() error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--image'\n")
fmt.Fprintf(&builder, "Images must have basenames.\n")
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase)

errMsg := builder.String()
return errors.New(errMsg)
}

func createErrorInvalidRelease(hint string) error {
var builder strings.Builder
fmt.Fprintf(&builder, "invalid argument for '--release'\n")
Expand Down Expand Up @@ -143,6 +153,7 @@ func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI,
if err != nil {
var errContainer *utils.ContainerError
var errDistro *utils.DistroError
var errImage *utils.ImageError
var errParseRelease *utils.ParseReleaseError

if errors.As(err, &errContainer) {
Expand Down Expand Up @@ -172,6 +183,14 @@ func resolveContainerAndImageNames(container, containerArg, distroCLI, imageCLI,
panicMsg := fmt.Sprintf("unexpected %T: %s", err, err)
panic(panicMsg)
}
} else if errors.As(err, &errImage) {
if errors.Is(err, utils.ErrImageWithoutBasename) {
err := createErrorInvalidImageWithoutBasename()
return "", "", "", err
} else {
panicMsg := fmt.Sprintf("unexpected %T: %s", err, err)
panic(panicMsg)
}
} else if errors.As(err, &errParseRelease) {
err := createErrorInvalidRelease(errParseRelease.Hint)
return "", "", "", err
Expand Down
14 changes: 14 additions & 0 deletions src/pkg/utils/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type DistroError struct {
Err error
}

type ImageError struct {
Image string
Err error
}

type ParseReleaseError struct {
Hint string
}
Expand All @@ -53,6 +58,15 @@ func (err *DistroError) Unwrap() error {
return err.Err
}

func (err *ImageError) Error() string {
errMsg := fmt.Sprintf("%s: %s", err.Image, err.Err)
return errMsg
}

func (err *ImageError) Unwrap() error {
return err.Err
}

func (err *ParseReleaseError) Error() string {
return err.Hint
}
4 changes: 3 additions & 1 deletion src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ var (
ErrDistroUnsupported = errors.New("distribution is unsupported")

ErrDistroWithoutRelease = errors.New("non-default distribution must specify release")

ErrImageWithoutBasename = errors.New("image does not have a basename")
)

func init() {
Expand Down Expand Up @@ -239,7 +241,7 @@ func GetCgroupsVersion() (int, error) {
func getContainerNamePrefixForImage(image string) (string, error) {
basename := ImageReferenceGetBasename(image)
if basename == "" {
return "", fmt.Errorf("failed to get the basename of image %s", image)
return "", &ImageError{image, ErrImageWithoutBasename}
}

for _, distroObj := range supportedDistros {
Expand Down

0 comments on commit f23a161

Please sign in to comment.