Skip to content

Fix: Preserve HTTP Response in URL Errors - #3369

Merged
gmlewis merged 4 commits into
google:masterfrom
silasalberti:devin/1733470068-preserve-response-in-url-errors
Dec 8, 2024
Merged

Fix: Preserve HTTP Response in URL Errors#3369
gmlewis merged 4 commits into
google:masterfrom
silasalberti:devin/1733470068-preserve-response-in-url-errors

Conversation

@silasalberti

@silasalbertisilasalberti commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

Fix: Preserve HTTP Response in URL Errors

Fixes#3362

This PR improves error handling in the BareDo method to properly preserve HTTP response information when encountering *url.Error. Changes include:

  • Move response creation earlier to avoid code duplication
  • Add proper nil response handling
  • Preserve HTTP response information in URL errors
  • Add test case TestDo_preservesResponseInURLError

The changes allow users to access HTTP status codes (like 404) even when encountering URL errors.

This PR was created with help from Devin: https://preview.devin.ai/sessions/1b2f7ce6e3b44942b3ac1f518eac7c22

@google-cla

google-claBot commented Dec 6, 2024

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

… errors
- Move newResponse call earlier to avoid code duplication
- Properly handle nil responses with var declaration
- Return response object with URL errors to preserve HTTP information
- Add test case using client.Do to verify behavior
@devin-ai-integration
devin-ai-integrationBotforce-pushed the devin/1733470068-preserve-response-in-url-errors branch from a4f97fc to d341400CompareDecember 6, 2024 08:58
@codecov

codecovBot commented Dec 6, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.

Project coverage is 92.30%. Comparing base (2b8c7fa) to head (d9e59c1).
Report is 183 commits behind head on master.

Files with missing linesPatch %Lines
github/github.go71.42%2 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## master #3369 +/- ##
==========================================
- Coverage 97.72% 92.30% -5.42% 
==========================================
Files 153 176 +23 Lines 13390 15031 +1641 ==========================================
+ Hits 13085 13874 +789 - Misses 215 1064 +849 - Partials 90 93 +3 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, @silasalberti !
This looks like a great start. I think the unit test can be improved, however.

Additionally, on line 854 I think it is also a safe location to return the new response instead of nil.

Comment threadgithub/github_test.go Outdated
}
}

func TestDo_preservesResponseInURLError(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not convinced that this test actually creates a url.Error.
For examples, please see: https://cs.opensource.google/go/go/+/refs/tags/go1.23.4:src/net/url/url_test.go;l=1763-1821

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

That's a good point. I guess the original issue was about a 404 error. Renamed the test to HTTPError and made the mock more in line with the other mocks in the file. Also, now it's returning response on line 854 too. Does that look better?

@gmlewisgmlewis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ideally, tests would cover each of the return paths through BareDo but I'm thinking that this may be much more effort to achieve... so I think I'm fine with this level of testing.

LGTM.

Awaiting second LGTM+Approval from any other contributor to this repo before merging.

@gmlewisgmlewis added the NeedsReview PR is awaiting a review before merging. label Dec 7, 2024

@tomfeigintomfeigin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@gmlewisgmlewis removed the NeedsReview PR is awaiting a review before merging. label Dec 8, 2024
@gmlewis

Copy link
Copy Markdown
Collaborator

Thank you, @tomfeigin !
Merging.

@gmlewis
gmlewis merged commit f0bfdaf into google:masterDec 8, 2024
jlaportebot added a commit to jlaportebot/go-github that referenced this pull request Jun 28, 2026
Sign up for freeto 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.

Propagate *http.Response when encountering a *url.Error?

3 participants

@silasalberti@gmlewis@tomfeigin