Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Preserve HTTP Response in URL Errors #3369

Conversation

silasalberti
Copy link
Contributor

@silasalberti silasalberti commented Dec 6, 2024

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

Copy link

google-cla bot commented Dec 6, 2024

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-integration bot force-pushed the devin/1733470068-preserve-response-in-url-errors branch from a4f97fc to d341400 Compare December 6, 2024 08:58
Copy link

codecov bot commented Dec 6, 2024

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 lines Patch % Lines
github/github.go 71.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.

Copy link
Collaborator

@gmlewis gmlewis left a comment

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.

@@ -1106,6 +1106,27 @@ func TestDo_redirectLoop(t *testing.T) {
}
}

func TestDo_preservesResponseInURLError(t *testing.T) {
Copy link
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
Contributor Author

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?

Copy link
Collaborator

@gmlewis gmlewis left a comment

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.

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Dec 7, 2024
Copy link
Contributor

@tomfeigin tomfeigin left a comment

Choose a reason for hiding this comment

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

LGTM

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

gmlewis commented Dec 8, 2024

Thank you, @tomfeigin !
Merging.

@gmlewis gmlewis merged commit f0bfdaf into google:master Dec 8, 2024
5 of 7 checks passed
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.

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