Skip to content

Unexported error sentinel values #24

@romshark

Description

@romshark

structtag/tags.go

Lines 11 to 19 in 2977b8d

var (
errTagSyntax = errors.New("bad syntax for struct tag pair")
errTagKeySyntax = errors.New("bad syntax for struct tag key")
errTagValueSyntax = errors.New("bad syntax for struct tag value")
errKeyNotSet = errors.New("tag key does not exist")
errTagNotExist = errors.New("tag does not exist")
errTagKeyMismatch = errors.New("mismatch between key and tag.key")
)

Is there a particular reason these error sentinel values are unexported?
Because of this I have to match errors by message, which is obviously bad:

tag, err := tags.Get(expectTag)
switch {
case err.Error() == "tag does not exist":
	// handle errTagNotExist
case err != nil:
	// handle other errors
}

Instead, it should be:

tag, err := tags.Get(expectTag)
switch {
case errors.Is(err, structtag.ErrTagNotExist):
	// handle
case err != nil:
	// handle other errors
}

I propose exporting error sentinel values. Such a change would be backward compatible anyway.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions