Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: response body after HTTP error should be closed #106

Merged
merged 8 commits into from
Sep 27, 2024
Prev Previous commit
Next Next commit
chore: refactor location of resp.Body.Close()
karel-rehor committed Sep 27, 2024
commit b136f757d8e0d2d95db3c5abbc858ed13229491c
12 changes: 3 additions & 9 deletions influxdb3/client.go
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"mime"
"net/http"
"net/url"
@@ -188,14 +187,6 @@ func (c *Client) makeAPICall(ctx context.Context, params httpParams) (*http.Resp
}
err = c.resolveHTTPError(resp)
if err != nil {
closingErr := resp.Body.Close()
if closingErr != nil {
slog.Warn(fmt.Sprintf("Failed to close response body on HTTP Error(%s): %s",
err.Error(),
closingErr.Error()))
} else {
slog.Debug(fmt.Sprintf("Closed response body on HTTP Error(%s)", err.Error()))
}
return nil, err
}
return resp, nil
@@ -207,6 +198,9 @@ func (c *Client) resolveHTTPError(r *http.Response) error {
if r.StatusCode >= 200 && r.StatusCode < 300 {
return nil
}
defer func() {
_ = r.Body.Close()
}()

var httpError struct {
ServerError