Skip to content

Commit 2b04af5

Browse files
committed
feat(QueryList): Return MultipleObjectsReturned, ObjectDoesNotExist
1 parent 47d5b78 commit 2b04af5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/libvcs/_internal/query_list.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
no_arg = object()
1515

1616

17+
class MultipleObjectsReturned(Exception):
18+
"""The requested object does not exist"""
19+
20+
21+
class ObjectDoesNotExist(Exception):
22+
"""The query returned multiple objects when only one was expected."""
23+
24+
1725
def keygetter(
1826
obj: Mapping[str, Any],
1927
path: str,
@@ -522,9 +530,9 @@ def get(
522530
) -> Optional[T]:
523531
objs = self.filter(matcher=matcher, **kwargs)
524532
if len(objs) > 1:
525-
raise Exception("Multiple objects returned")
533+
raise MultipleObjectsReturned()
526534
elif len(objs) == 0:
527535
if default == no_arg:
528-
raise Exception("No objects found")
536+
raise ObjectDoesNotExist()
529537
return default
530538
return objs[0]

tests/_internal/test_query_list.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
import pytest
55

6-
from libvcs._internal.query_list import QueryList
6+
from libvcs._internal.query_list import (
7+
MultipleObjectsReturned,
8+
ObjectDoesNotExist,
9+
QueryList,
10+
)
711

812

913
@dataclasses.dataclass
@@ -269,16 +273,16 @@ def test_filter(
269273
else:
270274
assert qs.get(filter_expr) == expected_result[0]
271275
elif len(expected_result) > 1:
272-
with pytest.raises(Exception) as e:
276+
with pytest.raises(MultipleObjectsReturned) as e:
273277
if isinstance(filter_expr, dict):
274278
assert qs.get(**filter_expr) == expected_result
275279
else:
276280
assert qs.get(filter_expr) == expected_result
277281
assert e.match("Multiple objects returned")
278282
elif len(expected_result) == 0:
279-
with pytest.raises(Exception) as e:
283+
with pytest.raises(ObjectDoesNotExist) as exc:
280284
if isinstance(filter_expr, dict):
281285
assert qs.get(**filter_expr) == expected_result
282286
else:
283287
assert qs.get(filter_expr) == expected_result
284-
assert e.match("No objects found")
288+
assert exc.match("No objects found")

0 commit comments

Comments
 (0)