Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message when ParseReference fails #1808

Closed
wants to merge 2 commits into from

Conversation

taeold
Copy link

@taeold taeold commented Oct 10, 2023

When given invalid reference name, name.ParseReference returns a generic error message:

package main

import (
	"fmt"

	"github.com/google/go-containerregistry/pkg/name"
)

var badTagNames = []string{
	"gcr.io/project-id/bad_chars:c@n'tuse",
	"gcr.io/project-id/wrong-length:white space",
	"gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends",
}

func main() {
	for _, n := range badTagNames {
		_, err := name.ParseReference(n)
		fmt.Printf("ParseReference(%q) returned err=%v\n", n, err)
	}
}

# ParseReference("gcr.io/project-id/bad_chars:c@n'tuse") returned err=could not parse reference: gcr.io/project-id/bad_chars:c@n'tuse
# ParseReference("gcr.io/project-id/wrong-length:white space") returned err=could not parse reference: gcr.io/project-id/wrong-length:white space
# ParseReference("gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends") returned err=could not parse reference: gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends

playground

The error message is succinct but unhelpful when the end user is trying to figure out why the given reference name is invalid.

We modify the code to include more descriptive error message on why the parsing failed as a tag or digest:

# could not parse reference 'gcr.io/project-id/bad_chars:c@n'tuse' as tag or digest: tag error: tag can only contain the characters `abcdefghijklmnopqrstuvwxyz0123456789_-.ABCDEFGHIJKLMNOPQRSTUVWXYZ`: c@n'tuse, digest error: unsupported digest algorithm: n'tuse
# could not parse reference 'gcr.io/project-id/wrong-length:white space' as tag or digest: tag error: tag can only contain the characters `abcdefghijklmnopqrstuvwxyz0123456789_-.ABCDEFGHIJKLMNOPQRSTUVWXYZ`: white space, digest error: a digest must contain exactly one '@' separator (e.g. registry/repository@digest) saw: gcr.io/project-id/wrong-length:white space
# could not parse reference 'gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends' as tag or digest: tag error: tag must be between 1 and 128 characters in length: thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends, digest error: a digest must contain exactly one '@' separator (e.g. registry/repository@digest) saw: gcr.io/project-id/too-many-chars:thisisthetagthatneverendsitgoesonandonmyfriendsomepeoplestartedtaggingitnotknowingwhatitwasandtheyllcontinuetaggingitforeverjustbecausethisisthetagthatneverends

The resulting error message is much more verbose, but clearly identifies the problem with the given resource name.

@taeold taeold changed the title Improve error message when ParseReference fails fix: improve error message when ParseReference fails Oct 13, 2023
Copy link

This Pull Request is stale because it has been open for 90 days with
no activity. It will automatically close after 30 more days of
inactivity. Keep fresh with the 'lifecycle/frozen' label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant