2
votes

So, I'm trying to query cosmos collection for a specific document, with the following line of code:

FeedIterator<dynamic> querieditem = container.GetItemQueryIterator<dynamic>(mysqlquery);

When there exists an item in the database, this goes without any problems. However, when there doesn't exist any match in the database, I get the following exception:

System.Private.CoreLib: Exception while executing function: TestFunction. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId:123123; Reason: (Message: {"Errors":["Resource Not Found. Learn more: https://aka.ms/cosmosdb-tsg-not-found"]}

Does this really mean, that I need to add a try/catch, in case GetItemQueryIterator() can't find anything in Cosmos? Given that it is necessary, why does this make sense?

1

1 Answers

2
votes

Some of the methods in the SDK throw exceptions on 404 as a legacy holdover. The better alternative is to use the Stream variants of the methods, which don't throw and use HTTP status codes instead that can be used to evaluate success. You just need an extra step to deserialize the response stream yourself.

See the docs and examples for GetItemQueryStreamIterator