-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EventHubsException.cs
257 lines (229 loc) · 12.3 KB
/
EventHubsException.cs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Globalization;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Producer;
namespace Azure.Messaging.EventHubs
{
/// <summary>
/// Serves as a basis for exceptions produced within the Event Hubs
/// context.
/// </summary>
///
/// <seealso cref="System.Exception" />
///
public class EventHubsException : Exception
{
/// <summary>
/// Indicates whether an exception should be considered transient or final.
/// </summary>
///
/// <value><c>true</c> if the exception is likely transient; otherwise, <c>false</c>.</value>
///
public bool IsTransient { get; }
/// <summary>
/// The reason for the failure of an Event Hubs operation that resulted
/// in the exception.
/// </summary>
///
public FailureReason Reason { get; }
/// <summary>
/// The name of the Event Hubs to which the exception is associated.
/// </summary>
///
/// <value>The name of the Event Hub, if available; otherwise, <c>null</c>.</value>
///
public string EventHubName { get; }
/// <summary>
/// Gets a message that describes the current exception.
/// </summary>
///
public override string Message
{
get
{
if (string.IsNullOrEmpty(EventHubName))
{
return string.Format(CultureInfo.InvariantCulture, "{0}. {1}", base.Message, Resources.TroubleshootingGuideLink);
}
return string.Format(CultureInfo.InvariantCulture, "{0} ({1}). {2}", base.Message, EventHubName, Resources.TroubleshootingGuideLink);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
///
public EventHubsException(bool isTransient,
string eventHubName) : this(isTransient, eventHubName, null, FailureReason.GeneralError, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="reason">The reason for the failure that resulted in the exception.</param>
///
public EventHubsException(bool isTransient,
string eventHubName,
FailureReason reason) : this(isTransient, eventHubName, null, reason, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
///
public EventHubsException(bool isTransient,
string eventHubName,
string message) : this(isTransient, eventHubName, message, FailureReason.GeneralError, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="reason">The reason for the failure that resulted in the exception.</param>
///
public EventHubsException(bool isTransient,
string eventHubName,
string message,
FailureReason reason) : this(isTransient, eventHubName, message, reason, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
///
public EventHubsException(bool isTransient,
string eventHubName,
string message,
Exception innerException) : this(isTransient, eventHubName, message, FailureReason.GeneralError, innerException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class.
/// </summary>
///
/// <param name="isTransient"><c>true</c> if the exception should be considered transient; otherwise, <c>false</c>.</param>
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="reason">The reason for the failure that resulted in the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
///
public EventHubsException(bool isTransient,
string eventHubName,
string message,
FailureReason reason,
Exception innerException) : base(message, innerException)
{
IsTransient = isTransient;
EventHubName = eventHubName;
Reason = reason;
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class, using the <paramref name="reason"/>
/// to detect whether or not it should be transient.
/// </summary>
///
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="reason">The reason for the failure that resulted in the exception.</param>
///
public EventHubsException(string eventHubName,
string message,
FailureReason reason) : this(eventHubName, message, reason, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="EventHubsException"/> class, using the <paramref name="reason"/>
/// to detect whether or not it should be transient.
/// </summary>
///
/// <param name="eventHubName">The name of the Event Hub to which the exception is associated.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="reason">The reason for the failure that resulted in the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
///
internal EventHubsException(string eventHubName,
string message,
FailureReason reason,
Exception innerException) : this(false, eventHubName, message, reason, innerException)
{
switch (reason)
{
case FailureReason.ServiceCommunicationProblem:
case FailureReason.ServiceTimeout:
case FailureReason.ServiceBusy:
IsTransient = true;
break;
default:
IsTransient = false;
break;
}
}
/// <summary>
/// Converts the instance to string representation.
/// </summary>
///
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
///
public override string ToString() =>
$"{ typeof(EventHubsException).FullName }({ Reason }): { Message }{ Environment.NewLine }{ StackTrace }{ FormatInnerException() }";
/// <summary>
/// Formats the <see cref="Exception.InnerException"/> for inclusion in the <see cref="ToString" />
/// details.
/// </summary>
///
/// <returns>The text to include for the inner exception, if any.</returns>
///
private string FormatInnerException() =>
(InnerException == null) ? string.Empty : $"{ Environment.NewLine }{ InnerException }";
/// <summary>
/// The set of well-known reasons for an Event Hubs operation failure that
/// was the cause of an exception.
/// </summary>
///
public enum FailureReason
{
/// <summary>The exception was the result of a general error within the client library.</summary>
GeneralError,
/// <summary>An operation has been attempted using an Event Hubs client instance which has already been closed.</summary>
ClientClosed,
/// <summary>A client was forcefully disconnected from an Event Hub instance. This typically occurs when another consumer with higher <see cref="ReadEventOptions.OwnerLevel" /> asserts ownership over the partition and consumer group.</summary>
ConsumerDisconnected,
/// <summary>An Event Hubs resource, such as an Event Hub, consumer group, or partition cannot be found by the Event Hubs service.</summary>
ResourceNotFound,
/// <summary>A message is larger than the maximum size allowed for its transport.</summary>
MessageSizeExceeded,
/// <summary>The quota applied to an Event Hubs resource has been exceeded while interacting with the Azure Event Hubs service.</summary>
QuotaExceeded,
/// <summary>The Azure Event Hubs service reports that it is busy in response to a client request to perform an operation.</summary>
ServiceBusy,
/// <summary>An operation or other request timed out while interacting with the Azure Event Hubs service.</summary>
ServiceTimeout,
/// <summary>There was a general communications error encountered when interacting with the Azure Event Hubs service.</summary>
ServiceCommunicationProblem,
/// <summary>A client was forcefully disconnected from an Event Hub instance. This typically occurs when another consumer with higher <see cref="PartitionPublishingOptions.OwnerLevel" /> asserts ownership over the partition and producer group.</summary>
ProducerDisconnected,
/// <summary>A client is in an invalid state from which it cannot recover. It is recommended that the client be closed and recreated to force reinitialization of state.</summary>
InvalidClientState
}
}
}