Check the error of closing response body

This commit is contained in:
Haiyan Meng
2020-01-08 10:32:12 -08:00
parent ccd129f7a5
commit b154af8be4

View File

@@ -124,7 +124,7 @@ func (gc githubCrawler) FetchDocument(_ context.Context, d *doc.Document) error
}
if err == nil && resp.StatusCode == http.StatusOK {
d.IsSame = httpclient.FromCache(resp.Header)
defer resp.Body.Close()
defer CloseResponseBody(resp)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
@@ -311,7 +311,10 @@ func (gcl GhClient) GetFileData(k GhFileSpec) ([]byte, error) {
return nil, fmt.Errorf("%+v: could not read '%s' metadata: %v",
k, url, err)
}
resp.Body.Close()
if err := resp.Body.Close(); err != nil {
return nil, err
}
type githubContentRawURL struct {
DownloadURL string `json:"download_url,omitempty"`
@@ -330,18 +333,24 @@ func (gcl GhClient) GetFileData(k GhFileSpec) ([]byte, error) {
k, rawURL.DownloadURL, err)
}
defer resp.Body.Close()
defer CloseResponseBody(resp)
data, err = ioutil.ReadAll(resp.Body)
return data, err
}
func CloseResponseBody(resp *http.Response) {
if err := resp.Body.Close(); err != nil {
log.Printf("failed to close response body: %v", err)
}
}
func (gcl GhClient) GetDefaultBranch(url string) (string, error) {
resp, err := gcl.GetReposData(url)
if err != nil {
return "", fmt.Errorf(
"'%s' could not get default_branch: %v", url, err)
}
defer resp.Body.Close()
defer CloseResponseBody(resp)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf(
@@ -393,7 +402,7 @@ func (gcl GhClient) GetFileCreationTime(
}
}
defer resp.Body.Close()
defer CloseResponseBody(resp)
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return defaultTime, fmt.Errorf(
@@ -516,7 +525,7 @@ func (gcl GhClient) parseGithubResponse(getRequest string) GhResponseInfo {
}
var data []byte
defer resp.Body.Close()
defer CloseResponseBody(resp)
data, requestInfo.Error = ioutil.ReadAll(resp.Body)
if requestInfo.Error != nil {
return requestInfo