Skip to content

Commit

Permalink
state: fix error report from entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Peppe committed Aug 7, 2013
1 parent 8a92c5b commit 5d67b0c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions names/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ func makeTag(kind, rest string) string {
func ParseTag(tag, expectKind string) (kind, id string, err error) {
kind, id, err = splitTag(tag)
if err != nil {
if expectKind != "" {
return "", "", fmt.Errorf("%q is not a valid %s tag", tag, expectKind)
}
return "", "", fmt.Errorf("%q is not a valid tag", tag)
return "", "", invalidTagError(tag, expectKind)
}
if expectKind != "" && kind != expectKind {
return "", "", fmt.Errorf("%q is not a valid %s tag", tag, expectKind)
return "", "", invalidTagError(tag, expectKind)
}
if toId := tagSuffixToId[kind]; toId != nil {
id = toId(id)
}
if verify := verifyId[kind]; verify != nil && !verify(id) {
return "", "", fmt.Errorf("%q is not a valid %s tag", tag, kind)
return "", "", invalidTagError(tag, expectKind)
}
return kind, id, nil
}

func invalidTagError(tag, kind string) error {
if kind != "" {
return fmt.Errorf("%q is not a valid %s tag", tag, kind)
}
return fmt.Errorf("%q is not a valid tag", tag)
}
2 changes: 1 addition & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (st *State) entity(tag string) (interface{}, error) {
// Return an invalid entity error if the requested environment is not
// the current one.
if id != conf.Name() {
return nil, fmt.Errorf(`%q is not a valid environment tag`, tag)
return nil, errors.NotFoundf("environment %q", id)
}
return st.Environment()
}
Expand Down
8 changes: 6 additions & 2 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1386,15 +1386,19 @@ func (s *StateSuite) TestSetAdminMongoPassword(c *gc.C) {
}

func (s *StateSuite) testEntity(c *gc.C, getEntity func(string) (state.Tagger, error)) {
bad := []string{"", "machine", "-foo", "foo-", "---", "machine-bad", "unit-123", "unit-foo", "service-", "service-foo/bar", "environment-foo"}
bad := []string{"", "machine", "-foo", "foo-", "---", "machine-bad", "unit-123", "unit-foo", "service-", "service-foo/bar"}
for i, name := range bad {
c.Logf("test %d. entity %q", i, name)
e, err := getEntity(name)
c.Check(e, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `".*" is not a valid( [a-z]+)? tag`)
}
e, err := getEntity("environment-foo")
c.Check(e, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `environment "foo" not found`)
c.Assert(err, jc.Satisfies, errors.IsNotFoundError)

e, err := getEntity("machine-1234")
e, err = getEntity("machine-1234")
c.Check(e, gc.IsNil)
c.Assert(err, gc.ErrorMatches, `machine 1234 not found`)
c.Assert(err, jc.Satisfies, errors.IsNotFoundError)
Expand Down

0 comments on commit 5d67b0c

Please sign in to comment.