Skip to content

Commit

Permalink
recover devin's local changes
Browse files Browse the repository at this point in the history
  • Loading branch information
silasalberti committed Dec 7, 2024
1 parent d959665 commit d9e59c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro
// the context's error is probably more useful.
select {
case <-ctx.Done():
return nil, ctx.Err()
return response, ctx.Err()

Check warning on line 854 in github/github.go

View check run for this annotation

Codecov / codecov/patch

github/github.go#L854

Added line #L854 was not covered by tests
default:
}

Expand Down
34 changes: 29 additions & 5 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,21 +1109,45 @@ func TestDo_redirectLoop(t *testing.T) {
func TestDo_preservesResponseInHTTPError(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Not Found", http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, `{
"message": "Resource not found",
"documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository"
}`)
})

req, _ := client.NewRequest("GET", ".", nil)
resp, err := client.Do(context.Background(), req, nil)
var resp *Response
var data interface{}
resp, err := client.Do(context.Background(), req, &data)

if err == nil {
t.Fatal("Expected error to be returned")
t.Fatal("Expected error response")
}

// Verify error type and access to status code
errResp, ok := err.(*ErrorResponse)
if !ok {
t.Fatalf("Expected *ErrorResponse error, got %T", err)
}

// Verify status code is accessible from both Response and ErrorResponse
if resp == nil {
t.Fatal("Expected response to be returned even with error")
}
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status code 404, got %d", resp.StatusCode)
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
t.Errorf("Response status = %d, want %d", got, want)
}
if got, want := errResp.Response.StatusCode, http.StatusNotFound; got != want {
t.Errorf("Error response status = %d, want %d", got, want)
}

// Verify error contains proper message
if !strings.Contains(errResp.Message, "Resource not found") {
t.Errorf("Error message = %q, want to contain 'Resource not found'", errResp.Message)
}
}

Expand Down

0 comments on commit d9e59c1

Please sign in to comment.