Skip to content

Commit

Permalink
Wrap errors in client
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Stankevich committed Apr 18, 2023
1 parent 3bf865d commit 74f3e01
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions buildkite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (client *Client) makeRequest(method string, path string, postData interface
if postData != nil {
jsonPayload, err := json.Marshal(postData)
if err != nil {
return err
return fmt.Errorf("failed to marshal request: %w", err)
}
bodyBytes = bytes.NewBuffer(jsonPayload)
}
Expand All @@ -61,27 +61,27 @@ func (client *Client) makeRequest(method string, path string, postData interface

req, err := http.NewRequest(method, url, bodyBytes)
if err != nil {
return err
return fmt.Errorf("failed to create request: %w", err)
}

req.Header.Add("User-Agent", client.userAgent)

resp, err := client.http.Do(req)
if err != nil {
return err
return fmt.Errorf("failed to send request: %w", err)
}
if resp.StatusCode >= 400 {
return fmt.Errorf("Buildkite API request failed: %s %s (status: %d)", method, url, resp.StatusCode)
}

defer resp.Body.Close()

responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
return fmt.Errorf("failed to read response: %w", err)
}

if err := json.Unmarshal(responseBody, responseObject); err != nil {
return err
return fmt.Errorf("failed to unmarshal response: %w", err)
}

return nil
Expand Down

0 comments on commit 74f3e01

Please sign in to comment.