Description
openedon Oct 11, 2023
We are continuously addressing and improving the SDK, if possible, make sure the problem persist in the latest SDK version.
Describe the bug
When trying to read an item that does not exist using ReadItemAsync or ReadItemStreamAsync, Cosmos SDK throws a serialization exception rather than a CosmosException with StatusCode NotFound.
I found that this is because 1) We set the default json serializer settings MissingMemberHandling to Error 2) cosmos sdk uses JToken.Parse (and not the custom serializer passed into cosmos client) thereby using the default json serializer settings 3) There is an extra property ‘Errors’ on the exception the sdk is trying to convert to a CosmosException.
To Reproduce
One serializer to deserialize successfully retrieved items -- we can customize this serializer when cosmosClient is created.
ReadItemAsync -> ResponseFactory.CreateItemResponse -> ToObjectPublic
A different serializer - the default Json serializer - is used to deserialize exceptions.
We set JsonConvert.DefaultSettings.MissingMemberHandling = Error at service host level.
When we ReadItemAsync and the item is not found, we expect the cosmos SDK to throw a NotFound exception. Instead, it throws a serialization exception
This is because in the ReadItemAsync code path, it tries to create a NotFound CosmosException from the response, but fails to deserialze the response into a CosmosException because it has a property called 'Errors' that does not exist in the CosmosException definition.
ReadItemStreamAsync-> ProcessItemStreamAsync -> ProcessResourceOperationStreamAsync -> RequestHandler.SendAsync … -> ToCosmosResponseMessage -> Create CosmosException if !IsSuccessStatusCode -> deserialize exception
Expected behavior
A CosmosException with StatusCode NotFound should be thrown when ReadItemAsync cannot find the item.
Actual behavior
A serialization exception Newtonsoft.Json.JsonSerializationException: Could not find member 'Errors' on object of type 'Error'. Path 'Errors', line 1, position 10.
Environment summary
SDK Version: v3
OS Version (e.g. Windows, Linux, MacOSX)
Additional context
Add any other context about the problem here (for example, complete stack traces or logs).
- What SDK version are you using? v3
- What Newtonsoft.Json version are you using? 13.0.0.0
- Are you customizing any other default setting? We set JsonConvert.DefaultSettings.ContractResolver to CamelCasePropertyNamesContractResolver
Metadata
Assignees
Type
Projects
Status
Done