From d480e471bbb171be83442917bac80edb6ceb75a7 Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Fri, 22 Nov 2019 09:08:12 -0800 Subject: [PATCH 1/4] added support for stack trace and inner exception --- .../src/Resource/CosmosException.cs | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs index 05240ed67c..0737850461 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos using System; using System.IO; using System.Net; + using System.Text; using Microsoft.Azure.Documents; /// @@ -14,6 +15,7 @@ namespace Microsoft.Azure.Cosmos /// public class CosmosException : Exception { + private static readonly string FullName = typeof(CosmosException).FullName; internal CosmosException( HttpStatusCode statusCode, string message, @@ -157,8 +159,49 @@ public virtual bool TryGetHeader(string headerName, out string value) /// A string representation of the exception. public override string ToString() { - string diagnostics = this.Diagnostics != null ? this.Diagnostics.ToString() : string.Empty; - return $"{nameof(CosmosException)};StatusCode={this.StatusCode};SubStatusCode={this.SubStatusCode};ActivityId={this.ActivityId ?? string.Empty};RequestCharge={this.RequestCharge};Message={this.Message};Diagnostics{diagnostics}"; + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.Append(CosmosException.FullName); + if (this.Message != null) + { + stringBuilder.Append(" : "); + stringBuilder.Append(this.Message); + stringBuilder.AppendLine(); + } + + stringBuilder.AppendFormat("StatusCode = {0}", this.StatusCode); + stringBuilder.AppendLine(); + + stringBuilder.AppendFormat("SubStatusCode = {0}", this.SubStatusCode); + stringBuilder.AppendLine(); + + stringBuilder.AppendFormat("ActivityId = {0}", this.ActivityId ?? Guid.Empty.ToString()); + stringBuilder.AppendLine(); + + stringBuilder.AppendFormat("RequestCharge = {0}", this.RequestCharge); + stringBuilder.AppendLine(); + + stringBuilder.AppendFormat("RequestCharge = {0}", this.RequestCharge); + stringBuilder.AppendLine(); + + if (this.Diagnostics != null) + { + stringBuilder.Append(this.Diagnostics); + stringBuilder.AppendLine(); + } + + if (this.InnerException != null) + { + stringBuilder.Append(" ---> "); + stringBuilder.Append(this.InnerException); + stringBuilder.AppendLine(); + stringBuilder.Append(" "); + stringBuilder.Append("--- End of inner exception stack trace ---"); + } + + stringBuilder.AppendLine(); + stringBuilder.Append(this.StackTrace); + + return stringBuilder.ToString(); } internal ResponseMessage ToCosmosResponseMessage(RequestMessage request) From 889ab7340a79db10d25298199fae2a2787b8fa3a Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Fri, 22 Nov 2019 09:14:57 -0800 Subject: [PATCH 2/4] whoops --- Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs index 0737850461..f5c2267186 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs @@ -180,9 +180,6 @@ public override string ToString() stringBuilder.AppendFormat("RequestCharge = {0}", this.RequestCharge); stringBuilder.AppendLine(); - stringBuilder.AppendFormat("RequestCharge = {0}", this.RequestCharge); - stringBuilder.AppendLine(); - if (this.Diagnostics != null) { stringBuilder.Append(this.Diagnostics); From af32780df14732d263be76481f8b783a981ac32a Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Tue, 26 Nov 2019 11:32:42 -0800 Subject: [PATCH 3/4] resolved iteration comments --- Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs index f5c2267186..527bcaedd5 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs @@ -168,16 +168,16 @@ public override string ToString() stringBuilder.AppendLine(); } - stringBuilder.AppendFormat("StatusCode = {0}", this.StatusCode); + stringBuilder.AppendFormat("StatusCode = {0};", this.StatusCode); stringBuilder.AppendLine(); - stringBuilder.AppendFormat("SubStatusCode = {0}", this.SubStatusCode); + stringBuilder.AppendFormat("SubStatusCode = {0};", this.SubStatusCode); stringBuilder.AppendLine(); - stringBuilder.AppendFormat("ActivityId = {0}", this.ActivityId ?? Guid.Empty.ToString()); + stringBuilder.AppendFormat("ActivityId = {0};", this.ActivityId ?? Guid.Empty.ToString()); stringBuilder.AppendLine(); - stringBuilder.AppendFormat("RequestCharge = {0}", this.RequestCharge); + stringBuilder.AppendFormat("RequestCharge = {0};", this.RequestCharge); stringBuilder.AppendLine(); if (this.Diagnostics != null) @@ -193,9 +193,9 @@ public override string ToString() stringBuilder.AppendLine(); stringBuilder.Append(" "); stringBuilder.Append("--- End of inner exception stack trace ---"); + stringBuilder.AppendLine(); } - stringBuilder.AppendLine(); stringBuilder.Append(this.StackTrace); return stringBuilder.ToString(); From 65cf8e06ce9692115f0ef28522930f258b45989b Mon Sep 17 00:00:00 2001 From: Brandon Chong Date: Tue, 26 Nov 2019 12:24:23 -0800 Subject: [PATCH 4/4] updated changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index e086abae5d..5ba0cd44e5 100644 --- a/changelog.md +++ b/changelog.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#1020](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1020) Direct package update removes debug statements - [#1023](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1023) Fixed ThroughputResponse.IsReplacePending header mapping - [#1036](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1036) Fixed query responses to return null Content if it is a failure +- [#1045](https://github.com/Azure/azure-cosmos-dotnet-v3/pull/1045) Added stack trace and innner exception to CosmosException ## [3.4.1](https://www.nuget.org/packages/Microsoft.Azure.Cosmos/3.4.1) - 2019-11-06