Skip to content

Commit 41114dd

Browse files
committed
Don't obfuscate URLError
1 parent 48c868d commit 41114dd

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

ml_datasets/test/test_util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
from urllib.error import HTTPError, URLError
3+
from ml_datasets.util import get_file
4+
5+
6+
def test_get_file_domain_resolution_fails():
7+
with pytest.raises(URLError, match="test_non_existent_file.*Name or service not known"):
8+
get_file(
9+
"non_existent_file.txt",
10+
"http://test_notexist.wth/test_non_existent_file.txt"
11+
)
12+
13+
14+
def test_get_file_404_file_not_found():
15+
with pytest.raises(HTTPError, match="test_non_existent_file.*404.*Not Found") as e:
16+
get_file(
17+
"non_existent_file.txt",
18+
"http://google.com/test_non_existent_file.txt"
19+
)
20+
assert e.value.code == 404
21+
# Suppress pytest.PytestUnraisableExceptionWarning:
22+
# Exception ignored while calling deallocator
23+
# This questionable design quirk comes from urllib.request.urlretrieve,
24+
# so we shouldn't shim around it.
25+
e.value.close()

ml_datasets/util.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ def dl_progress(count, block_size, total_size):
3737
else:
3838
progbar.update(block_size)
3939

40-
error_msg = "URL fetch failure on {}: {} -- {}"
4140
if not os.path.exists(fpath):
4241
try:
4342
try:
4443
urlretrieve(origin, fpath, dl_progress)
45-
except URLError as e:
46-
raise Exception(error_msg.format(origin, e.errno, e.reason))
44+
# Enrich download exceptions with full file name
45+
# HTTPError is a subclass of URLError, so it must be caught first
4746
except HTTPError as e:
48-
raise Exception(error_msg.format(origin, e.code, e.msg))
47+
error_msg = "URL fetch failure on {} : {} -- {}"
48+
e.msg = error_msg.format(origin, e.code, e.msg)
49+
raise
50+
except URLError as e:
51+
error_msg = "URL fetch failure on {} -- {}"
52+
e.reason = error_msg.format(origin, e.reason)
53+
raise
4954
except (Exception, KeyboardInterrupt):
5055
if os.path.exists(fpath):
5156
os.remove(fpath)

0 commit comments

Comments
 (0)