Skip to content

Commit

Permalink
Merge pull request #1919 from vntw/ignore-head-req-body
Browse files Browse the repository at this point in the history
Ignore response body during HEAD request error handling
  • Loading branch information
svanharmelen committed May 14, 2024
2 parents 16ebc6f + f49ba02 commit f44d73d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
19 changes: 15 additions & 4 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -65,6 +66,8 @@ const (
PrivateToken
)

var ErrNotFound = errors.New("404 Not Found")

// A Client manages communication with the GitLab API.
type Client struct {
// HTTP client used to communicate with the API.
Expand Down Expand Up @@ -504,7 +507,7 @@ func (c *Client) retryHTTPBackoff(min, max time.Duration, attemptNum int, resp *
// min and max are mainly used for bounding the jitter that will be added to
// the reset time retrieved from the headers. But if the final wait time is
// less then min, min will be used instead.
func rateLimitBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
func rateLimitBackoff(min, max time.Duration, _ int, resp *http.Response) time.Duration {
// rnd is used to generate pseudo-random numbers.
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

Expand Down Expand Up @@ -957,20 +960,28 @@ type ErrorResponse struct {

func (e *ErrorResponse) Error() string {
path, _ := url.QueryUnescape(e.Response.Request.URL.Path)
u := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, u, e.Response.StatusCode, e.Message)
url := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)

if e.Message == "" {
return fmt.Sprintf("%s %s: %d", e.Response.Request.Method, url, e.Response.StatusCode)
} else {
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, url, e.Response.StatusCode, e.Message)
}
}

// CheckResponse checks the API response for errors, and returns them if present.
func CheckResponse(r *http.Response) error {
switch r.StatusCode {
case 200, 201, 202, 204, 304:
return nil
case 404:
return ErrNotFound
}

errorResponse := &ErrorResponse{Response: r}

data, err := io.ReadAll(r.Body)
if err == nil && data != nil {
if err == nil && strings.TrimSpace(string(data)) != "" {
errorResponse.Body = data

var raw interface{}
Expand Down
29 changes: 29 additions & 0 deletions gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,35 @@ func TestCheckResponseOnUnknownErrorFormat(t *testing.T) {
}
}

func TestCheckResponseOnHeadRequestError(t *testing.T) {
c, err := NewClient("")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}

req, err := c.NewRequest(http.MethodHead, "test", nil, nil)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}

resp := &http.Response{
Request: req.Request,
StatusCode: http.StatusNotFound,
Body: nil,
}

errResp := CheckResponse(resp)
if errResp == nil {
t.Fatal("Expected error response.")
}

want := "404 Not Found"

if errResp.Error() != want {
t.Errorf("Expected error: %s, got %s", want, errResp.Error())
}
}

func TestRequestWithContext(t *testing.T) {
c, err := NewClient("")
if err != nil {
Expand Down

0 comments on commit f44d73d

Please sign in to comment.