Skip to content

Commit

Permalink
cloudflare: improve error context
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo <[email protected]>
  • Loading branch information
lfaoro committed Jun 4, 2019
1 parent 55c20da commit e57e272
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -62,30 +63,33 @@ func (cf Cloudflare) Zones() (map[string]string, error) {
endpoint := cf.API + "/zones"
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cloudflare:")
}

v := url.Values{}
v.Add("per_page", "50")
v.Add("page", string(count))
maxPerPageValue := 50
v.Add("per_page", strconv.Itoa(maxPerPageValue))
v.Add("page", strconv.Itoa(count))
u.RawQuery = v.Encode()

fmt.Println(u.String())

req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cloudflare:")
}

cf.setAuthHeaders(req)

res, err := cf.Client.Do(req)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "cloudflare:")
}
defer res.Body.Close()

data := response{}
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
return nil, errors.Wrap(err, "cloudflare:")
}

if !data.Success {
Expand All @@ -96,7 +100,7 @@ func (cf Cloudflare) Zones() (map[string]string, error) {
result[res.ID] = res.Name
}

pages := data.ResultInfo.TotalCount / data.ResultInfo.PerPage
pages := data.ResultInfo.TotalCount / maxPerPageValue
if count < pages {
count++
continue
Expand Down

0 comments on commit e57e272

Please sign in to comment.