From e7f324457f335ea8b69e71c6fdd98e01c23f8b40 Mon Sep 17 00:00:00 2001 From: Kit Patella Date: Wed, 25 Sep 2024 10:07:05 -0700 Subject: [PATCH] fix: ensure we capture and propagate errors in src/internal Signed-off-by: Kit Patella --- src/internal/git/repository_test.go | 3 ++- src/internal/gitea/gitea.go | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/internal/git/repository_test.go b/src/internal/git/repository_test.go index 7e75319eee..e833e6e2f7 100644 --- a/src/internal/git/repository_test.go +++ b/src/internal/git/repository_test.go @@ -56,7 +56,8 @@ func TestRepository(t *testing.T) { require.NoError(t, err) _, err = newFile.Write([]byte("Hello World")) require.NoError(t, err) - newFile.Close() + err = newFile.Close() + require.NoError(t, err) _, err = w.Add(filePath) require.NoError(t, err) _, err = w.Commit("Initial commit", &git.CommitOptions{ diff --git a/src/internal/gitea/gitea.go b/src/internal/gitea/gitea.go index 94244d03f1..6aff480af6 100644 --- a/src/internal/gitea/gitea.go +++ b/src/internal/gitea/gitea.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -47,7 +48,7 @@ func NewClient(endpoint, username, password string) (*Client, error) { } // DoRequest performs a request to the Gitea API at the given path. -func (g *Client) DoRequest(ctx context.Context, method string, path string, body []byte) ([]byte, int, error) { +func (g *Client) DoRequest(ctx context.Context, method string, path string, body []byte) (_ []byte, _ int, err error) { u, err := g.endpoint.Parse(path) if err != nil { return nil, 0, err @@ -60,10 +61,14 @@ func (g *Client) DoRequest(ctx context.Context, method string, path string, body req.Header.Add("accept", "application/json") req.Header.Add("content-type", "application/json") resp, err := g.httpClient.Do(req) + // Ensure we close the body of the http client and capture the error + defer func() { + errClose := resp.Body.Close() + err = errors.Join(err, errClose) + }() if err != nil { return nil, 0, err } - defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err != nil { return nil, 0, err