Skip to content

Commit 3878f24

Browse files
committed
robuster
1 parent 9be5ef1 commit 3878f24

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/zarr/api/asynchronous.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from zarr.core.group import AsyncGroup
1515
from zarr.core.metadata.v2 import ArrayV2Metadata
1616
from zarr.core.metadata.v3 import ArrayV3Metadata
17+
from zarr.errors import NodeTypeValidationError
1718
from zarr.storage import (
1819
StoreLike,
1920
StorePath,
@@ -247,9 +248,9 @@ async def open(
247248

248249
try:
249250
return await open_array(store=store_path, zarr_format=zarr_format, **kwargs)
250-
except (KeyError, ValueError):
251+
except (KeyError, NodeTypeValidationError):
251252
# KeyError for a missing key
252-
# ValueError for failing to parse node metadata as an array when it's
253+
# NodeTypeValidationError for failing to parse node metadata as an array when it's
253254
# actually a group
254255
return await open_group(store=store_path, zarr_format=zarr_format, **kwargs)
255256

src/zarr/core/metadata/v3.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from zarr.core.common import ZARR_JSON, parse_named_configuration, parse_shapelike
2929
from zarr.core.config import config
3030
from zarr.core.metadata.common import ArrayMetadata, parse_attributes
31+
from zarr.errors import MetadataValidationError, NodeTypeValidationError
3132
from zarr.registry import get_codec_class
3233

3334
DEFAULT_DTYPE = "float64"
@@ -36,13 +37,13 @@
3637
def parse_zarr_format(data: object) -> Literal[3]:
3738
if data == 3:
3839
return 3
39-
raise ValueError(f"Invalid value. Expected 3. Got {data}.")
40+
raise MetadataValidationError(f"Invalid value. Expected 3. Got {data}.")
4041

4142

4243
def parse_node_type_array(data: object) -> Literal["array"]:
4344
if data == "array":
4445
return "array"
45-
raise ValueError(f"Invalid value. Expected 'array'. Got {data}.")
46+
raise NodeTypeValidationError(f"Invalid value. Expected 'array'. Got {data}.")
4647

4748

4849
def parse_codecs(data: object) -> tuple[Codec, ...]:

src/zarr/errors.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ class ContainsArrayAndGroupError(_BaseZarrError):
2525
)
2626

2727

28+
class MetadataValidationError(_BaseZarrError):
29+
"""An exception raised when the Zarr metadata is invalid in some way"""
30+
31+
32+
class NodeTypeValidationError(MetadataValidationError):
33+
"""
34+
Specialized exception when the node_type of the metadata document is incorrect..
35+
36+
This can be raised when the value is invalid or unexpected given the context,
37+
for example an 'array' node when we expected a 'group'.
38+
"""
39+
40+
2841
__all__ = [
2942
"ContainsArrayAndGroupError",
3043
"ContainsArrayError",

0 commit comments

Comments
 (0)