Skip to content

Commit

Permalink
fix: ensure we capture and propagate errors in src/internal
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp committed Sep 25, 2024
1 parent 7505f18 commit e7f3244
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/internal/git/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 7 additions & 2 deletions src/internal/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e7f3244

Please sign in to comment.