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.

ReadItemAsync does not throw exception as in documentation

See original GitHub issue

Describe the bug The MS docs state that when ReadItemAsync is ran on an entity that does not exist, a 404 exception should be returned. Link to MS docs.

In my testing, no exception is thrown but the CosmosItemResponse<T> is returned with an Resource = null and a StatusCode = 404.

To Reproduce Have Cosmos SQL API Collection named tweets with partitionKey /subjectId.

using Microsoft.Azure.Cosmos;
using cosmos.test.Models;
using Xunit;

namespace cosmos.test
{
    public class CosmosTest
    {
        private const string _endpoint = "{endpoint}";
        private const string _authkey = "{authkey}";
        private const string _database = "{database}";

       [Fact]
        public async void ReadFail()
        {
            var client = new CosmosClient(_endpoint, _authkey);

            await Assert.ThrowsAsync<CosmosException>(async () =>
            {
                await client.Databases[_database].Containers["tweets"].Items.ReadItemAsync<Tweet>("foo", "not-a-real-id");
            });
        }
    }
}

Expected behavior An exception of type CosmosException with StatusCode = 404 should be thrown.

Actual behavior No exception is thrown.

Environment summary SDK Version: 3.0.0.9-preview OS Version (e.g. Windows, Linux, MacOSX) MacOSX

Additional context

$ dotnet test
Build started, please wait...
Build completed.

Test run for /Users/mpalumbo7/Code/tweet-api/test/cosmost.test/bin/Debug/netcoreapp2.2/tweet.api.test.dll(.NETCoreApp,Version=v2.2)
Microsoft (R) Test Execution Command Line Tool Version 15.9.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
[xUnit.net 00:00:06.0785620]     cosmos.test.CosmosTest.ReadFail [FAIL]
Failed   cosmos.test.CosmosTest.ReadFail
Error Message:
 Assert.Throws() Failure
Expected: typeof(Microsoft.Azure.Cosmos.CosmosException)
Actual:   (No exception was thrown)
Stack Trace:
   at cosmos.test.CosmosTest.ReadFail() in /Users/mpalumbo7/Code/tweet-api/test/cosmos.test/CosmosTest.cs:line 20

Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 6.7763 Seconds

Issue Analytics

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

github_iconTop GitHub Comments

5reactions
nahk-ivanovcommented, Jan 28, 2020

@j82w can we have this as an option in RequestOptions or a separate flag? Performance is a concern and to use stream APIs we need to pass serializer everywhere. It would probably improve ever so slightly if CosmosClient.ClientOptions.Serializer would be set to a default one, rather than kept null, if nothing specified, but having it in RequestOption will allow us to reuse it for all of the read APIs, not just the document.

3reactions
claitonlovatocommented, Aug 22, 2019

Hi @j82w , I am facing the opposite problem here, I am using 3.1.1 (downgraded to 3.0.0 with same results), and ReadItemAsync actually throws an exception instead of returning a ItemResponse as described on the sample documentation. Any ideas? Thank you so much. Claiton

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cosmos always gets 404 when doing container. ...
ReadItemAsync even though the item is there. The code always returns 404 NotFound! But I can see the item in the container from...
Read more >
Container.ReadItemAsync<T> Method
A Task containing a ItemResponse<T> which wraps the read resource record. Exceptions. CosmosException. This exception can encapsulate many different types of ...
Read more >
Azure Cosmos DB Basics
var iterator = query.ToFeedIterator(); var cosmosResponse = await iterator.ReadNextAsync();. But it throws an error "An expression tree may not ...
Read more >
Cosmos DB EF ReadItemAsync exception occurs Response ...
Coding example for the question Cosmos DB EF ReadItemAsync exception occurs Response status code does not indicate success: Unauthorized (401);
Read more >
Mobile SDK for Xamarin - Web API 1.0
The Mobile SDK does not cache the content downloaded from a Sitecore instance. ... An incorrect request that throws InvalidOperationException:.
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