The SDK was designed with the following two scenarios.
This is for most users that want a simple interface that follows the normal Dot NET guidelines. Typed APIs is any method that returns the deserialized body, and can be identified as any method that does not return a ResponseMessage.
- Will throw the following exceptions types:
- CosmosException is used for all service related issues like 404 Not found
- ArgumentException
- OperationCancelledException
- CosmosException.ToString() includes the message, stack trace, and diagnostics. All of these properties are needed to troubleshoot issues
These APIs are designed for more advance scenario where perfomance is critical or it's being used in a middle tier service where a stream can just be passed through. Stream APIs return the content of the response as a stream, and can be identified as any method that returns a ResponseMessage.
- Returns a ResponseMessage instead of throwing a CosmosException for service related errors like a 404 Not Found. Throwing an exception causes unncessary overhead
- User still needs to handle the following exceptions.
- ArgumentException: This is thrown because it represent a bug in the user code, and should not be hit in production.
- OperationCancelledException: Not throwing an exception would violate the CancellationToken contract.
- ResponseMessage.IsSuccessStatusCode can be used to validate if the operation was successful
- ResponseMessage.EnsureSuccessStatusCode() will throw a CosmosException if the operation failed
- ResponseMessage.ErrorMessage contains the error message. It does not include the diagnostics.
- ResponseMessage.Diagnostics.ToString() contains information required to troubleshoot most issues.
Cosmos DB SDK on any IO failure will attempt to retry the failed operation if retry in the SDK is feasible. Having a retry in place for any failure is a good practice but specifically handling/retrying write failures is a must.
- Read and query IO failures will get retried by the SDK without surfacing them to the end user.
- Writes (Create, Upsert, Replace, Delete) are "not" idempotent and hence SDK cannot always blindly retry the failed write operations. It is a must that user's application logic to handle the failure and retry.
To see a list of common error code and issues please see .NET SDK troubleshooting guide
When you receive a 404 (Not Found) status code from Cosmos DB, it can indicate two different scenarios:
- Item not found: The requested item doesn't exist in the container
- Owner resource not found: The parent resource (container or database) doesn't exist
To distinguish between these cases, check the SubStatusCode property:
- SubStatusCode 0: Regular item not found (the item doesn't exist in an existing container)
- SubStatusCode 1003: Owner resource not found (the container or database doesn't exist)
try
{
ItemResponse<MyItem> response = await container.ReadItemAsync<MyItem>("itemId", new PartitionKey("partitionKey"));
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
if (ex.SubStatusCode == 1003)
{
// The container or database doesn't exist
Console.WriteLine("Owner resource (container/database) not found");
}
else
{
// The item doesn't exist in an existing container
Console.WriteLine("Item not found");
}
}ResponseMessage response = await container.ReadItemStreamAsync("itemId", new PartitionKey("partitionKey"));
if (response.StatusCode == HttpStatusCode.NotFound)
{
int subStatusCode = (int)response.Headers.SubStatusCode;
if (subStatusCode == 1003)
{
// The container or database doesn't exist
Console.WriteLine("Owner resource (container/database) not found");
}
else
{
// The item doesn't exist in an existing container
Console.WriteLine("Item not found");
}
}This distinction is particularly useful when implementing retry logic or error handling strategies, as you may want to handle these scenarios differently (e.g., creating the container if it doesn't exist vs. handling a missing item).