Skip to content

Commit

Permalink
[TOOLCM-2168] Catch missing link header (#45)
Browse files Browse the repository at this point in the history
* Catch missing link header

In Python 2, `response.headers["header-key"]` would return a `KeyError` if the header wasn't present, but in Python 3 it returns `None`. This is currently the top source of errors in Sentry.

* Add comment

* Update comment

* Fix if-else indentation
  • Loading branch information
joetrollo authored Jul 8, 2024
1 parent 4ce4446 commit 35b29b7
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/github_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,16 @@ def get_repos(org, repo_type, access_token=None, username=None, password=None, p
raise ValueError('unworkable combination of authentication inputs')

response = urlopen(request)
try:
pagination = get_pagination(response.headers['Link'])
except KeyError:
# response.headers is an instance of http.client.HTTPMessage, which returns `None`
# for missing keys. Refer to email.message.Message.__getitem__ (currently at
# https://github.com/python/cpython/blob/3.11/Lib/email/message.py#L410-L419)
# for the specific implementation.
raw_link_header = response.headers['Link']
if raw_link_header is None:
logging.debug('no Link header, nothing to paginate through.')
pagination = Pagination(None, None, None, None)
else:
pagination = get_pagination(raw_link_header)

repos = json.loads(response.read())
for r in repos:
Expand Down

0 comments on commit 35b29b7

Please sign in to comment.