How could we handle ReadItemAsync if item not existed in collection
See original GitHub issueFor the ReadItemAsync()
method, if an item does not exist in the collection, will throw a CosmosException of status code “NotFound” (e.g, catch(CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
), so instead of implementing the business logic in the catch(...)
statement, could we just handle the item not existed case outside the catch statement but in the try statment? For example, if the ItemResponse<T>
could contain some flag to identity if the item existed or not? or is there a way just by check if (itemResponse<T>.Resource == null)
?
Thank you very much for your help!
Issue Analytics
- State:
- Created 4 years ago
- Comments:20 (14 by maintainers)
Top Results From Across the Web
Cosmos always gets 404 when doing container. ...
I have created an azure cosmosdb and manually inserted an item in the container and then query it from C# code:
Read more >Container.ReadItemAsync<T> Method
Reads a item from the Azure Cosmos service as an asynchronous operation.
Read more >Troubleshoot Azure Cosmos DB not found exceptions
Here are the possible reasons for a status code 404 to be returned if the item should exist or does exist.
Read more >Cosmos always gets 404 when doing ... - appsloveworld.com
Coding example for the question Cosmos always gets 404 when doing container.ReadItemAsync even though the item is there.
Read more >Exploring the new .NET Cosmos DB SDK (v3)
I have an existing collection with no partition key. Using ReadItemAsync, I have tried specifying null but that gives me "Value cannot be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could do something like this:
if(
This is a very long running debate. We initially started v3 out with it returning null. We got feedback from central .NET team and central SDK team that it should throw. The reason why is 404 is considered an exception according to the http standard and most developers expect it throw. We had several users report it thinking it was a bug.
If you want you can use the Stream APIs. They only throw argument exceptions, and return all other exceptions as a status code.