mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/fs: follow-up for ec45f1bc5f
Properly close response body before checking for the response code. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4034
This commit is contained in:
parent
d577657fb7
commit
4d00107b92
1 changed files with 6 additions and 5 deletions
11
lib/fs/fs.go
11
lib/fs/fs.go
|
@ -424,13 +424,14 @@ func ReadFileOrHTTP(path string) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot fetch %q: %w", path, err)
|
return nil, fmt.Errorf("cannot fetch %q: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("unexpected status code when fetching %q: %d, expecting %d", path, resp.StatusCode, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := io.ReadAll(resp.Body)
|
data, err := io.ReadAll(resp.Body)
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
if len(data) > 4*1024 {
|
||||||
|
data = data[:4*1024]
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unexpected status code when fetching %q: %d, expecting %d; response: %q", path, resp.StatusCode, http.StatusOK, data)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot read %q: %s", path, err)
|
return nil, fmt.Errorf("cannot read %q: %s", path, err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue