Skip to content

Commit

Permalink
Wrap some additonal errors such that they can be identified as credhub
Browse files Browse the repository at this point in the history
related
  • Loading branch information
aeijdenberg committed Dec 21, 2018
1 parent f855260 commit 06c930a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions credhub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Client struct {
}

var (
errCredNotFound = errors.New("not found in credhub")
errCredNotFound = credHubErr{errors.New("not found in credhub")}
)

type credHubErr struct {
Expand All @@ -50,13 +50,13 @@ func (c *Client) Init() error {
for _, ca := range c.UAACACerts {
ok := uaaCaCertPool.AppendCertsFromPEM([]byte(ca))
if !ok {
return errors.New("AppendCertsFromPEM was not ok")
return credHubErr{errors.New("AppendCertsFromPEM was not ok")}
}
}
for _, ca := range c.CredHubCACerts {
ok := credHubCaCertPool.AppendCertsFromPEM([]byte(ca))
if !ok {
return errors.New("AppendCertsFromPEM was not ok")
return credHubErr{errors.New("AppendCertsFromPEM was not ok")}
}
}

Expand Down Expand Up @@ -101,13 +101,13 @@ func (ch *Client) updateToken() error {
return credHubErr{err}
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("not OK response from UAA: %s", data)
return credHubErr{fmt.Errorf("not OK response from UAA: %s", data)}
}

var at oauthToken
err = json.Unmarshal(data, &at)
if err != nil {
return err
return credHubErr{err}
}

ch.token = &at
Expand All @@ -128,7 +128,7 @@ func (ch *Client) MakeRequest(path string, params url.Values, rv interface{}) er
func (ch *Client) PutRequest(path string, val, rv interface{}) error {
data, err := json.Marshal(val)
if err != nil {
return err
return credHubErr{err}
}

req, err := http.NewRequest(http.MethodPut, ch.CredHubURL+path, bytes.NewReader(data))
Expand Down Expand Up @@ -172,12 +172,16 @@ func (ch *Client) rawMakeRequest(req *http.Request, rv interface{}) error {

switch resp.StatusCode {
case http.StatusOK:
return json.Unmarshal(contents, rv)
err = json.Unmarshal(contents, rv)
if err != nil {
return credHubErr{err}
}
return nil
case http.StatusNoContent:
return nil // expected for deleted
case http.StatusNotFound:
return errCredNotFound
default:
return fmt.Errorf("not OK response from CredHub: %s", contents)
return credHubErr{fmt.Errorf("not OK response from CredHub: %s", contents)}
}
}

0 comments on commit 06c930a

Please sign in to comment.