Skip to content

Commit 019d684

Browse files
committed
show source url in error when sha is wrong
1 parent 6ce3d4b commit 019d684

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

bioimageio/spec/_internal/io.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -705,18 +705,23 @@ def _open_url(
705705
or hashlib.sha256(str(source).encode(encoding="utf-8")).hexdigest()
706706
)
707707

708-
return BytesReader(
709-
cache.fetch(
708+
try:
709+
reader = cache.fetch(
710710
source,
711711
fetcher=partial(_fetch_url, progressbar=progressbar),
712712
force_refetch=digest,
713-
),
714-
suffix=source_path.suffix,
715-
sha256=sha,
716-
original_file_name=source_path.name,
717-
original_root=source.parent,
718-
is_zipfile=None,
719-
)
713+
)
714+
except Exception as e:
715+
raise ValueError(f"Failed to fetch {source}.") from e
716+
else:
717+
return BytesReader(
718+
reader,
719+
suffix=source_path.suffix,
720+
sha256=sha,
721+
original_file_name=source_path.name,
722+
original_root=source.parent,
723+
is_zipfile=None,
724+
)
720725

721726

722727
def _fetch_url(

tests/test_internal/test_io.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from respx import MockRouter
1010

1111
from bioimageio.spec import ValidationContext
12-
from bioimageio.spec._internal.io_basics import ZipPath
12+
from bioimageio.spec._internal.io_basics import Sha256, ZipPath
13+
from bioimageio.spec._internal.url import HttpUrl
1314
from bioimageio.spec.common import RelativeFilePath
1415

1516

@@ -244,3 +245,15 @@ def test_serialize_relative_file_path_from_union():
244245
data = file_path.model_dump(mode="json")
245246

246247
assert data == path_str
248+
249+
250+
def test_open_url_with_wrong_sha():
251+
from bioimageio.spec._internal.io import (
252+
_open_url, # pyright: ignore[reportPrivateUsage]
253+
)
254+
255+
url = "https://example.com/file.txt"
256+
sha = Sha256("0" * 64) # invalid sha256 for testing
257+
258+
with pytest.raises(ValueError, match=f"Failed to fetch {url}."):
259+
_ = _open_url(HttpUrl(url), sha256=sha)

0 commit comments

Comments
 (0)