Skip to content

Raise PyDrive2's RefreshError when a refresh during a request fails - #452

Draft
shcheklein wants to merge 3 commits into
mainfrom
wrap-request-refresh-errors
Draft

shcheklein wants to merge 3 commits into
mainfrom
wrap-request-refresh-errors

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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

drive = GoogleDrive(gauth)   # signed in; the token was revoked elsewhere
drive.CreateFile({"id": file_id}).FetchMetadata()

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:

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

After, the same error and message as GoogleAuth.Refresh() (#437):

pydrive2.auth.RefreshError: Access token refresh failed: (invalid_grant) Token has been expired or revoked.

Code that catches pydrive2.auth.RefreshError or AuthError now sees both cases. The google-auth error is kept as __cause__.

Where

The four places that send Drive requests:

  • file requests: metadata, upload, copy, trash, delete, permissions and revisions
  • file listings, including the filesystem's
  • downloads
  • GoogleDrive requests such as GetAbout()

They share one context manager, _request_errors() in files.py, which turns HttpError into ApiRequestError as before and uses the same conversion as GoogleAuth.Refresh() (_refresh_errors() in auth.py). For downloads it wraps each chunk, so a refresh that fails on a later read() 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:

Each expects PyDrive2's RefreshError with 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 same RefreshError and 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

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>
Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/test/test_auth_flows.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

[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.

Comment thread pydrive2/files.py Resolved
Comment thread pydrive2/test/test_auth_flows.py Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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

Comment thread pydrive2/files.py Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the changes standardizing token-refresh error handling.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch has not been deployed

No deployments
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