Skip to content

Commit a03008a

Browse files
committed
Improve error handling for unknown data processing strategies and add test for invalid object expansion strategy
1 parent 7555333 commit a03008a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

infrahub_sdk/spec/object.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ class DataProcessorFactory:
238238
def get_processor(cls, strategy: ObjectStrategy) -> DataProcessor:
239239
processor_class = cls._processors.get(strategy)
240240
if not processor_class:
241-
raise ValueError(f"Unknown strategy: {strategy}")
241+
raise ValueError(
242+
f"Unknown strategy: {strategy} - no processor found. Valid strategies are: {list(cls._processors.keys())}"
243+
)
242244
return processor_class()
243245

244246
@classmethod
@@ -699,14 +701,20 @@ class ObjectFile(InfrahubFile):
699701
@property
700702
def spec(self) -> InfrahubObjectFileData:
701703
if not self._spec:
702-
self._spec = InfrahubObjectFileData(**self.data.spec)
704+
try:
705+
self._spec = InfrahubObjectFileData(**self.data.spec)
706+
except Exception as exc:
707+
raise ValidationError(identifier=str(self.location), message=str(exc))
703708
return self._spec
704709

705710
def validate_content(self) -> None:
706711
super().validate_content()
707712
if self.kind != InfrahubFileKind.OBJECT:
708713
raise ValueError("File is not an Infrahub Object file")
709-
self._spec = InfrahubObjectFileData(**self.data.spec)
714+
try:
715+
self._spec = InfrahubObjectFileData(**self.data.spec)
716+
except Exception as exc:
717+
raise ValidationError(identifier=str(self.location), message=str(exc))
710718

711719
async def validate_format(self, client: InfrahubClient, branch: str | None = None) -> None:
712720
self.validate_content()

tests/unit/sdk/spec/test_object.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,21 @@ async def test_get_relationship_info_tags(
228228
rel_info = await get_relationship_info(client, location_schema, "tags", data)
229229
assert rel_info.is_valid == is_valid
230230
assert rel_info.format == format
231+
232+
233+
async def test_invalid_object_expansion_strategy(
234+
client: InfrahubClient, mock_schema_query_01: HTTPXMock, location_expansion
235+
) -> None:
236+
obj = ObjectFile(location="some/path", content=location_expansion)
237+
238+
from infrahub_sdk.spec.object import DataProcessorFactory, ObjectStrategy # noqa: PLC0415
239+
240+
# Patch _processors to remove the invalid strategy
241+
original_processors = DataProcessorFactory._processors.copy()
242+
try:
243+
DataProcessorFactory._processors[ObjectStrategy.RANGE_EXPAND] = None
244+
with pytest.raises(ValueError) as exc:
245+
await obj.validate_format(client=client)
246+
assert "Unknown strategy" in str(exc.value)
247+
finally:
248+
DataProcessorFactory._processors = original_processors

0 commit comments

Comments
 (0)