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

Preserve stack trace DocumentClientException to ResponseMessage #921

Merged
merged 10 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions Microsoft.Azure.Cosmos/src/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Microsoft.Azure.Cosmos

internal static class Extensions
{
private static readonly char[] NewLineCharacters = new[] { '\r', '\n' };

internal static ResponseMessage ToCosmosResponseMessage(this DocumentServiceResponse response, RequestMessage requestMessage)
{
Debug.Assert(requestMessage != null, nameof(requestMessage));
Expand Down Expand Up @@ -59,24 +57,7 @@ internal static ResponseMessage ToCosmosResponseMessage(this DocumentClientExcep

// if there is a status code then it came from the backend, return error as http error instead of throwing the exception
ResponseMessage cosmosResponse = new ResponseMessage(dce.StatusCode ?? HttpStatusCode.InternalServerError, request);
string reasonPhraseString = string.Empty;
if (!string.IsNullOrEmpty(dce.Message))
{
if (dce.Message.IndexOfAny(Extensions.NewLineCharacters) >= 0)
{
StringBuilder sb = new StringBuilder(dce.Message);
sb = sb.Replace("\r", string.Empty);
sb = sb.Replace("\n", string.Empty);
reasonPhraseString = sb.ToString();
}
else
{
reasonPhraseString = dce.Message;
}
}

cosmosResponse.ErrorMessage = reasonPhraseString;
cosmosResponse.Error = dce.Error;
cosmosResponse.ErrorMessage = dce.ToString();
j82w marked this conversation as resolved.
Show resolved Hide resolved
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved

if (dce.Headers != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,30 @@ public void EnsureSuccessStatusCode_ThrowsOnFailure_ContainsJsonBody()
}
}
}

[TestMethod]
public void VerifyDocumentClientExceptionToResponseMessage()
{
string errorMessage = "Test Exception!";
DocumentClientException dce = null;
try
{
throw new DocumentClientException(
message: errorMessage,
statusCode: HttpStatusCode.BadRequest,
subStatusCode: SubStatusCodes.WriteForbidden);
}
catch (DocumentClientException exception)
{
dce = exception;
}

ResponseMessage responseMessage = dce.ToCosmosResponseMessage(null);
Assert.IsFalse(responseMessage.IsSuccessStatusCode);
Assert.AreEqual(HttpStatusCode.BadRequest, responseMessage.StatusCode);
Assert.AreEqual(SubStatusCodes.WriteForbidden, responseMessage.Headers.SubStatusCode);
Assert.IsTrue(responseMessage.ErrorMessage.Contains(errorMessage));
Assert.IsTrue(responseMessage.ErrorMessage.Contains("VerifyDocumentClientExceptionToResponseMessage"), $"Message should have method name for the stack trace {responseMessage.ErrorMessage}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public void TestAggregateExceptionConverter()
ResponseMessage response = TransportHandler.AggregateExceptionConverter(ae, null);
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
Assert.IsTrue(response.ErrorMessage.StartsWith(errorMessage));
Assert.IsTrue(response.ErrorMessage.Contains(errorMessage));
}

private class SomePayload
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- [#901](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/901) Fix a bug causing query response to create a new stream for each content call
- [#918](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/918) Fixed serializer being used for Scripts, Permissions, and Conflict related iterators
- [#921](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/921) Fixed error handling to preserve stack trace in certain scenarios
- [#927](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/927) Fixed query returning partial results instead of error

## <a name="3.3.2"/> [3.3.2](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.3.2) - 2019-10-16
Expand Down