Skip to content

Commit bd43e0a

Browse files
committed
Improve error message
1 parent 5161b3f commit bd43e0a

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/parcels/_core/sgrid.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,17 @@ def __eq__(self, other: Any) -> bool:
106106
)
107107

108108
@classmethod
109-
def from_attrs(cls, d: dict[str, Hashable]) -> Self:
110-
return cls(
111-
cf_role=d.get("cf_role"), # type: ignore[arg-type]
112-
topology_dimension=d.get("topology_dimension"), # type: ignore[arg-type]
113-
node_dimensions=load_mappings(d.get("node_dimensions")), # type: ignore[arg-type]
114-
face_dimensions=load_mappings(d.get("face_dimensions")), # type: ignore[arg-type]
115-
vertical_dimensions=maybe_load_mappings(d.get("vertical_dimensions")), # type: ignore[arg-type]
116-
) # type: ignore[arg-type]
109+
def from_attrs(cls, attrs):
110+
try:
111+
return cls(
112+
cf_role=attrs["cf_role"],
113+
topology_dimension=attrs["topology_dimension"],
114+
node_dimensions=load_mappings(attrs["node_dimensions"]),
115+
face_dimensions=load_mappings(attrs["face_dimensions"]),
116+
vertical_dimensions=maybe_load_mappings(attrs.get("vertical_dimensions")),
117+
)
118+
except Exception as e:
119+
raise SGridParsingException(f"Failed to parse Grid2DMetadata from {attrs=!r}") from e
117120

118121
def to_attrs(self) -> dict[str, str | int]:
119122
d = dict(
@@ -188,13 +191,16 @@ def __eq__(self, other: Any) -> bool:
188191
)
189192

190193
@classmethod
191-
def from_attrs(cls, d: dict[str, Hashable]) -> Self:
192-
return cls(
193-
cf_role=d.get("cf_role"), # type: ignore[arg-type]
194-
topology_dimension=d.get("topology_dimension"), # type: ignore[arg-type]
195-
node_dimensions=load_mappings(d["node_dimensions"]), # type: ignore[arg-type]
196-
volume_dimensions=load_mappings(d["volume_dimensions"]), # type: ignore[arg-type]
197-
)
194+
def from_attrs(cls, attrs):
195+
try:
196+
return cls(
197+
cf_role=attrs["cf_role"],
198+
topology_dimension=attrs["topology_dimension"],
199+
node_dimensions=load_mappings(attrs["node_dimensions"]),
200+
volume_dimensions=load_mappings(attrs["volume_dimensions"]),
201+
)
202+
except Exception as e:
203+
raise SGridParsingException(f"Failed to parse Grid3DMetadata from {attrs=!r}") from e
198204

199205
def to_attrs(self) -> dict[str, str | int]:
200206
return dict(
@@ -211,6 +217,9 @@ class DimDimPadding:
211217
dim2: str
212218
padding: Padding
213219

220+
def __repr__(self) -> str:
221+
return f"DimDimPadding(dim1={self.dim1!r}, dim2={self.dim2!r}, padding={self.padding!r})"
222+
214223
def __str__(self) -> str:
215224
return f"{self.dim1}:{self.dim2} (padding:{self.padding.value})"
216225

0 commit comments

Comments
 (0)