Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix panic when fetching resources fails due to network error#1506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SamMorrowDrums wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromfix-connectivity-panic

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

InRepositoryResourceContentsHandler, the code calleddefer resp.Body.Close()before checking ifGetRawContent returned an error:

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

When a network error occurs,resp isnil, causing a panic when the deferred function tries to accessresp.Body.

Solution

Move the error checkbefore the defer statement:

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

Testing

AddedTest_repositoryResourceContentsHandler_NetworkError which uses a customerrorTransport that always returns an error. This test ensures the handler gracefully handles network errors without panicking.

$ gotest ./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

CopilotAI review requested due to automatic review settingsNovember 28, 2025 22:34
@SamMorrowDrumsSamMorrowDrums requested a review froma team as acode ownerNovember 28, 2025 22:34
Copy link
Contributor

CopilotAI 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 thedefer resp.Body.Close() statement to prevent nil pointer dereference
  • Added comprehensive test coverage using a customerrorTransport 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.

FileDescription
pkg/github/repository_resource.goMoved error check forGetRawContent before the defer statement to prevent panic on nil response
pkg/github/repository_resource_test.goAddederrorTransport type andTest_repositoryResourceContentsHandler_NetworkError to verify graceful handling of network errors

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

Reviewers

Copilot code reviewCopilotCopilot left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@SamMorrowDrums

[8]ページ先頭

©2009-2025 Movatter.jp