-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCosmosOperationCanceledException.cs
More file actions
203 lines (180 loc) · 8.83 KB
/
CosmosOperationCanceledException.cs
File metadata and controls
203 lines (180 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Runtime.Serialization;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Telemetry;
using Microsoft.Azure.Cosmos.Tracing;
/// <summary>
/// The exception that is thrown in a thread upon cancellation of an operation that
/// the thread was executing. This extends the OperationCanceledException to include the
/// diagnostics of the operation that was canceled.
/// </summary>
[Serializable]
public class CosmosOperationCanceledException : OperationCanceledException, ICloneable
{
private readonly Object thisLock = new object();
private readonly OperationCanceledException originalException;
private readonly bool tokenCancellationRequested;
private string lazyMessage;
private string toStringMessage;
/// <summary>
/// Create an instance of CosmosOperationCanceledException
/// </summary>
/// <param name="originalException">The original operation canceled exception</param>
/// <param name="diagnostics"></param>
public CosmosOperationCanceledException(
OperationCanceledException originalException,
CosmosDiagnostics diagnostics)
: base(originalException.CancellationToken)
{
this.originalException = originalException ?? throw new ArgumentNullException(nameof(originalException));
this.Diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
this.tokenCancellationRequested = originalException.CancellationToken.IsCancellationRequested;
this.toStringMessage = null;
this.lazyMessage = null;
}
internal CosmosOperationCanceledException(
OperationCanceledException originalException,
ITrace trace)
: base(originalException.CancellationToken)
{
this.originalException = originalException ?? throw new ArgumentNullException(nameof(originalException));
if (trace == null)
{
throw new ArgumentNullException(nameof(trace));
}
using (ITrace child = trace.StartChild("CosmosOperationCanceledException"))
{
#pragma warning disable CDX1000 // DontConvertExceptionToObject
child.AddDatum("Operation Cancelled Exception", originalException);
#pragma warning restore CDX1000 // DontConvertExceptionToObject
}
this.Diagnostics = new CosmosTraceDiagnostics(trace);
this.tokenCancellationRequested = originalException.CancellationToken.IsCancellationRequested;
this.toStringMessage = null;
this.lazyMessage = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="CosmosOperationCanceledException"/> class.
/// </summary>
/// <param name="info">The SerializationInfo object that holds serialized object data for the exception being thrown.</param>
/// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param>
protected CosmosOperationCanceledException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
this.originalException = (OperationCanceledException)info.GetValue("originalException", typeof(OperationCanceledException));
this.tokenCancellationRequested = (bool)info.GetValue("tokenCancellationRequested", typeof(bool));
this.lazyMessage = null;
this.toStringMessage = null;
//Diagnostics cannot be serialized
this.Diagnostics = new CosmosTraceDiagnostics(NoOpTrace.Singleton);
}
/// <inheritdoc/>
public override string Source
{
get => this.originalException.Source;
set => this.originalException.Source = value;
}
/// <inheritdoc/>
public override string Message => this.EnsureLazyMessage();
/// <inheritdoc/>
#pragma warning disable CDX1002 // DontUseExceptionStackTrace
public override string StackTrace => this.originalException.StackTrace;
#pragma warning restore CDX1002 // DontUseExceptionStackTrace
/// <inheritdoc/>
public override IDictionary Data => this.originalException.Data;
/// <summary>
/// Gets the diagnostics for the request
/// </summary>
public CosmosDiagnostics Diagnostics { get; }
/// <inheritdoc/>
public override string HelpLink
{
get => this.originalException.HelpLink;
set => this.originalException.HelpLink = value;
}
/// <inheritdoc/>
public override Exception GetBaseException()
{
return this.originalException.GetBaseException();
}
/// <inheritdoc/>
public override string ToString()
{
return this.EnsureToStringMessage(false);
}
private string EnsureLazyMessage()
{
if (this.lazyMessage != null)
{
return this.lazyMessage;
}
lock (this.thisLock)
{
return this.lazyMessage ??=
$"{this.originalException.Message}{Environment.NewLine}Cancellation Token has expired: {this.tokenCancellationRequested}. Learn more at: https://aka.ms/cosmosdb-tsg-request-timeout{Environment.NewLine}";
}
}
internal string EnsureToStringMessage(Boolean skipDiagnostics)
{
if (this.toStringMessage != null)
{
return this.toStringMessage;
}
lock (this.thisLock)
{
if (skipDiagnostics)
{
return this.toStringMessage ??=
$"{this.originalException}{Environment.NewLine}Cancellation Token has expired: {this.tokenCancellationRequested}. Learn more at: https://aka.ms/cosmosdb-tsg-request-timeout";
}
return this.toStringMessage ??=
$"{this.originalException}{Environment.NewLine}Cancellation Token has expired: {this.tokenCancellationRequested}. Learn more at: https://aka.ms/cosmosdb-tsg-request-timeout{Environment.NewLine}CosmosDiagnostics: {this.Diagnostics}";
}
}
/// <summary>
/// RecordOtelAttributes
/// </summary>
/// <param name="exception"></param>
/// <param name="scope"></param>
internal static void RecordOtelAttributes(CosmosOperationCanceledException exception, DiagnosticScope scope)
{
scope.AddAttribute(OpenTelemetryAttributeKeys.Region,
ClientTelemetryHelper.GetContactedRegions(exception.Diagnostics?.GetContactedRegions()));
scope.AddAttribute(OpenTelemetryAttributeKeys.ExceptionMessage,
exception.GetBaseException().Message);
CosmosDbEventSource.RecordDiagnosticsForExceptions(exception.Diagnostics);
}
/// <summary>
/// Sets the System.Runtime.Serialization.SerializationInfo with information about the exception.
/// </summary>
/// <param name="info">The SerializationInfo object that holds serialized object data for the exception being thrown.</param>
/// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
#pragma warning disable CDX1000 // DontConvertExceptionToObject
info.AddValue("originalException", this.originalException);
#pragma warning restore CDX1000 // DontConvertExceptionToObject
info.AddValue("tokenCancellationRequested", this.tokenCancellationRequested);
info.AddValue("lazyMessage", this.EnsureLazyMessage());
info.AddValue("toStringMessage", this.EnsureToStringMessage(false));
}
/// <summary>
/// Creates a shallow copy of the current exception instance.
/// This ensures that the cloned exception retains the same properties but does not
/// excessively proliferate stack traces or deep-copy unnecessary objects.
/// </summary>
/// <returns>A shallow copy of the current <see cref="CosmosOperationCanceledException"/>.</returns>
public object Clone()
{
return this.MemberwiseClone();
}
}
}