Skip to content

Commit

Permalink
Merge pull request #84 from buildkite/handle-invalid-token
Browse files Browse the repository at this point in the history
Handle http errors from the API
  • Loading branch information
lox authored May 5, 2019
2 parents e47fb1d + 70286bf commit 5fbcc6a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package collector

import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -118,15 +120,33 @@ func (c *Collector) Collect() (*Result, error) {
if err != nil {
return nil, err
}
defer res.Body.Close()

if c.DebugHttp {
if dump, err := httputil.DumpResponse(res, true); err == nil {
log.Printf("DEBUG response uri=%s\n%s\n", req.URL, dump)
}
}

// Handle any errors
if res.StatusCode != http.StatusOK {
// If it's json response, show the error message
if strings.HasPrefix(res.Header.Get("Content-Type"), "application/json") {
var errStruct struct {
Message string `json:"message"`
}
err := json.NewDecoder(res.Body).Decode(&errStruct)
if err == nil {
return nil, errors.New(errStruct.Message)
} else {
log.Printf("Failed to decode error: %v", err)
}
}

return nil, fmt.Errorf("Request failed with %s (%d)", res.Status, res.StatusCode)
}

var allMetrics allMetricsResponse
defer res.Body.Close()

// Check if we get a poll duration header from server
if pollSeconds := res.Header.Get(PollDurationHeader); pollSeconds != "" {
Expand Down

0 comments on commit 5fbcc6a

Please sign in to comment.