@@ -52,9 +52,12 @@ def _lookup_id(id: str, table, session):
52
52
def all_collections (self , ** kwargs ) -> Collections :
53
53
"""Read all collections from the database."""
54
54
base_url = str (kwargs ["request" ].base_url )
55
- collections = self .client .search (
56
- index = "stac_collections" , doc_type = "_doc" , query = {"match_all" : {}}
57
- )
55
+ try :
56
+ collections = self .client .search (
57
+ index = "stac_collections" , doc_type = "_doc" , query = {"match_all" : {}}
58
+ )
59
+ except elasticsearch .exceptions .NotFoundError :
60
+ raise NotFoundError ("No collections exist" )
58
61
serialized_collections = [
59
62
self .collection_serializer .db_to_stac (
60
63
collection ["_source" ], base_url = base_url
@@ -99,15 +102,16 @@ def item_collection(
99
102
"""Read an item collection from the database."""
100
103
links = []
101
104
base_url = str (kwargs ["request" ].base_url )
102
-
103
105
search = Search (using = self .client , index = "stac_items" )
104
106
105
107
collection_filter = Q (
106
108
"bool" , should = [Q ("match_phrase" , ** {"collection" : collection_id })]
107
109
)
108
110
search = search .query (collection_filter )
109
-
110
- count = search .count ()
111
+ try :
112
+ count = search .count ()
113
+ except elasticsearch .exceptions .NotFoundError :
114
+ raise NotFoundError ("No items exist" )
111
115
# search = search.sort({"id.keyword" : {"order" : "asc"}})
112
116
search = search .query ()[0 :limit ]
113
117
collection_children = search .execute ().to_dict ()
@@ -138,7 +142,9 @@ def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
138
142
try :
139
143
item = self .client .get (index = "stac_items" , id = item_id )
140
144
except elasticsearch .exceptions .NotFoundError :
141
- raise NotFoundError
145
+ raise NotFoundError (
146
+ f"Item { item_id } does not exist in Collection { collection_id } "
147
+ )
142
148
return self .item_serializer .db_to_stac (item ["_source" ], base_url )
143
149
144
150
def _return_date (self , datetime ):
@@ -316,7 +322,11 @@ def post_search(
316
322
field = sort .field + ".keyword"
317
323
search = search .sort ({field : {"order" : sort .direction }})
318
324
319
- count = search .count ()
325
+ try :
326
+ count = search .count ()
327
+ except elasticsearch .exceptions .NotFoundError :
328
+ raise NotFoundError ("No items exist" )
329
+
320
330
# search = search.sort({"id.keyword" : {"order" : "asc"}})
321
331
search = search .query ()[0 : search_request .limit ]
322
332
response = search .execute ().to_dict ()
0 commit comments