Repository navigation
Raise PyDrive2's RefreshError when a refresh during a request fails - #452
Draft
shcheklein wants to merge 3 commits into
Draft
shcheklein wants to merge 3 commits into
shcheklein wants to merge 3 commits into
Conversation
When Drive answered 401 to a token that had not expired by the clock, google-auth refreshed it inside the request. If that refresh failed, its own RefreshError reached the caller, with the whole token response in its text. Requests now raise PyDrive2's RefreshError with the same message as Refresh(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shcheklein
commented
Dec 3, 2026
shcheklein
commented
Dec 3, 2026
shcheklein
left a comment
Member
Author
There was a problem hiding this comment.
[AI generated — Copilot]
5-pass review summary: covers all 4 Drive request execution sites cleanly. Worth deduplicating the 4 repeated error-translation blocks and asserting __cause__ in the tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Regression coverage is missing for a refresh failure after the first successful download chunk, including assertions for PyDrive2's RefreshError and its original cause.
1 open issue
What changed in this PR
Makes request-time token refresh failures raise PyDrive2’s RefreshError, matching explicit refresh behavior from #437.
Changes:
- Shares error conversion across file operations, listings, downloads, and Drive requests.
- Preserves the original error as the cause.
- Expands live tests to cover six refresh-failure paths.
| File | Description |
|---|---|
| pydrive2/test/test_auth_flows.py | Adds shared credentials and parameterized refresh-failure tests. |
| pydrive2/files.py | Applies shared error handling to requests and download chunks. |
| pydrive2/drive.py | Uses shared error handling for Drive requests. |
| pydrive2/auth.py | Extracts reusable refresh-error conversion. |
Review effort: Balanced
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

A failed token refresh surfaced in two different forms, depending on when the refresh happened. One of them was google-auth's own error, with the whole token response in its text.
Example
The token has not expired by the clock, so PyDrive2 sends the request. Drive answers 401, google-auth tries to refresh inside the request, and Google refuses the refresh. Before:
After, the same error and message as
GoogleAuth.Refresh()(#437):Code that catches
pydrive2.auth.RefreshErrororAuthErrornow sees both cases. The google-auth error is kept as__cause__.Where
The four places that send Drive requests:
GoogleDriverequests such asGetAbout()They share one context manager,
_request_errors()infiles.py, which turnsHttpErrorintoApiRequestErroras before and uses the same conversion asGoogleAuth.Refresh()(_refresh_errors()inauth.py). For downloads it wraps each chunk, so a refresh that fails on a laterread()is converted too.Validation
Six live tests run against real Drive and Google's real token endpoint, with a made-up login whose token Drive rejects and whose refresh Google refuses. No credentials are needed. Each one makes a different kind of request:
GoogleAuth.Refresh()itself (the test that Leave the token response out of the refresh error message #437 added, now one of these cases)GetAbout()ls()Each expects PyDrive2's
RefreshErrorwith the(invalid_client)code and google-auth's error as its__cause__.A seventh live test reads the first 1 KiB chunk of a real file with the CI service account, then swaps the connection's credentials for an expired made-up login. The next
read()asks Google to refresh it, Google refuses, and the test expects the sameRefreshErrorand cause. That is the path of a long download whose login stops working part way. The five request cases fail on main (run locally against Google), where google-auth's error comes through.The offline suite passes with the current google-auth and with 2.15.0 (536 passed); the six live tests also pass with 2.15.0.
🤖 Generated with Claude Code