in src/WebJobs.Extensions.CosmosDB/Bindings/CosmosDBAsyncCollector.cs [22:51]
public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
await UpsertDocument(_docDBContext, item);
}
catch (Exception ex)
{
if (CosmosDBUtility.TryGetCosmosException(ex, out CosmosException de) &&
de.StatusCode == HttpStatusCode.NotFound)
{
if (_docDBContext.ResolvedAttribute.CreateIfNotExists)
{
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(_docDBContext);
await UpsertDocument(_docDBContext, item);
}
else
{
// Throw a custom error so that it's easier to decipher.
string message = $"The container '{_docDBContext.ResolvedAttribute.ContainerName}' (in database '{_docDBContext.ResolvedAttribute.DatabaseName}') does not exist. To automatically create the container, set '{nameof(CosmosDBAttribute.CreateIfNotExists)}' to 'true'.";
throw new InvalidOperationException(message, ex);
}
}
else
{
throw;
}
}
}