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.

Missing documentation on how to manage a 404 when querying Cosmos

See original GitHub issue

In our program we perform LINQ queries with the SDKv3 with a helper method that looks something like the following example :

IQueryable<TEntity> cosmosQuery = _container.GetItemLinqQueryable<TEntity>(
    continuationToken: continuationToken,
    requestOptions: queryOptions
);

if (filter != null)
    cosmosQuery = cosmosQuery.Where(filter);

if (orderBy != null)
    cosmosQuery = cosmosQuery.OrderByDescending(orderBy);

var query = cosmosQuery.Select(select).ToFeedIterator();

var iterator = cosmosQuery.ToFeedIterator();
var items = new List<TEntity>();
while (iterator.HasMoreResults)
{
    var item = await iterator.ReadNextAsync();
    items.AddRange(item);
}
return items;

It happens occasionally that the ReadNextAsync raises a Cosmos Exception with StatusCode 404 and SubStatus 0. Message from ApplicationInsights:

Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: ; Reason: ();

The documentation specifies that this is expected behavior and the reason is:

The collection is no longer a resource. For example, the resource may have been deleted.

What I can’t find anywhere is how to manage this exception. Could I simply get a new Iterator?

...
var query = cosmosQuery.Select(select).ToFeedIterator();
try {
    var iterator = cosmosQuery.ToFeedIterator();
    var items = new List<TEntity>();
    while (iterator.HasMoreResults)
    {
        var item = await iterator.ReadNextAsync();
        items.AddRange(item);
    }
    return items;
}
catch (CosmosException e) when (e.StatusCode == HttpStatusCode.NotFound)
{
    var iterator = cosmosQuery.ToFeedIterator();
    var items = new List<TEntity>();
    while (iterator.HasMoreResults)
    {
        var item = await iterator.ReadNextAsync();
        items.AddRange(item);
    }
    return items;
}

Or do I need to open a new IOrderedQueryable from the Container? I know that the code is really ugly but I hope that I pointed my needs.

Thank you all!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Ettores88commented, Feb 11, 2022

@j82w the exception.ToString() is the one that I already provided: “Response status code does not indicate success: NotFound (404); Substatus: 0; ActivityId: ; Reason: ();”

I cannot get the “ResponseMessage.Diagnostics.ToString()” 'cause a CosmosException is raised from the SDK when the Response is 404. But I see that CosmosException contains a Diagnostics property so with a HOTFIX we are going to trace that out.

0reactions
j82wcommented, Jan 31, 2022

@Ettores88 can you provide either the ResponseMessage.Diagnostics.ToString() or exception.ToString() of those 404?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot Azure Cosmos DB not found exceptions
The HTTP status code 404 represents that the resource no longer exists. Expected behavior. There are many valid scenarios where an application ...
Read more >
NotFound (404) on Cosmos Query Execution - Microsoft Q&A
It means either the Database, or the Container, or the Item (depending on the operation) doesn't exist. Regards, Oury.
Read more >
Azure/azure-cosmos-js - Error 404 when deleting a document
I am trying to delete a document following the guide that is here: ... .com/en-us/azure/cosmos-db/sql-api-nodejs-get-started#DeleteIte...
Read more >
Patch request to COSMOS DB: 404 not found
I'm trying to implement the api to partially update cosmos db. I got 404 not found when I tested this patch request. I...
Read more >
Cosmos DB Response 404 - appsloveworld.com
The reason you receive a 404 is because the partition key of your updated object is not identical to the one of your...
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