diff --git a/stdpopsim/utils.py b/stdpopsim/utils.py index 447f2b1be..f4cb2ea67 100644 --- a/stdpopsim/utils.py +++ b/stdpopsim/utils.py @@ -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): diff --git a/tests/test_utils.py b/tests/test_utils.py index 6bce2e273..99ecf18e0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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: