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

CosmosOperationCanceledException: Adds short link and cancellation token status into message #3234

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Threading;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Tracing;

Expand All @@ -18,6 +19,7 @@ public class CosmosOperationCanceledException : OperationCanceledException
{
private readonly OperationCanceledException originalException;
private readonly Lazy<string> lazyMessage;
private readonly Lazy<string> toStringMessage;

/// <summary>
/// Create an instance of CosmosOperationCanceledException
Expand All @@ -31,7 +33,8 @@ public CosmosOperationCanceledException(
{
this.originalException = originalException ?? throw new ArgumentNullException(nameof(originalException));
this.Diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
this.lazyMessage = this.CreateLazyMessage();
this.lazyMessage = this.CreateLazyMessage(originalException.CancellationToken);
this.toStringMessage = this.CreateToStringMessage(originalException.CancellationToken);
}

internal CosmosOperationCanceledException(
Expand All @@ -47,7 +50,8 @@ internal CosmosOperationCanceledException(

trace.AddDatum("Operation Cancelled Exception", originalException);
this.Diagnostics = new CosmosTraceDiagnostics(trace);
this.lazyMessage = this.CreateLazyMessage();
this.lazyMessage = this.CreateLazyMessage(originalException.CancellationToken);
this.toStringMessage = this.CreateToStringMessage(originalException.CancellationToken);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -87,12 +91,17 @@ public override Exception GetBaseException()
/// <inheritdoc/>
public override string ToString()
{
return $"{this.originalException} {Environment.NewLine}CosmosDiagnostics: {this.Diagnostics}";
return this.toStringMessage.Value;
}

private Lazy<string> CreateLazyMessage()
private Lazy<string> CreateLazyMessage(CancellationToken token)
{
return new Lazy<string>(() => $"{this.originalException.Message} {Environment.NewLine}CosmosDiagnostics: {this.Diagnostics}");
return new Lazy<string>(() => $"{this.originalException.Message}{Environment.NewLine}Cancellation Token has expired: {token.IsCancellationRequested}. Learn more at: https://aka.ms/cosmosdb-tsg-request-timeout{Environment.NewLine}CosmosDiagnostics: {this.Diagnostics}");
imanvt marked this conversation as resolved.
Show resolved Hide resolved
}

private Lazy<string> CreateToStringMessage(CancellationToken token)
{
return new Lazy<string>(() => $"{this.originalException}{Environment.NewLine}Cancellation Token has expired: {token.IsCancellationRequested}. Learn more at: https://aka.ms/cosmosdb-tsg-request-timeout{Environment.NewLine}CosmosDiagnostics: {this.Diagnostics}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ StoreResponse sendDirectFunc(Uri uri, ResourceOperation resourceOperation, Docum

((MockDocumentClient)client.DocumentClient).MockGlobalEndpointManager.Verify(gep => gep.MarkEndpointUnavailableForRead(It.IsAny<Uri>()), Times.Once, "Should had marked the endpoint unavailable");
((MockDocumentClient)client.DocumentClient).MockGlobalEndpointManager.Verify(gep => gep.RefreshLocationAsync(false), Times.Once, "Should had refreshed the account information");

string expectedHelpLink = "https://aka.ms/cosmosdb-tsg-request-timeout";
string expectedCancellationTokenStatus = $"Cancellation Token has expired: {cancellationToken.IsCancellationRequested}";
Assert.IsTrue(ex.Message.Contains(expectedHelpLink));
Assert.IsTrue(ex.Message.Contains(expectedCancellationTokenStatus));
Assert.IsTrue(ex.ToString().Contains(expectedHelpLink));
Assert.IsTrue(ex.ToString().Contains(expectedCancellationTokenStatus));
}
}

Expand Down