Skip to content

Conversation

@SamMorrowDrums
Copy link
Collaborator

Summary

Fixes a panic that occurs when fetching repository resource content fails due to a network error (e.g., connection timeout, DNS failure).

Problem

In RepositoryResourceContentsHandler, the code called defer resp.Body.Close() before checking if GetRawContent returned an error:

resp, err := rawClient.GetRawContent(ctx, owner, repo, path, rawOpts)
defer func() {
    _ = resp.Body.Close()
}()
// If the raw content is not found...
switch {
case err != nil:
    return nil, fmt.Errorf("failed to get raw content: %w", err)

When a network error occurs, resp is nil, causing a panic when the deferred function tries to access resp.Body.

Solution

Move the error check before the defer statement:

resp, err := rawClient.GetRawContent(ctx, owner, repo, path, rawOpts)
if err != nil {
    return nil, fmt.Errorf("failed to get raw content: %w", err)
}
defer func() {
    _ = resp.Body.Close()
}()

Testing

Added Test_repositoryResourceContentsHandler_NetworkError which uses a custom errorTransport that always returns an error. This test ensures the handler gracefully handles network errors without panicking.

$ go test ./pkg/github/... -run "Test_repositoryResource" -v
=== RUN   Test_repositoryResourceContentsHandler
...
--- PASS: Test_repositoryResourceContentsHandler (0.01s)
=== RUN   Test_repositoryResourceContentsHandler_NetworkError
--- PASS: Test_repositoryResourceContentsHandler_NetworkError (0.00s)
PASS

Checklist

  • script/lint passes
  • script/test passes
  • Added test for the fix

Copilot AI review requested due to automatic review settings November 28, 2025 22:34
@SamMorrowDrums SamMorrowDrums requested a review from a team as a code owner November 28, 2025 22:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug that caused a panic when network errors occurred during repository resource content fetching. The fix ensures error handling happens before attempting to access the response body.

  • Moved error check before the defer resp.Body.Close() statement to prevent nil pointer dereference
  • Added comprehensive test coverage using a custom errorTransport to simulate network failures
  • Follows Go best practices by checking errors before using returned values

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/github/repository_resource.go Moved error check for GetRawContent before the defer statement to prevent panic on nil response
pkg/github/repository_resource_test.go Added errorTransport type and Test_repositoryResourceContentsHandler_NetworkError to verify graceful handling of network errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants