fix(gmail-oauth-draft): wrap api_get with HTTPError handler for clean failure surface#240
Merged
Merged
Conversation
… failure surface
`api_get` was the only `urllib.request.urlopen` call in the package
without a `try/except urllib.error.HTTPError`. `api_post`,
`mark_threads_read` (both calls), and the token refresh in
`credentials.py` all wrap the call and raise `SystemExit` with a
one-line `"Gmail API <path> failed (<code>): <body>"` message.
`api_get` is the path that `latest_reply_headers` uses, so any
401 / 403 / 404 from `GET /threads/{id}?format=full` surfaced as a
raw Python traceback instead of the clean error the rest of the
tool produces. That path is exercised on every
`oauth-draft-create --thread-id <X>` invocation that does not pass
`--no-reply-headers`.
Wraps `api_get` with the same handler as `api_post` and adds a
regression test mirroring `test_api_post_raises_on_http_error`.
potiuk
approved these changes
May 20, 2026
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.
What
api_getwas the onlyurllib.request.urlopencall in theoauth-draftpackage without atry/except urllib.error.HTTPError.api_post, both calls inmark_threads_read, and the token refresh incredentials.pyall wrap the call and raiseSystemExitwith a one-line"Gmail API <path> failed (<code>): <body>"message.Impact
api_getis the path thatlatest_reply_headersuses, so any 401 / 403 / 404 fromGET /threads/{id}?format=full(token expired, thread missing, missing scope) surfaced as a raw Python traceback instead of the clean error the rest of the tool produces. That path is exercised on everyoauth-draft-create --thread-id <X>invocation that does not pass--no-reply-headers.Fix
Wraps
api_getwith the same handler asapi_postand adds a regression test mirroringtest_api_post_raises_on_http_error.Changes
src/oauth_draft/create_draft.py—api_getwrapped withtry/except urllib.error.HTTPErrortests/test_create_draft.py— regression test assertingapi_getraisesSystemExitwith thefailed (<code>)shape on a Gmail 404Validation
pytest: 59 passedruff check/ruff format/mypy: cleanprek: all hooks pass