Exception handling for repo refresh

This commit is contained in:
Ellwood Zwovic
2017-07-14 20:26:03 -07:00
parent ae5fe0abcf
commit 74f10ba3cd

View File

@@ -82,10 +82,6 @@ class InstallException(Exception):
class DownloadException(Exception):
"""Raised when there is an error downloading something"""
def __init__(self, status_code: int, message: str):
self.status_code = status_code
self.message = message
class BadRepository(Exception):
"""Raised when reading a repository results in an error"""
@@ -207,7 +203,10 @@ class Repository:
except KeyError:
pass
resp = requests.get(self.url, headers=req_headers)
try:
resp = requests.get(self.url, headers=req_headers, timeout=60)
except requests.exceptions.RequestException as err:
raise DownloadException(err) from err
try:
resp.raise_for_status()
@@ -232,7 +231,11 @@ class Repository:
self.log.debug("Found headers: %s", resp_headers)
repodict = resp.json()
try:
repodict = resp.json()
except json.decoder.JSONDecodeError:
self.log.exception("Failed to parse downloaded repository")
raise DownloadException("Could not parse repository downloaded from '%s'. Are you sure this is the correct URL?" % self.url)
repodict['_headers'] = resp_headers
self.set_from_dict(repodict)
@@ -482,7 +485,8 @@ def refresh(pipe_to_blender, storage_path: pathlib.Path, repository_url: str):
try:
repo.refresh()
except DownloadException as err:
pipe_to_blender.send(DownloadError(err.status_code, err.message))
pipe_to_blender.send(SubprocError(err))
return
repo.to_file(repo_path) # TODO: this always writes even if repo wasn't changed
pipe_to_blender.send(Result(repo.to_dict(sort=True)))