Skip to content

Commit bfbd698

Browse files
Document substatus code 1003 for disambiguating 404 errors
Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com>
1 parent 54f3b2c commit bfbd698

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Exceptions.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,60 @@ Cosmos DB SDK on any IO failure will attempt to retry the failed operation if re
3333
## Common error status codes and troubleshooting guide <a id="error-codes"></a>
3434

3535
To see a list of common error code and issues please see [.NET SDK troubleshooting guide](https://docs.microsoft.com/azure/cosmos-db/troubleshoot-dot-net-sdk)
36+
37+
### Disambiguating 404 (Not Found) errors using SubStatusCode <a id="substatus-codes"></a>
38+
39+
When you receive a 404 (Not Found) status code from Cosmos DB, it can indicate two different scenarios:
40+
1. **Item not found**: The requested item doesn't exist in the container
41+
2. **Owner resource not found**: The parent resource (container or database) doesn't exist
42+
43+
To distinguish between these cases, check the `SubStatusCode` property:
44+
45+
- **SubStatusCode 0**: Regular item not found (the item doesn't exist in an existing container)
46+
- **SubStatusCode 1003**: Owner resource not found (the container or database doesn't exist)
47+
48+
#### Example with Typed APIs (throws CosmosException):
49+
50+
```csharp
51+
try
52+
{
53+
ItemResponse<MyItem> response = await container.ReadItemAsync<MyItem>("itemId", new PartitionKey("partitionKey"));
54+
}
55+
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
56+
{
57+
if (ex.SubStatusCode == 1003)
58+
{
59+
// The container or database doesn't exist
60+
Console.WriteLine("Owner resource (container/database) not found");
61+
}
62+
else
63+
{
64+
// The item doesn't exist in an existing container
65+
Console.WriteLine("Item not found");
66+
}
67+
}
68+
```
69+
70+
#### Example with Stream APIs (returns ResponseMessage):
71+
72+
```csharp
73+
ResponseMessage response = await container.ReadItemStreamAsync("itemId", new PartitionKey("partitionKey"));
74+
75+
if (response.StatusCode == HttpStatusCode.NotFound)
76+
{
77+
int subStatusCode = (int)response.Headers.SubStatusCode;
78+
79+
if (subStatusCode == 1003)
80+
{
81+
// The container or database doesn't exist
82+
Console.WriteLine("Owner resource (container/database) not found");
83+
}
84+
else
85+
{
86+
// The item doesn't exist in an existing container
87+
Console.WriteLine("Item not found");
88+
}
89+
}
90+
```
91+
92+
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).

0 commit comments

Comments
 (0)