question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How could we handle ReadItemAsync if item not existed in collection

See original GitHub issue

For 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:closed
  • Created 4 years ago
  • Comments:20 (14 by maintainers)

github_iconTop GitHub Comments

5reactions
j82wcommented, Aug 14, 2019

You could do something like this:

ItemResponse<ToDoActivity> response = null;
            try
            {
                response = await this.Container.ReadItemAsync<ToDoActivity>();
            }
            catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) { }

            if(response == null)
            {

            }

if(

4reactions
j82wcommented, Aug 14, 2019

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.

T item = null;
using(ResponseMessage responseMessage = await this.Container.ReadItemStreamAsync())
{
    if(responseMessage.StatusCode == System.Net.HttpStatusCode.NotFound){
       // Do other logic
    }

    item = jsonSerializer.Deserialize<T>(responseMessage.Content);
}

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found