diff --git a/buildkite/client.go b/buildkite/client.go index 484317b2..7f53519d 100644 --- a/buildkite/client.go +++ b/buildkite/client.go @@ -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) } @@ -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