Skip to content

Commit

Permalink
Defer closing body before checking HTTP status code (google#2870)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored Aug 15, 2023
1 parent 3ecf12c commit bb00f57
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,17 @@ func (c *Client) get(urlStr string, a ...interface{}) (*goquery.Document, error)
if err != nil {
return nil, fmt.Errorf("error parsing URL: %q: %v", urlStr, err)
}

resp, err := c.Client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("error fetching url %q: %v", u, err)
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("received %v response fetching URL %q", resp.StatusCode, u)
}

defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("error parsing response: %v", err)
Expand Down

0 comments on commit bb00f57

Please sign in to comment.