Skip to content

Commit

Permalink
Merge pull request #1285 from grahamgower/download-error-message
Browse files Browse the repository at this point in the history
Print url when download fails
  • Loading branch information
grahamgower committed Jun 1, 2022
2 parents f782e49 + 117e9d7 commit f76b1c7
Show file tree
Hide file tree
Showing 2 changed files with 10 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
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ def test_download_nonexistent(self):
input_filename.resolve().as_uri(), # local file
"http://example.com/nonexistant", # remote file
):
with pytest.raises(OSError):
with pytest.raises(OSError, match="nonexistant"):
utils.download(url, output_filename)
assert not (output_filename.exists())
assert not output_filename.exists()


class TestSha256:
Expand Down

0 comments on commit f76b1c7

Please sign in to comment.