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

CosmosException StackTrace and InnerException in ToString() #1045

Merged
merged 4 commits into from
Nov 26, 2019
Merged
Changes from 1 commit
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
47 changes: 45 additions & 2 deletions Microsoft.Azure.Cosmos/src/Resource/CosmosException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ namespace Microsoft.Azure.Cosmos
using System;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Azure.Documents;

/// <summary>
/// The Cosmos Client exception
/// </summary>
public class CosmosException : Exception
{
private static readonly string FullName = typeof(CosmosException).FullName;
internal CosmosException(
HttpStatusCode statusCode,
string message,
Expand Down Expand Up @@ -157,8 +159,49 @@ public virtual bool TryGetHeader(string headerName, out string value)
/// <returns>A string representation of the exception.</returns>
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);
bchong95 marked this conversation as resolved.
Show resolved Hide resolved
if (this.Message != null)
{
stringBuilder.Append(" : ");
stringBuilder.Append(this.Message);
stringBuilder.AppendLine();
}

stringBuilder.AppendFormat("StatusCode = {0}", this.StatusCode);
bchong95 marked this conversation as resolved.
Show resolved Hide resolved
stringBuilder.AppendLine();

stringBuilder.AppendFormat("SubStatusCode = {0}", this.SubStatusCode);
stringBuilder.AppendLine();
bchong95 marked this conversation as resolved.
Show resolved Hide resolved

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);
j82w marked this conversation as resolved.
Show resolved Hide resolved
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(" ");
bchong95 marked this conversation as resolved.
Show resolved Hide resolved
stringBuilder.Append("--- End of inner exception stack trace ---");
}

stringBuilder.AppendLine();
stringBuilder.Append(this.StackTrace);

return stringBuilder.ToString();
bchong95 marked this conversation as resolved.
Show resolved Hide resolved
}

internal ResponseMessage ToCosmosResponseMessage(RequestMessage request)
Expand Down