title: Determine if Cosmos DB NotFound due to missing collection vs. document
body: <p>Is there a way to programmatically determine from a <code>DocumentClientException</code> where <code>StatusCode == HttpStatusCode.NotFound</code> whether it was the document, the collection, or the database that was not found?</p>
  
  <p>I'm trying to figure out whether I can implement on-demand collection provisioning and only call <code>DocumentClient.CreateDocumentCollectionIfNotExistsAsync</code> when I need to. I'm trying to avoid calling it before making every request (presumably this adds an extra network roundtrip to every request). Likewise, I'm trying to avoid calling it on error recovery when I know it won't help.</p>
  
  <p>From experimentation with the local emulator, the only field I see varying in these three cases is <code>DocumentClientException.Error.Message</code>, and only when the database cannot be found. I generally try to avoid exception dispatching based on human-readable messages.</p>
  
  <p>Wrong database name:</p>
  
  <ul>
  <li><code>StatusCode</code>: <code>HttpStatusCode.NotFound</code></li>
  <li><code>Error.Message</code>: <code>{\"Errors\":[\"Owner resource does not exist\"]}...</code></li>
  </ul>
  
  <p>Correct database name, wrong collection name:</p>
  
  <ul>
  <li><code>StatusCode</code>: <code>HttpStatusCode.NotFound</code></li>
  <li><code>Error.Message</code>: <code>{\"Errors\":[\"Resource Not Found\"]}...</code></li>
  </ul>
  
  <p>Correct database name, correct collection name, incorrect document ID:</p>
  
  <ul>
  <li><code>StatusCode</code>: <code>HttpStatusCode.NotFound</code></li>
  <li><code>Error.Message</code>: <code>{\"Errors\":[\"Resource Not Found\"]}...</code></li>
  </ul>
  
  <p>I'm planning to use a database with its own offer. Since collections inside a database with its own offer are cheap, I'm trying to see whether I <a href="https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data#partitioning-and-multitenancy" rel="nofollow noreferrer">can segregate each tenant in my multi-tenant application into its own collection</a>. Each tenant ends up having a different indexing and default TTL policy. The set of collections is not fixed and changes dynamically during runtime as new tenants sign up. I cannot predict when I will need to add a new collection. There's no new tenant notification: I just get a request that I need to handle by creating a document in a possibly non-existent collection. There's a process to garbage collect unused collections.</p>
  
  <p>I'm using the NuGet package Microsoft.Azure.DocumentDB.Core Version 1.9.1 in a .NET Core 2.1 app targeting a SQL API Cosmos DB instance.</p>