< Summary

Class:Azure.Messaging.ServiceBus.Amqp.AmqpExceptionHelper
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpExceptionHelper.cs
Covered lines:28
Uncovered lines:98
Coverable lines:126
Total lines:283
Line coverage:22.2% (28 of 126)
Covered branches:28
Total branches:104
Branch coverage:26.9% (28 of 104)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
GetResponseErrorCondition(...)-0%0%
GetResponseStatusCode(...)-0%0%
ToMessagingContractException(...)-0%0%
ToMessagingContractException(...)-66.67%50%
ToMessagingContractException(...)-27.78%25%
TranslateException(...)-51.61%60%
GetTrackingId(...)-0%0%
GetInnerException(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Amqp\AmqpExceptionHelper.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Globalization;
 7using System.IO;
 8using System.Net.Sockets;
 9using System.Text;
 10using System.Threading.Tasks;
 11using Microsoft.Azure.Amqp;
 12using Microsoft.Azure.Amqp.Encoding;
 13using Microsoft.Azure.Amqp.Framing;
 14
 15namespace Azure.Messaging.ServiceBus.Amqp
 16{
 17    internal static class AmqpExceptionHelper
 18    {
 019        private static readonly Dictionary<string, AmqpResponseStatusCode> s_conditionToStatusMap = new Dictionary<strin
 020        {
 021            { AmqpClientConstants.TimeoutError.Value, AmqpResponseStatusCode.RequestTimeout },
 022            { AmqpErrorCode.NotFound.Value, AmqpResponseStatusCode.NotFound },
 023            { AmqpErrorCode.NotImplemented.Value, AmqpResponseStatusCode.NotImplemented },
 024            { AmqpClientConstants.EntityAlreadyExistsError.Value, AmqpResponseStatusCode.Conflict },
 025            { AmqpClientConstants.MessageLockLostError.Value, AmqpResponseStatusCode.Gone },
 026            { AmqpClientConstants.SessionLockLostError.Value, AmqpResponseStatusCode.Gone },
 027            { AmqpErrorCode.ResourceLimitExceeded.Value, AmqpResponseStatusCode.Forbidden },
 028            { AmqpClientConstants.NoMatchingSubscriptionError.Value, AmqpResponseStatusCode.InternalServerError },
 029            { AmqpErrorCode.NotAllowed.Value, AmqpResponseStatusCode.BadRequest },
 030            { AmqpErrorCode.UnauthorizedAccess.Value, AmqpResponseStatusCode.Unauthorized },
 031            { AmqpErrorCode.MessageSizeExceeded.Value, AmqpResponseStatusCode.Forbidden },
 032            { AmqpClientConstants.ServerBusyError.Value, AmqpResponseStatusCode.ServiceUnavailable },
 033            { AmqpClientConstants.ArgumentError.Value, AmqpResponseStatusCode.BadRequest },
 034            { AmqpClientConstants.ArgumentOutOfRangeError.Value, AmqpResponseStatusCode.BadRequest },
 035            { AmqpClientConstants.StoreLockLostError.Value, AmqpResponseStatusCode.Gone },
 036            { AmqpClientConstants.SessionCannotBeLockedError.Value, AmqpResponseStatusCode.Gone },
 037            { AmqpClientConstants.PartitionNotOwnedError.Value, AmqpResponseStatusCode.Gone },
 038            { AmqpClientConstants.EntityDisabledError.Value, AmqpResponseStatusCode.BadRequest },
 039            { AmqpClientConstants.PublisherRevokedError.Value, AmqpResponseStatusCode.Unauthorized },
 040            { AmqpClientConstants.AuthorizationFailedError.Value, AmqpResponseStatusCode.Unauthorized},
 041            { AmqpErrorCode.Stolen.Value, AmqpResponseStatusCode.Gone }
 042        };
 43
 44        public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode)
 45        {
 046            object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition];
 047            if (condition != null)
 48            {
 049                return (AmqpSymbol)condition;
 50            }
 51
 52            // Most of the time we should have an error condition
 053            foreach (var kvp in s_conditionToStatusMap)
 54            {
 055                if (kvp.Value == statusCode)
 56                {
 057                    return kvp.Key;
 58                }
 59            }
 60
 061            return AmqpErrorCode.InternalError;
 062        }
 63
 64        public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage)
 65        {
 066            var amqpResponseStatusCode = AmqpResponseStatusCode.Unused;
 067            object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode]
 068            if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue))
 69            {
 70
 071                amqpResponseStatusCode = (AmqpResponseStatusCode)statusCodeValue;
 72            }
 73
 074            return amqpResponseStatusCode;
 75        }
 76
 77        public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode st
 78        {
 079            AmqpSymbol errorCondition = GetResponseErrorCondition(responseMessage, statusCode);
 080            var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescrip
 081            return ToMessagingContractException(errorCondition.Value, statusDescription);
 82        }
 83
 84        public static Exception ToMessagingContractException(this Error error, bool connectionError = false)
 85        {
 686            if (error == null)
 87            {
 088                return new ServiceBusException(true, "Unknown error.");
 89            }
 90
 691            return ToMessagingContractException(error.Condition.Value, error.Description, connectionError);
 92        }
 93
 94        public static Exception ToMessagingContractException(string condition, string message, bool connectionError = fa
 95        {
 696            if (string.Equals(condition, AmqpClientConstants.TimeoutError.Value, StringComparison.InvariantCultureIgnore
 97            {
 098                return new ServiceBusException(message, ServiceBusFailureReason.ServiceTimeout);
 99            }
 100
 6101            if (string.Equals(condition, AmqpErrorCode.NotFound.Value, StringComparison.InvariantCultureIgnoreCase))
 102            {
 0103                if (connectionError)
 104                {
 0105                    return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem);
 106                }
 107
 0108                return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityNotFound);
 109            }
 110
 6111            if (string.Equals(condition, AmqpErrorCode.NotImplemented.Value, StringComparison.InvariantCultureIgnoreCase
 112            {
 0113                return new NotSupportedException(message);
 114            }
 115
 6116            if (string.Equals(condition, AmqpErrorCode.NotAllowed.Value, StringComparison.InvariantCultureIgnoreCase))
 117            {
 0118                return new InvalidOperationException(message);
 119            }
 120
 6121            if (string.Equals(condition, AmqpErrorCode.UnauthorizedAccess.Value, StringComparison.InvariantCultureIgnore
 6122                string.Equals(condition, AmqpClientConstants.AuthorizationFailedError.Value, StringComparison.InvariantC
 123            {
 0124                return new ServiceBusException(message, ServiceBusFailureReason.Unauthorized);
 125            }
 126
 6127            if (string.Equals(condition, AmqpClientConstants.ServerBusyError.Value, StringComparison.InvariantCultureIgn
 128            {
 4129                return new ServiceBusException(message, ServiceBusFailureReason.ServiceBusy);
 130            }
 131
 2132            if (string.Equals(condition, AmqpClientConstants.ArgumentError.Value, StringComparison.InvariantCultureIgnor
 133            {
 2134                return new ArgumentException(message);
 135            }
 136
 0137            if (string.Equals(condition, AmqpClientConstants.ArgumentOutOfRangeError.Value, StringComparison.InvariantCu
 138            {
 0139                return new ArgumentOutOfRangeException(message);
 140            }
 141
 0142            if (string.Equals(condition, AmqpClientConstants.EntityDisabledError.Value, StringComparison.InvariantCultur
 143            {
 0144                return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityDisabled);
 145            }
 146
 0147            if (string.Equals(condition, AmqpClientConstants.MessageLockLostError.Value, StringComparison.InvariantCultu
 148            {
 0149                return new ServiceBusException(message, ServiceBusFailureReason.MessageLockLost);
 150            }
 151
 0152            if (string.Equals(condition, AmqpClientConstants.EntityAlreadyExistsError.Value, StringComparison.InvariantC
 153            {
 0154                return new ServiceBusException(message, ServiceBusFailureReason.MessagingEntityAlreadyExists);
 155            }
 156
 0157            if (string.Equals(condition, AmqpClientConstants.SessionLockLostError.Value, StringComparison.InvariantCultu
 158            {
 0159                return new ServiceBusException(message, ServiceBusFailureReason.SessionLockLost);
 160            }
 161
 0162            if (string.Equals(condition, AmqpErrorCode.ResourceLimitExceeded.Value, StringComparison.InvariantCultureIgn
 163            {
 0164                return new ServiceBusException(message, ServiceBusFailureReason.QuotaExceeded);
 165            }
 166
 0167            if (string.Equals(condition, AmqpErrorCode.MessageSizeExceeded.Value, StringComparison.InvariantCultureIgnor
 168            {
 0169                return new ServiceBusException(message, ServiceBusFailureReason.MessageSizeExceeded);
 170            }
 171
 0172            if (string.Equals(condition, AmqpClientConstants.MessageNotFoundError.Value, StringComparison.InvariantCultu
 173            {
 0174                return new ServiceBusException(message, ServiceBusFailureReason.MessageNotFound);
 175            }
 176
 0177            if (string.Equals(condition, AmqpClientConstants.SessionCannotBeLockedError.Value, StringComparison.Invarian
 178            {
 0179                return new ServiceBusException(message, ServiceBusFailureReason.SessionCannotBeLocked);
 180            }
 181
 0182            return new ServiceBusException(true, message);
 183        }
 184
 185        public static Exception TranslateException(Exception exception, string referenceId = null, Exception innerExcept
 186        {
 72187            var stringBuilder = new StringBuilder();
 72188            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, exception.Message);
 72189            if (referenceId != null)
 190            {
 0191                stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $"Reference: {referenceId}, {DateTime.UtcNow}")
 192            }
 193
 72194            var message = stringBuilder.ToString();
 72195            var aggregateException = innerException == null ? exception : new AggregateException(exception, innerExcepti
 196
 72197            switch (exception)
 198            {
 199                case SocketException _:
 0200                    message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {((SocketException)
 0201                    return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx
 202
 203                case IOException _:
 0204                    if (exception.InnerException is SocketException socketException)
 205                    {
 0206                        message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {socketExceptio
 207                    }
 0208                    return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx
 209
 210                case AmqpException amqpException:
 0211                    return amqpException.Error.ToMessagingContractException(connectionError);
 212
 18213                case OperationCanceledException operationCanceledException when operationCanceledException.InnerExceptio
 2214                    return amqpException.Error.ToMessagingContractException(connectionError);
 215
 16216                case OperationCanceledException _ when connectionError:
 0217                    return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx
 218
 16219                case OperationCanceledException operationCanceledException when
 16220                operationCanceledException.InnerException != null:
 2221                    return operationCanceledException.InnerException;
 222
 14223                case OperationCanceledException operationEx when !(operationEx is TaskCanceledException):
 10224                    return new ServiceBusException(operationEx.Message, ServiceBusFailureReason.ServiceTimeout);
 225
 226                case TimeoutException _:
 0227                    return new ServiceBusException(
 0228                        message,
 0229                        ServiceBusFailureReason.ServiceTimeout,
 0230                        innerException: aggregateException);
 231
 0232                case InvalidOperationException _ when connectionError:
 0233                    return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerEx
 234            }
 235
 58236            if (connectionError)
 237            {
 0238                return new ServiceBusException(message, ServiceBusFailureReason.ServiceCommunicationProblem, innerExcept
 239            }
 240
 58241            return aggregateException;
 242        }
 243
 244        public static string GetTrackingId(this AmqpLink link)
 245        {
 0246            if (link.Settings.Properties != null &&
 0247                link.Settings.Properties.TryGetValue<string>(
 0248                    AmqpClientConstants.TrackingIdName,
 0249                    out var trackingContext))
 250            {
 0251                return trackingContext;
 252            }
 253
 0254            return null;
 255        }
 256
 257        public static Exception GetInnerException(this AmqpObject amqpObject)
 258        {
 0259            var connectionError = false;
 260            Exception innerException;
 261            switch (amqpObject)
 262            {
 263                case AmqpSession amqpSession:
 0264                    innerException = amqpSession.TerminalException ?? amqpSession.Connection.TerminalException;
 0265                    break;
 266
 267                case AmqpLink amqpLink:
 0268                    connectionError = amqpLink.Session.IsClosing();
 0269                    innerException = amqpLink.TerminalException ?? amqpLink.Session.TerminalException ?? amqpLink.Sessio
 0270                    break;
 271
 272                case RequestResponseAmqpLink amqpReqRespLink:
 0273                    innerException = amqpReqRespLink.TerminalException ?? amqpReqRespLink.Session.TerminalException ?? a
 0274                    break;
 275
 276                default:
 0277                    return null;
 278            }
 279
 0280            return innerException == null ? null : TranslateException(innerException, null, null, connectionError);
 281        }
 282    }
 283}