Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions cns/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ var clientPaths = []string{
cns.GetHomeAz,
}

var ErrAPINotFound error = errors.New("api not found")

type do interface {
Do(*http.Request) (*http.Response, error)
}
Expand Down Expand Up @@ -403,16 +401,18 @@ func (c *Client) RequestIPs(ctx context.Context, ipconfig cns.IPConfigsRequest)
req.Header.Set(headerContentType, contentTypeJSON)
res, err := c.client.Do(req)

// if we get a 404 error
if res.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("cannot find API RequestIPs %w: %v", ErrAPINotFound, err) //nolint:errorlint // multiple %w not supported in 1.19
}

if err != nil {
return nil, errors.Wrap(err, "http request failed")
}
defer res.Body.Close()

if res.StatusCode == http.StatusNotFound {
return nil, &CNSClientError{
Code: types.UnsupportedAPI,
Err: errors.Errorf("Unsupported API"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this error should be a typed error, not an anonymous error, so that it can be compared to with errors.Is/As
  • error strings should not be sentence-case ("unsupported API") because they are embedded within other strings

}
}

if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("http response %d", res.StatusCode)
}
Expand Down Expand Up @@ -445,17 +445,19 @@ func (c *Client) ReleaseIPs(ctx context.Context, ipconfig cns.IPConfigsRequest)
}
req.Header.Set(headerContentType, contentTypeJSON)
res, err := c.client.Do(req)

// if we get a 404 error
if res.StatusCode == http.StatusNotFound {
return fmt.Errorf("cannot find API ReleaseIPs %w: %v", ErrAPINotFound, err) //nolint:errorlint // multiple %w not supported in 1.19
}

if err != nil {
return errors.Wrap(err, "http request failed")
}
defer res.Body.Close()

// if we get a 404 error
if res.StatusCode == http.StatusNotFound {
return &CNSClientError{
Code: types.UnsupportedAPI,
Err: errors.Errorf("Unsupported API"),
}
}

if res.StatusCode != http.StatusOK {
return errors.Errorf("http response %d", res.StatusCode)
}
Expand Down