Skip to content

Commit 98ccecc

Browse files
committed
amended runtimeerrors -> valueerror
Signed-off-by: Enoch Mok <enochmokny@gmail.com>
1 parent 51a49b0 commit 98ccecc

2 files changed

Lines changed: 10 additions & 11 deletions

File tree

monai/apps/utils.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ def download_url(
220220
HTTPError: See urllib.request.urlretrieve.
221221
ContentTooShortError: See urllib.request.urlretrieve.
222222
IOError: See urllib.request.urlretrieve.
223-
RuntimeError: When the hash validation of the ``url`` downloaded file fails.
224-
223+
ValueError: When the hash validation of the ``url`` downloaded file fails.
225224
"""
226225
if not filepath:
227226
filepath = Path(".", _basename(url)).resolve()
@@ -260,20 +259,20 @@ def download_url(
260259
raise RuntimeError(
261260
f"Download of file from {url} to {filepath} failed due to network issue or denied permission."
262261
)
262+
if not check_hash(filepath, hash_val, hash_type):
263+
raise ValueError(
264+
f"{hash_type} hash check of downloaded file failed: URL={url}, "
265+
f"filepath={filepath}, expected {hash_type}={hash_val}, "
266+
f"The file may be corrupted or tampered with. "
267+
"Please retry the download or verify the source."
268+
)
263269
file_dir = filepath.parent
264270
if file_dir:
265271
os.makedirs(file_dir, exist_ok=True)
266272
shutil.move(f"{tmp_name}", f"{filepath}") # copy the downloaded to a user-specified cache.
267273
except (PermissionError, NotADirectoryError): # project-monai/monai issue #3613 #3757 for windows
268274
pass
269275
logger.info(f"Downloaded: {filepath}")
270-
if not check_hash(filepath, hash_val, hash_type):
271-
raise ValueError(
272-
f"{hash_type} hash check of downloaded file failed: URL={url}, "
273-
f"filepath={filepath}, expected {hash_type}={hash_val}, "
274-
f"The file may be corrupted or tampered with. "
275-
"Please retry the download or verify the source."
276-
)
277276

278277

279278
def _extract_zip(filepath, output_dir):

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_download_url(self):
199199
"""Download a sample TIFF and validate hash handling.
200200
201201
Raises:
202-
RuntimeError: When the downloaded file's hash does not match.
202+
ValueError: When the downloaded file's hash does not match.
203203
"""
204204
with tempfile.TemporaryDirectory() as tempdir:
205205
with skip_if_downloading_fails():
@@ -209,7 +209,7 @@ def test_download_url(self):
209209
hash_val=SAMPLE_TIFF_HASH,
210210
hash_type=SAMPLE_TIFF_HASH_TYPE,
211211
)
212-
with self.assertRaises(RuntimeError):
212+
with self.assertRaises(ValueError):
213213
download_url(
214214
url=SAMPLE_TIFF,
215215
filepath=os.path.join(tempdir, "model_bad.tiff"),

0 commit comments

Comments
 (0)