| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.IO; |
| | 8 | | using System.Net.Sockets; |
| | 9 | | using System.Text; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Microsoft.Azure.Amqp; |
| | 12 | | using Microsoft.Azure.Amqp.Encoding; |
| | 13 | | using Microsoft.Azure.Amqp.Framing; |
| | 14 | |
|
| | 15 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 16 | | { |
| | 17 | | internal static class AmqpExceptionHelper |
| | 18 | | { |
| 0 | 19 | | private static readonly Dictionary<string, AmqpResponseStatusCode> s_conditionToStatusMap = new Dictionary<strin |
| 0 | 20 | | { |
| 0 | 21 | | { AmqpClientConstants.TimeoutError.Value, AmqpResponseStatusCode.RequestTimeout }, |
| 0 | 22 | | { AmqpErrorCode.NotFound.Value, AmqpResponseStatusCode.NotFound }, |
| 0 | 23 | | { AmqpErrorCode.NotImplemented.Value, AmqpResponseStatusCode.NotImplemented }, |
| 0 | 24 | | { AmqpClientConstants.EntityAlreadyExistsError.Value, AmqpResponseStatusCode.Conflict }, |
| 0 | 25 | | { AmqpClientConstants.MessageLockLostError.Value, AmqpResponseStatusCode.Gone }, |
| 0 | 26 | | { AmqpClientConstants.SessionLockLostError.Value, AmqpResponseStatusCode.Gone }, |
| 0 | 27 | | { AmqpErrorCode.ResourceLimitExceeded.Value, AmqpResponseStatusCode.Forbidden }, |
| 0 | 28 | | { AmqpClientConstants.NoMatchingSubscriptionError.Value, AmqpResponseStatusCode.InternalServerError }, |
| 0 | 29 | | { AmqpErrorCode.NotAllowed.Value, AmqpResponseStatusCode.BadRequest }, |
| 0 | 30 | | { AmqpErrorCode.UnauthorizedAccess.Value, AmqpResponseStatusCode.Unauthorized }, |
| 0 | 31 | | { AmqpErrorCode.MessageSizeExceeded.Value, AmqpResponseStatusCode.Forbidden }, |
| 0 | 32 | | { AmqpClientConstants.ServerBusyError.Value, AmqpResponseStatusCode.ServiceUnavailable }, |
| 0 | 33 | | { AmqpClientConstants.ArgumentError.Value, AmqpResponseStatusCode.BadRequest }, |
| 0 | 34 | | { AmqpClientConstants.ArgumentOutOfRangeError.Value, AmqpResponseStatusCode.BadRequest }, |
| 0 | 35 | | { AmqpClientConstants.StoreLockLostError.Value, AmqpResponseStatusCode.Gone }, |
| 0 | 36 | | { AmqpClientConstants.SessionCannotBeLockedError.Value, AmqpResponseStatusCode.Gone }, |
| 0 | 37 | | { AmqpClientConstants.PartitionNotOwnedError.Value, AmqpResponseStatusCode.Gone }, |
| 0 | 38 | | { AmqpClientConstants.EntityDisabledError.Value, AmqpResponseStatusCode.BadRequest }, |
| 0 | 39 | | { AmqpClientConstants.PublisherRevokedError.Value, AmqpResponseStatusCode.Unauthorized }, |
| 0 | 40 | | { AmqpClientConstants.AuthorizationFailedError.Value, AmqpResponseStatusCode.Unauthorized}, |
| 0 | 41 | | { AmqpErrorCode.Stolen.Value, AmqpResponseStatusCode.Gone } |
| 0 | 42 | | }; |
| | 43 | |
|
| | 44 | | public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode) |
| | 45 | | { |
| 0 | 46 | | object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition]; |
| 0 | 47 | | if (condition != null) |
| | 48 | | { |
| 0 | 49 | | return (AmqpSymbol)condition; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | // Most of the time we should have an error condition |
| 0 | 53 | | foreach (var kvp in s_conditionToStatusMap) |
| | 54 | | { |
| 0 | 55 | | if (kvp.Value == statusCode) |
| | 56 | | { |
| 0 | 57 | | return kvp.Key; |
| | 58 | | } |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | return AmqpErrorCode.InternalError; |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage) |
| | 65 | | { |
| 0 | 66 | | var amqpResponseStatusCode = AmqpResponseStatusCode.Unused; |
| 0 | 67 | | object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode] |
| 0 | 68 | | if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue)) |
| | 69 | | { |
| | 70 | |
|
| 0 | 71 | | amqpResponseStatusCode = (AmqpResponseStatusCode)statusCodeValue; |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | return amqpResponseStatusCode; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode st |
| | 78 | | { |
| 0 | 79 | | AmqpSymbol errorCondition = GetResponseErrorCondition(responseMessage, statusCode); |
| 0 | 80 | | var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescrip |
| 0 | 81 | | return ToMessagingContractException(errorCondition.Value, statusDescription); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public static Exception ToMessagingContractException(this Error error, bool connectionError = false) |
| | 85 | | { |
| 6 | 86 | | if (error == null) |
| | 87 | | { |
| 0 | 88 | | return new ServiceBusException(true, "Unknown error."); |
| | 89 | | } |
| | 90 | |
|
| 6 | 91 | | return ToMessagingContractException(error.Condition.Value, error.Description, connectionError); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | public static Exception ToMessagingContractException(string condition, string message, bool connectionError = fa |
| | 95 | | { |
| 6 | 96 | | if (string.Equals(condition, AmqpClientConstants.TimeoutError.Value, StringComparison.InvariantCultureIgnore |
| | 97 | | { |
| 0 | 98 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceTimeout); |
| | 99 | | } |
| | 100 | |
|
| 6 | 101 | | if (string.Equals(condition, AmqpErrorCode.NotFound.Value, StringComparison.InvariantCultureIgnoreCase)) |
| | 102 | | { |
| 0 | 103 | | if (connectionError) |
| | 104 | | { |
| 0 | 105 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem); |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityNotFound); |
| | 109 | | } |
| | 110 | |
|
| 6 | 111 | | if (string.Equals(condition, AmqpErrorCode.NotImplemented.Value, StringComparison.InvariantCultureIgnoreCase |
| | 112 | | { |
| 0 | 113 | | return new NotSupportedException(message); |
| | 114 | | } |
| | 115 | |
|
| 6 | 116 | | if (string.Equals(condition, AmqpErrorCode.NotAllowed.Value, StringComparison.InvariantCultureIgnoreCase)) |
| | 117 | | { |
| 0 | 118 | | return new InvalidOperationException(message); |
| | 119 | | } |
| | 120 | |
|
| 6 | 121 | | if (string.Equals(condition, AmqpErrorCode.UnauthorizedAccess.Value, StringComparison.InvariantCultureIgnore |
| 6 | 122 | | string.Equals(condition, AmqpClientConstants.AuthorizationFailedError.Value, StringComparison.InvariantC |
| | 123 | | { |
| 0 | 124 | | return new ServiceBusException(message, ServiceBusFailureReason.Unauthorized); |
| | 125 | | } |
| | 126 | |
|
| 6 | 127 | | if (string.Equals(condition, AmqpClientConstants.ServerBusyError.Value, StringComparison.InvariantCultureIgn |
| | 128 | | { |
| 4 | 129 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceBusy); |
| | 130 | | } |
| | 131 | |
|
| 2 | 132 | | if (string.Equals(condition, AmqpClientConstants.ArgumentError.Value, StringComparison.InvariantCultureIgnor |
| | 133 | | { |
| 2 | 134 | | return new ArgumentException(message); |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | if (string.Equals(condition, AmqpClientConstants.ArgumentOutOfRangeError.Value, StringComparison.InvariantCu |
| | 138 | | { |
| 0 | 139 | | return new ArgumentOutOfRangeException(message); |
| | 140 | | } |
| | 141 | |
|
| 0 | 142 | | if (string.Equals(condition, AmqpClientConstants.EntityDisabledError.Value, StringComparison.InvariantCultur |
| | 143 | | { |
| 0 | 144 | | return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityDisabled); |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (string.Equals(condition, AmqpClientConstants.MessageLockLostError.Value, StringComparison.InvariantCultu |
| | 148 | | { |
| 0 | 149 | | return new ServiceBusException(message, ServiceBusFailureReason.MessageLockLost); |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | if (string.Equals(condition, AmqpClientConstants.EntityAlreadyExistsError.Value, StringComparison.InvariantC |
| | 153 | | { |
| 0 | 154 | | return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityAlreadyExists); |
| | 155 | | } |
| | 156 | |
|
| 0 | 157 | | if (string.Equals(condition, AmqpClientConstants.SessionLockLostError.Value, StringComparison.InvariantCultu |
| | 158 | | { |
| 0 | 159 | | return new ServiceBusException(message, ServiceBusFailureReason.SessionLockLost); |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | if (string.Equals(condition, AmqpErrorCode.ResourceLimitExceeded.Value, StringComparison.InvariantCultureIgn |
| | 163 | | { |
| 0 | 164 | | return new ServiceBusException(message, ServiceBusFailureReason.QuotaExceeded); |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | if (string.Equals(condition, AmqpErrorCode.MessageSizeExceeded.Value, StringComparison.InvariantCultureIgnor |
| | 168 | | { |
| 0 | 169 | | return new ServiceBusException(message, ServiceBusFailureReason.MessageSizeExceeded); |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | if (string.Equals(condition, AmqpClientConstants.MessageNotFoundError.Value, StringComparison.InvariantCultu |
| | 173 | | { |
| 0 | 174 | | return new ServiceBusException(message, ServiceBusFailureReason.MessageNotFound); |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | if (string.Equals(condition, AmqpClientConstants.SessionCannotBeLockedError.Value, StringComparison.Invarian |
| | 178 | | { |
| 0 | 179 | | return new ServiceBusException(message, ServiceBusFailureReason.SessionCannotBeLocked); |
| | 180 | | } |
| | 181 | |
|
| 0 | 182 | | return new ServiceBusException(true, message); |
| | 183 | | } |
| | 184 | |
|
| | 185 | | public static Exception TranslateException(Exception exception, string referenceId = null, Exception innerExcept |
| | 186 | | { |
| 72 | 187 | | var stringBuilder = new StringBuilder(); |
| 72 | 188 | | stringBuilder.AppendFormat(CultureInfo.InvariantCulture, exception.Message); |
| 72 | 189 | | if (referenceId != null) |
| | 190 | | { |
| 0 | 191 | | stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $"Reference: {referenceId}, {DateTime.UtcNow}") |
| | 192 | | } |
| | 193 | |
|
| 72 | 194 | | var message = stringBuilder.ToString(); |
| 72 | 195 | | var aggregateException = innerException == null ? exception : new AggregateException(exception, innerExcepti |
| | 196 | |
|
| 72 | 197 | | switch (exception) |
| | 198 | | { |
| | 199 | | case SocketException _: |
| 0 | 200 | | message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {((SocketException) |
| 0 | 201 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx |
| | 202 | |
|
| | 203 | | case IOException _: |
| 0 | 204 | | if (exception.InnerException is SocketException socketException) |
| | 205 | | { |
| 0 | 206 | | message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {socketExceptio |
| | 207 | | } |
| 0 | 208 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx |
| | 209 | |
|
| | 210 | | case AmqpException amqpException: |
| 0 | 211 | | return amqpException.Error.ToMessagingContractException(connectionError); |
| | 212 | |
|
| 18 | 213 | | case OperationCanceledException operationCanceledException when operationCanceledException.InnerExceptio |
| 2 | 214 | | return amqpException.Error.ToMessagingContractException(connectionError); |
| | 215 | |
|
| 16 | 216 | | case OperationCanceledException _ when connectionError: |
| 0 | 217 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx |
| | 218 | |
|
| 16 | 219 | | case OperationCanceledException operationCanceledException when |
| 16 | 220 | | operationCanceledException.InnerException != null: |
| 2 | 221 | | return operationCanceledException.InnerException; |
| | 222 | |
|
| 14 | 223 | | case OperationCanceledException operationEx when !(operationEx is TaskCanceledException): |
| 10 | 224 | | return new ServiceBusException(operationEx.Message, ServiceBusFailureReason.ServiceTimeout); |
| | 225 | |
|
| | 226 | | case TimeoutException _: |
| 0 | 227 | | return new ServiceBusException( |
| 0 | 228 | | message, |
| 0 | 229 | | ServiceBusFailureReason.ServiceTimeout, |
| 0 | 230 | | innerException: aggregateException); |
| | 231 | |
|
| 0 | 232 | | case InvalidOperationException _ when connectionError: |
| 0 | 233 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx |
| | 234 | | } |
| | 235 | |
|
| 58 | 236 | | if (connectionError) |
| | 237 | | { |
| 0 | 238 | | return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerExcept |
| | 239 | | } |
| | 240 | |
|
| 58 | 241 | | return aggregateException; |
| | 242 | | } |
| | 243 | |
|
| | 244 | | public static string GetTrackingId(this AmqpLink link) |
| | 245 | | { |
| 0 | 246 | | if (link.Settings.Properties != null && |
| 0 | 247 | | link.Settings.Properties.TryGetValue<string>( |
| 0 | 248 | | AmqpClientConstants.TrackingIdName, |
| 0 | 249 | | out var trackingContext)) |
| | 250 | | { |
| 0 | 251 | | return trackingContext; |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | return null; |
| | 255 | | } |
| | 256 | |
|
| | 257 | | public static Exception GetInnerException(this AmqpObject amqpObject) |
| | 258 | | { |
| 0 | 259 | | var connectionError = false; |
| | 260 | | Exception innerException; |
| | 261 | | switch (amqpObject) |
| | 262 | | { |
| | 263 | | case AmqpSession amqpSession: |
| 0 | 264 | | innerException = amqpSession.TerminalException ?? amqpSession.Connection.TerminalException; |
| 0 | 265 | | break; |
| | 266 | |
|
| | 267 | | case AmqpLink amqpLink: |
| 0 | 268 | | connectionError = amqpLink.Session.IsClosing(); |
| 0 | 269 | | innerException = amqpLink.TerminalException ?? amqpLink.Session.TerminalException ?? amqpLink.Sessio |
| 0 | 270 | | break; |
| | 271 | |
|
| | 272 | | case RequestResponseAmqpLink amqpReqRespLink: |
| 0 | 273 | | innerException = amqpReqRespLink.TerminalException ?? amqpReqRespLink.Session.TerminalException ?? a |
| 0 | 274 | | break; |
| | 275 | |
|
| | 276 | | default: |
| 0 | 277 | | return null; |
| | 278 | | } |
| | 279 | |
|
| 0 | 280 | | return innerException == null ? null : TranslateException(innerException, null, null, connectionError); |
| | 281 | | } |
| | 282 | | } |
| | 283 | | } |