@@ -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
279278def _extract_zip (filepath , output_dir ):
0 commit comments