ReadItemAsync does not throw exception as in documentation
See original GitHub issueDescribe 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:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top 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 >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
@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 ifCosmosClient.ClientOptions.Serializer
would be set to a default one, rather than keptnull
, if nothing specified, but having it inRequestOption
will allow us to reuse it for all of the read APIs, not just the document.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