-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
AmqpExceptionHelper.cs
284 lines (235 loc) · 13.9 KB
/
AmqpExceptionHelper.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Amqp.Encoding;
using Microsoft.Azure.Amqp.Framing;
namespace Azure.Messaging.ServiceBus.Amqp
{
internal static class AmqpExceptionHelper
{
private static readonly Dictionary<string, AmqpResponseStatusCode> s_conditionToStatusMap = new Dictionary<string, AmqpResponseStatusCode>
{
{ AmqpClientConstants.TimeoutError.Value, AmqpResponseStatusCode.RequestTimeout },
{ AmqpErrorCode.NotFound.Value, AmqpResponseStatusCode.NotFound },
{ AmqpErrorCode.NotImplemented.Value, AmqpResponseStatusCode.NotImplemented },
{ AmqpClientConstants.EntityAlreadyExistsError.Value, AmqpResponseStatusCode.Conflict },
{ AmqpClientConstants.MessageLockLostError.Value, AmqpResponseStatusCode.Gone },
{ AmqpClientConstants.SessionLockLostError.Value, AmqpResponseStatusCode.Gone },
{ AmqpErrorCode.ResourceLimitExceeded.Value, AmqpResponseStatusCode.Forbidden },
{ AmqpClientConstants.NoMatchingSubscriptionError.Value, AmqpResponseStatusCode.InternalServerError },
{ AmqpErrorCode.NotAllowed.Value, AmqpResponseStatusCode.BadRequest },
{ AmqpErrorCode.UnauthorizedAccess.Value, AmqpResponseStatusCode.Unauthorized },
{ AmqpErrorCode.MessageSizeExceeded.Value, AmqpResponseStatusCode.Forbidden },
{ AmqpClientConstants.ServerBusyError.Value, AmqpResponseStatusCode.ServiceUnavailable },
{ AmqpClientConstants.ArgumentError.Value, AmqpResponseStatusCode.BadRequest },
{ AmqpClientConstants.ArgumentOutOfRangeError.Value, AmqpResponseStatusCode.BadRequest },
{ AmqpClientConstants.StoreLockLostError.Value, AmqpResponseStatusCode.Gone },
{ AmqpClientConstants.SessionCannotBeLockedError.Value, AmqpResponseStatusCode.Gone },
{ AmqpClientConstants.PartitionNotOwnedError.Value, AmqpResponseStatusCode.Gone },
{ AmqpClientConstants.EntityDisabledError.Value, AmqpResponseStatusCode.BadRequest },
{ AmqpClientConstants.PublisherRevokedError.Value, AmqpResponseStatusCode.Unauthorized },
{ AmqpClientConstants.AuthorizationFailedError.Value, AmqpResponseStatusCode.Unauthorized},
{ AmqpErrorCode.Stolen.Value, AmqpResponseStatusCode.Gone }
};
public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode)
{
object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition];
if (condition != null)
{
return (AmqpSymbol)condition;
}
// Most of the time we should have an error condition
foreach (var kvp in s_conditionToStatusMap)
{
if (kvp.Value == statusCode)
{
return kvp.Key;
}
}
return AmqpErrorCode.InternalError;
}
public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage)
{
var amqpResponseStatusCode = AmqpResponseStatusCode.Unused;
object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode];
if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue))
{
amqpResponseStatusCode = (AmqpResponseStatusCode)statusCodeValue;
}
return amqpResponseStatusCode;
}
public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode statusCode)
{
AmqpSymbol errorCondition = GetResponseErrorCondition(responseMessage, statusCode);
var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescription] as string ?? errorCondition.Value;
return ToMessagingContractException(errorCondition.Value, statusDescription);
}
public static Exception ToMessagingContractException(this Error error, bool connectionError = false)
{
if (error == null)
{
return new ServiceBusException(true, "Unknown error.");
}
return ToMessagingContractException(error.Condition.Value, error.Description, connectionError);
}
public static Exception ToMessagingContractException(string condition, string message, bool connectionError = false)
{
if (string.Equals(condition, AmqpClientConstants.TimeoutError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.ServiceTimeout);
}
if (string.Equals(condition, AmqpErrorCode.NotFound.Value, StringComparison.InvariantCultureIgnoreCase))
{
if (connectionError)
{
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem);
}
return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityNotFound);
}
if (string.Equals(condition, AmqpErrorCode.NotImplemented.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new NotSupportedException(EnrichMessage(message));
}
if (string.Equals(condition, AmqpErrorCode.NotAllowed.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new InvalidOperationException(EnrichMessage(message));
}
if (string.Equals(condition, AmqpErrorCode.UnauthorizedAccess.Value, StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(condition, AmqpClientConstants.AuthorizationFailedError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new UnauthorizedAccessException(EnrichMessage(message));
}
if (string.Equals(condition, AmqpClientConstants.ServerBusyError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.ServiceBusy);
}
if (string.Equals(condition, AmqpClientConstants.ArgumentError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ArgumentException(EnrichMessage(message));
}
if (string.Equals(condition, AmqpClientConstants.ArgumentOutOfRangeError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ArgumentOutOfRangeException(EnrichMessage(message));
}
if (string.Equals(condition, AmqpClientConstants.EntityDisabledError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityDisabled);
}
if (string.Equals(condition, AmqpClientConstants.MessageLockLostError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.MessageLockLost);
}
if (string.Equals(condition, AmqpClientConstants.EntityAlreadyExistsError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityAlreadyExists);
}
if (string.Equals(condition, AmqpClientConstants.SessionLockLostError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.SessionLockLost);
}
if (string.Equals(condition, AmqpErrorCode.ResourceLimitExceeded.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.QuotaExceeded);
}
if (string.Equals(condition, AmqpErrorCode.MessageSizeExceeded.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.MessageSizeExceeded);
}
if (string.Equals(condition, AmqpClientConstants.MessageNotFoundError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.MessageNotFound);
}
if (string.Equals(condition, AmqpClientConstants.SessionCannotBeLockedError.Value, StringComparison.InvariantCultureIgnoreCase))
{
return new ServiceBusException(message, ServiceBusFailureReason.SessionCannotBeLocked);
}
return new ServiceBusException(true, message);
}
public static Exception TranslateException(Exception exception, string referenceId = null, Exception innerException = null, bool connectionError = false)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(exception.Message);
if (referenceId != null)
{
stringBuilder.Append($"Reference: {referenceId}, {DateTime.UtcNow}");
}
var message = stringBuilder.ToString();
var aggregateException = innerException == null ? exception : new AggregateException(exception, innerException);
switch (exception)
{
case SocketException _:
message = stringBuilder.Append($" ErrorCode: {((SocketException)exception).SocketErrorCode}").ToString();
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerException: aggregateException);
case IOException _:
if (exception.InnerException is SocketException socketException)
{
message = stringBuilder.Append($" ErrorCode: {socketException.SocketErrorCode}").ToString();
}
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerException: aggregateException);
case AmqpException amqpException:
return amqpException.Error.ToMessagingContractException(connectionError);
case OperationCanceledException operationCanceledException when operationCanceledException.InnerException is AmqpException amqpException:
return amqpException.Error.ToMessagingContractException(connectionError);
case OperationCanceledException _ when connectionError:
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerException: aggregateException);
case OperationCanceledException operationCanceledException when
operationCanceledException.InnerException != null:
return operationCanceledException.InnerException;
case OperationCanceledException operationEx when !(operationEx is TaskCanceledException):
return new ServiceBusException(operationEx.Message, ServiceBusFailureReason.ServiceTimeout);
case TimeoutException _:
return new ServiceBusException(
message,
ServiceBusFailureReason.ServiceTimeout,
innerException: aggregateException);
case InvalidOperationException _ when connectionError:
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerException: aggregateException);
}
if (connectionError)
{
return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerException: aggregateException);
}
return aggregateException;
}
public static string GetTrackingId(this AmqpLink link)
{
if (link.Settings.Properties != null &&
link.Settings.Properties.TryGetValue<string>(
AmqpClientConstants.TrackingIdName,
out var trackingContext))
{
return trackingContext;
}
return null;
}
public static Exception GetInnerException(this AmqpObject amqpObject)
{
var connectionError = false;
Exception innerException;
switch (amqpObject)
{
case AmqpSession amqpSession:
innerException = amqpSession.TerminalException ?? amqpSession.Connection.TerminalException;
break;
case AmqpLink amqpLink:
connectionError = amqpLink.Session.IsClosing();
innerException = amqpLink.TerminalException ?? amqpLink.Session.TerminalException ?? amqpLink.Session.Connection.TerminalException;
break;
case RequestResponseAmqpLink amqpReqRespLink:
innerException = amqpReqRespLink.TerminalException ?? amqpReqRespLink.Session.TerminalException ?? amqpReqRespLink.Session.Connection.TerminalException;
break;
default:
return null;
}
return innerException == null ? null : TranslateException(innerException, null, null, connectionError);
}
private static string EnrichMessage(string message) => $"{message}{Environment.NewLine}{Constants.TroubleshootingMessage}";
}
}