Skip to content

Commit

Permalink
handle error correctly when dumping http responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kiamran authored Jan 23, 2020
1 parent 52cf75b commit 30806d7
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,13 @@ type Anodot20Client struct {
}

//Constructs new Anodot 2.0 submitter which should be used to send metrics to Anodot.
func NewAnodot20Client(anodotURL string, apiToken string, httpClient *http.Client) (*Anodot20Client, error) {
parsedUrl, err := url.Parse(anodotURL)
if err != nil {
return nil, fmt.Errorf("failed to parse Anodot server url: %w", err)
}
func NewAnodot20Client(anodotURL url.URL, apiToken string, httpClient *http.Client) (*Anodot20Client, error) {

if len(strings.TrimSpace(apiToken)) == 0 {
return nil, fmt.Errorf("anodot api token should not be blank")
}

submitter := Anodot20Client{Token: apiToken, ServerURL: parsedUrl, client: httpClient}
submitter := Anodot20Client{Token: apiToken, ServerURL: &anodotURL, client: httpClient}
if httpClient == nil {
client := http.Client{Timeout: 30 * time.Second}

Expand Down Expand Up @@ -265,6 +261,11 @@ func (d *debugHTTPTransport) RoundTrip(h *http.Request) (*http.Response, error)
dump, _ := httputil.DumpRequestOut(h, true)
fmt.Printf("----------------------------------REQUEST----------------------------------\n%s\n", string(dump))
resp, err := d.r.RoundTrip(h)
if err != nil {
fmt.Println("failed to obtain response: ", err.Error())
return resp, err
}

dump, _ = httputil.DumpResponse(resp, true)
fmt.Printf("----------------------------------RESPONSE----------------------------------\n%s\n----------------------------------\n\n", string(dump))
return resp, err
Expand Down

0 comments on commit 30806d7

Please sign in to comment.