-
Notifications
You must be signed in to change notification settings - Fork 494
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
Handle invalid JSON HTTP response from Gateway for error scenarios #4162
Comments
Doesn't it service responsibility to define a standard format of response in case of error which any SDK can understand and use (or log) the information accordingly. |
Agree that the service should behave correctly. But this change would avoid the conversation be "The SDK is throwing an exception, it's probably a problem in the SDK" which is what happened in all cases this was reported. We then found out that it was never the SDK problem. Making this change would surface the actional information that is required to course the investigation correctly. |
I am able to repro this in branch. Will work through suggested solutions. |
Clarification:
TestingTesting for empty and whitespaces. |
Blocked while waiting on approvals and feedback. |
We have received multiple reports of cases where the SDK throws a JSON serialization exception when processing HTTP responses from the Cosmos DB Gateway, for example:
Microsoft.Azure.Cosmos.GatewayStoreClient.<ParseResponseAsync>
hints as this being an HTTP response from Gateway.Microsoft.Azure.Cosmos.GatewayStoreClient.<CreateDocumentClientExceptionAsync>
means the statuscode of the HTTP response was a failure (not 200 or any success).Which means, Gateway returned a failure but the content was invalid.
SDK checks the
Content-Type
header and if it'sapplication/json
then expects the content to be valid JSON:azure-cosmos-dotnet-v3/Microsoft.Azure.Cosmos/src/GatewayStoreClient.cs
Lines 151 to 154 in 083df87
In all these cases what happened was that the content was either empty or invalid JSON.
To avoid the exception in the most common case (empty), we should check the Content-Size of the response, and if it's 0, either throw an exception saying that the response is invalid because it's empty or avoid attempting the JSON deserialization of the body and just produce the error response using the statuscode as if it had no content.
Expected outcome
application/json
but the content size is 0, skip attempting to deserialize the contentapplication/json
but the content size > 0, attempt to deserialize the content. If that fails, follow the code path that treats the content as text (non-JSON path).The text was updated successfully, but these errors were encountered: