Skip to content

Commit

Permalink
fixed cannot open document missing partition key (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tentacule authored Aug 4, 2023
1 parent 5226e96 commit 99ace10
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/CosmosDbExplorer.Core/Services/CosmosDocumentService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -76,26 +75,47 @@ public async Task<CosmosQueryResult<JObject>> GetDocumentAsync(ICosmosDocument d
IfNoneMatchEtag = options.AccessCondition == CosmosAccessConditionType.IfNotMatch ? options.ETag : null,
PreTriggers = options.PreTriggers,
PostTriggers = options.PostTriggers
};
};

var partitionKey = PartitionKeyHelper.Get(document.PartitionKey);
var (response, exception) = await ReadItemAsync(document, requestOptions, partitionKey, cancellationToken);

if (response == null)
{
(response, exception) = await ReadItemAsync(document, requestOptions, PartitionKey.None, cancellationToken);

if (response == null && exception != null)
{
throw exception;
}
}

if (response != null)
{
result.RequestCharge = response.RequestCharge;
result.Items = response.Resource;
result.Headers = response.Headers.ToDictionary();
//result.Diagnostics = JObject.Parse(result.Diagnostics?.ToString());
}

return result;
}

private async Task<(ItemResponse<JObject>? response, Exception? exception)> ReadItemAsync(ICosmosDocument document, ItemRequestOptions requestOptions, PartitionKey partitionKey, CancellationToken cancellationToken)
{
try
{
var response = await _container.ReadItemAsync<JObject>(document.Id,
partitionKey: PartitionKeyHelper.Get(document.PartitionKey),
partitionKey: partitionKey,
requestOptions: requestOptions,
cancellationToken);

result.RequestCharge = response.RequestCharge;
result.Items = response.Resource;
result.Headers = response.Headers.ToDictionary();
//result.Diagnostics = JObject.Parse(result.Diagnostics?.ToString());

return result;
return (response, null);
}
catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return result;
}
return (null, ex);
}
}

public async Task<CosmosQueryResult<JObject>> SaveDocumentAsync(string content, IDocumentRequestOptions options, CancellationToken cancellationToken)
Expand Down

0 comments on commit 99ace10

Please sign in to comment.