Skip to content

Commit

Permalink
Make errors from API2Resource users friendly by include relevent
Browse files Browse the repository at this point in the history
details like charm and resource names if available.
  • Loading branch information
hmlanigan committed Mar 19, 2021
1 parent cff0719 commit ee66d9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions charmstore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (c Client) listResources(ch CharmID) ([]resource.Resource, error) {
if err != nil {
return nil, errors.Trace(err)
}
return api2resources(resources)
return api2resources(ch.URL.String(), resources)
}

// csWrapper is a type that abstracts away the low-level implementation details
Expand Down Expand Up @@ -333,13 +333,17 @@ func (c csclientImpl) ResourceMeta(channel csparams.Channel, id *charm.URL, name
return client.ResourceMeta(id, name, revision)
}

func api2resources(res []csparams.Resource) ([]resource.Resource, error) {
func api2resources(name string, res []csparams.Resource) ([]resource.Resource, error) {
result := make([]resource.Resource, len(res))
for i, r := range res {
var err error
result[i], err = csparams.API2Resource(r)
if err != nil {
return nil, errors.Trace(err)
msg := name
if r.Name != "" {
msg = fmt.Sprintf("%s resource %q", msg, r.Name)
}
return nil, errors.Trace(errors.Annotatef(err, "%s", msg))
}
}
return result, nil
Expand Down
18 changes: 18 additions & 0 deletions charmstore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package charmstore

import (
"fmt"
"io/ioutil"
"strings"

Expand Down Expand Up @@ -169,6 +170,23 @@ func (s *ClientSuite) TestListResourcesError(c *gc.C) {
c.Assert(ret, gc.IsNil)
}

func (s *ClientSuite) TestListResourcesAPI2ResourcesFailure(c *gc.C) {
dev := params.Resource{
Name: "name2",
}
s.wrapper.ReturnListResourcesStable = []resourceResult{oneResourceResult(dev)}
client, err := newCachingClient(s.cache, "", s.wrapper.makeWrapper)
c.Assert(err, jc.ErrorIsNil)

curl := charm.MustParseURL("cs:quantal/foo-1")
ret, err := client.ListResources([]CharmID{{
URL: curl,
Channel: params.StableChannel,
}})
c.Assert(err, gc.ErrorMatches, fmt.Sprintf("%s resource %q: .*", curl.String(), dev.Name))
c.Assert(ret, gc.IsNil)
}

func (s *ClientSuite) TestGetResource(c *gc.C) {
fp, err := resource.GenerateFingerprint(strings.NewReader("data"))
c.Assert(err, jc.ErrorIsNil)
Expand Down

0 comments on commit ee66d9f

Please sign in to comment.