diff options
| author | Shivaram Lingamneni <[email protected]> | 2020-08-20 14:48:50 -0400 |
|---|---|---|
| committer | Shivaram Lingamneni <[email protected]> | 2020-08-20 14:48:50 -0400 |
| commit | 625e9787a9a287977ea2d1db0560fbd7015b50f4 (patch) | |
| tree | 053731f9b9ecb0f300d46e7c9078dcc4b3abe284 | |
| parent | 281d0d70c35c3d299c5b33e6584962e233521263 (diff) | |
review fix: handle urllib.error.HTTPError
| -rw-r--r-- | ssh_import_id/__init__.py | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/ssh_import_id/__init__.py b/ssh_import_id/__init__.py index 3cfa25d..336120d 100644 --- a/ssh_import_id/__init__.py +++ b/ssh_import_id/__init__.py @@ -30,6 +30,7 @@ import subprocess import ssl import sys import tempfile +import urllib.error from urllib.parse import quote_plus from urllib.request import Request, urlopen @@ -313,14 +314,15 @@ def fetch_keys_lp(lpid, useragent): url = "https://launchpad.net/~%s/+sshkeys" % (quote_plus(lpid)) headers = {'User-Agent': user_agent(useragent)} - with urlopen(Request(url, headers=headers), - timeout=DEFAULT_TIMEOUT) as response: - if response.status != 200: - msg = 'Requesting Launchpad keys failed.' - if response.status == 404: - msg = 'Launchpad user not found.' - die(msg + " status_code=%d user=%s" % (response.status, lpid)) - keys = response.read().decode('utf-8') + try: + with urlopen(Request(url, headers=headers), + timeout=DEFAULT_TIMEOUT) as response: + keys = response.read().decode('utf-8') + except urllib.error.HTTPError as e: + msg = 'Requesting Launchpad keys failed.' + if e.code == 404: + msg = 'Launchpad user not found.' + die(msg + " status_code=%d user=%s" % (e.code, lpid)) # pylint: disable=broad-except except Exception as e: @@ -335,19 +337,18 @@ def fetch_keys_gh(ghid, useragent): try: url = "https://api.github.com/users/%s/keys" % (quote_plus(ghid)) headers = {'User-Agent': user_agent(useragent)} - with urlopen(Request(url, headers=headers), - timeout=DEFAULT_TIMEOUT) as resp: - status = resp.status - ratelimit_header = resp.headers.get(x_ratelimit_remaining) - data = json.load(resp) - if status != 200: + try: + with urlopen(Request(url, headers=headers), + timeout=DEFAULT_TIMEOUT) as resp: + data = json.load(resp) + except urllib.error.HTTPError as e: msg = 'Requesting GitHub keys failed.' - if status == 404: + if e.code == 404: msg = 'Username "%s" not found at GitHub API.' % ghid - elif ratelimit_header == "0": + elif e.hdrs.get(x_ratelimit_remaining) == "0": msg = ('GitHub REST API rate-limited this IP address. See %s .' % help_url) - die(msg + " status_code=%d user=%s" % (resp.status_code, ghid)) + die(msg + " status_code=%d user=%s" % (e.code, ghid)) for keyobj in data: keys += "%s %s@github/%s\n" % (keyobj['key'], ghid, keyobj['id']) # pylint: disable=broad-except |
