Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentDb Exception failed to be deserialized #881

Open
Dudu-cmy opened this issue Nov 3, 2021 · 3 comments
Open

DocumentDb Exception failed to be deserialized #881

Dudu-cmy opened this issue Nov 3, 2021 · 3 comments

Comments

@Dudu-cmy
Copy link

Dudu-cmy commented Nov 3, 2021

Describe the bug

I encountered an exception while using azure document client to work with cosmos db. but when i should have received a normal document client exception i receive "Failed to deserialize exception from TaskActivity: {"$type":"Microsoft.Azure.Documents.DocumentClientException, Microsoft.Azure.DocumentDB.Core","Error":{"$type":"Microsoft.Azure.Documents.Error, Microsoft.Azure.DocumentDB.Core","code":"Conflict","message":"Message: {\"Errors\":[\"Resource with specified id, name, or unique index already exists.\"]}\r\nActivityId: 8f407976-9ac5-4cb2-87b0-b63b47106608, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: , SDK: Microsoft.Azure.Documents.Common/2.14.0","_ts":0}, with the root cause being Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Microsoft.Azure.Documents.DocumentClientException. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute

To Reproduce

Expected behavior

Actual behavior

Environment summary

SDK Version:
OS Version (e.g. Windows, Linux, MacOSX):

Additional context

@ealsur
Copy link
Member

ealsur commented Nov 4, 2021

Could you share what is the code that is facing this error? Any way to reproduce?

@Dudu-cmy
Copy link
Author

Dudu-cmy commented Nov 4, 2021

Sure code snippet(), but i was able to reproduce the issue with this by trying to force an exception

try{
var cosmosClient = new DocumentClient(new Uri(EndpointUri), PrimaryKey, serializerSettings: new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto });
                await cosmosClient.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId }).ConfigureAwait(false);

 var docCollection = new DocumentCollection { Id = "test", DefaultTimeToLive = -1 };
                docCollection.IndexingPolicy.IncludedPaths.Add(new IncludedPath()
                {
                    Path = "/*",
                    Indexes = new Collection<Microsoft.Azure.Documents.Index>
                        {
                            new RangeIndex(DataType.Number, -1),
                            new RangeIndex(DataType.String, -1),
                        },
                });

                await cosmosClient.CreateDocumentCollectionIfNotExistsAsync(
                            UriFactory.CreateDatabaseUri(databaseId),
                            docCollection,
                            new RequestOptions { OfferThroughput = 400 }).ConfigureAwait(false);

await cosmosClient.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(databaseId, "<test>"),
                        <Object I want to create>,
                        new Microsoft.Azure.Documents.Client.RequestOptions { PartitionKey = new PartitionKey("/LastName") })
                        .ConfigureAwait(false);
}
catch (DocumentClientException ex)
            {
throw ex;
}

Any way to reproduce

  • Create an azure function app that communicates with a cosmos db
  • Then simulate any exception forexample "Conflict" error

@ealsur
Copy link
Member

ealsur commented Nov 4, 2021

And you are just throwing the error to the caller and expect it to JSON serialize and the caller makes sense of it? How would the caller know what a DocumentClientException is?
Wouldn't it be better from a design perspective to log the exception, and then return a known contract to the caller? It could contain some of the DocumentClientException information (like the Message, StatusCode, etc) but having an explicit contract even for errors (because you could face any sort of errors, not only DocumentClientException) is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants