Skip to content

Commit

Permalink
Print url when download fails
Browse files Browse the repository at this point in the history
Closes #697.
  • Loading branch information
grahamgower committed Jun 1, 2022
1 parent f782e49 commit 2d53942
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 8 additions & 4 deletions stdpopsim/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ def download(url, filename):
"""
Download url to the specified local file.
"""
# TODO: what is a sensible timeout here?
with urllib.request.urlopen(url, timeout=30) as f_in:
with open(filename, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
try:
with urllib.request.urlopen(url, timeout=30) as f_in:
with open(filename, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
except urllib.error.HTTPError as e:
# Amend the error message to include the url.
e.msg += f": {url}"
raise e


def sha256(filename):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,12 @@ def test_download_nonexistent(self):
input_filename.resolve().as_uri(), # local file
"http://example.com/nonexistant", # remote file
):
with pytest.raises(OSError):
error_match = url
if error_match.startswith("file:///"):
error_match = error_match[len("file:///") :]
with pytest.raises(OSError, match=error_match):
utils.download(url, output_filename)
assert not (output_filename.exists())
assert not output_filename.exists()


class TestSha256:
Expand Down

0 comments on commit 2d53942

Please sign in to comment.