Skip to content

Commit

Permalink
chore: add debug-level logging to doHTTPReq (#869)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildwonkey authored Jan 8, 2025
1 parent 3219c19 commit 376f686
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/pkg/domains/api/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func doHTTPReq(ctx context.Context, client http.Client, method string, url url.U

req, err := http.NewRequestWithContext(ctx, method, url.String(), body)
if err != nil {
message.Debugf("error from http.NewRequestWithContext: %s", err)
return nil, err
}
// add each header to the request
Expand All @@ -32,27 +33,33 @@ func doHTTPReq(ctx context.Context, client http.Client, method string, url url.U
}

// log the request
message.Debug("%q %s", method, req.URL.Redacted())
message.Debugf("%q %s", method, req.URL.Redacted())

// do the thing
res, err := client.Do(req)
if err != nil {
message.Debugf("error from client.Do: %s", err)
return nil, err
}
if res == nil {
return nil, fmt.Errorf("error: calling %s returned empty response", url.Redacted())
message.Debug("empty response")
return nil, fmt.Errorf("error: %s returned empty response", url.Redacted())
}
defer res.Body.Close()

responseData, err := io.ReadAll(res.Body)
if err != nil {
message.Debugf("error reading response body: %s", err)
return nil, err
}

var respObj APIResponse
respObj.Raw = responseData
respObj.Status = res.StatusCode
err = json.Unmarshal(responseData, &respObj.Response)
if err != nil {
message.Debugf("error unmarshalling response: %s", err)
}
return &respObj, err
}

Expand Down

0 comments on commit 376f686

Please sign in to comment.