Search code examples
exceptionazure-cosmosdb

Is it possible to catch Azure's DocumentClientException? Should I?


I have old code that catches Microsoft.Azure.Documents.DocumentClientException and reacts to it.
Then I updated some nuget dependencies and now DocumentClientException is internal.

The code I have catches DocumentClientException like so:

  ...
  // a-CosmosDb-call
  ...
catch (DocumentClientException de)
{
    if ((int)de.StatusCode != 429)
    {
        throw;
    }

    sleepTime = de.RetryAfter;
}

Is there a way to reuse the code above or have Microsoft changed the behaviour of CosmodDb to not publish the reason for an error?


Solution

  • If you are using Microsoft.Azure.Cosmos Nuget package, you should use CosmosException. Your code would be something like:

      ...
      // a-CosmosDb-call
      ...
    catch (CosmosException ce)
    {
        if ((int)ce.StatusCode != 429)
        {
            throw;
        }
    
        sleepTime = ce.RetryAfter;
    }