< Summary

Class:Microsoft.Azure.ServiceBus.Amqp.AmqpExceptionHelper
Assembly:Microsoft.Azure.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\servicebus\Microsoft.Azure.ServiceBus\src\Amqp\AmqpExceptionHelper.cs
Covered lines:0
Uncovered lines:115
Coverable lines:115
Total lines:267
Line coverage:0% (0 of 115)
Covered branches:0
Total branches:98
Branch coverage:0% (0 of 98)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-0%100%
GetResponseErrorCondition(...)-0%0%
GetResponseStatusCode(...)-0%0%
ToMessagingContractException(...)-0%0%
ToMessagingContractException(...)-0%0%
ToMessagingContractException(...)-0%0%
GetClientException(...)-0%0%
GetTrackingId(...)-0%0%
GetInnerException(...)-0%0%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft. All rights reserved.
 2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
 3
 4namespace Microsoft.Azure.ServiceBus.Amqp
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Globalization;
 9    using System.IO;
 10    using System.Net.Sockets;
 11    using System.Text;
 12    using Microsoft.Azure.Amqp;
 13    using Microsoft.Azure.Amqp.Encoding;
 14    using Microsoft.Azure.Amqp.Framing;
 15
 16    static class AmqpExceptionHelper
 17    {
 018        static readonly Dictionary<string, AmqpResponseStatusCode> ConditionToStatusMap = new Dictionary<string, AmqpRes
 019        {
 020            { AmqpClientConstants.TimeoutError.Value, AmqpResponseStatusCode.RequestTimeout },
 021            { AmqpErrorCode.NotFound.Value, AmqpResponseStatusCode.NotFound },
 022            { AmqpErrorCode.NotImplemented.Value, AmqpResponseStatusCode.NotImplemented },
 023            { AmqpClientConstants.EntityAlreadyExistsError.Value, AmqpResponseStatusCode.Conflict },
 024            { AmqpClientConstants.MessageLockLostError.Value, AmqpResponseStatusCode.Gone },
 025            { AmqpClientConstants.SessionLockLostError.Value, AmqpResponseStatusCode.Gone },
 026            { AmqpErrorCode.ResourceLimitExceeded.Value, AmqpResponseStatusCode.Forbidden },
 027            { AmqpClientConstants.NoMatchingSubscriptionError.Value, AmqpResponseStatusCode.InternalServerError },
 028            { AmqpErrorCode.NotAllowed.Value, AmqpResponseStatusCode.BadRequest },
 029            { AmqpErrorCode.UnauthorizedAccess.Value, AmqpResponseStatusCode.Unauthorized },
 030            { AmqpErrorCode.MessageSizeExceeded.Value, AmqpResponseStatusCode.Forbidden },
 031            { AmqpClientConstants.ServerBusyError.Value, AmqpResponseStatusCode.ServiceUnavailable },
 032            { AmqpClientConstants.ArgumentError.Value, AmqpResponseStatusCode.BadRequest },
 033            { AmqpClientConstants.ArgumentOutOfRangeError.Value, AmqpResponseStatusCode.BadRequest },
 034            { AmqpClientConstants.StoreLockLostError.Value, AmqpResponseStatusCode.Gone },
 035            { AmqpClientConstants.SessionCannotBeLockedError.Value, AmqpResponseStatusCode.Gone },
 036            { AmqpClientConstants.PartitionNotOwnedError.Value, AmqpResponseStatusCode.Gone },
 037            { AmqpClientConstants.EntityDisabledError.Value, AmqpResponseStatusCode.BadRequest },
 038            { AmqpClientConstants.PublisherRevokedError.Value, AmqpResponseStatusCode.Unauthorized },
 039            { AmqpClientConstants.AuthorizationFailedError.Value, AmqpResponseStatusCode.Unauthorized},
 040            { AmqpErrorCode.Stolen.Value, AmqpResponseStatusCode.Gone }
 041        };
 42
 43        public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode)
 44        {
 045            object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition];
 046            if (condition != null)
 47            {
 048                return (AmqpSymbol)condition;
 49            }
 50
 51            // Most of the time we should have an error condition
 052            foreach (var kvp in ConditionToStatusMap)
 53            {
 054                if (kvp.Value == statusCode)
 55                {
 056                    return kvp.Key;
 57                }
 58            }
 59
 060            return AmqpErrorCode.InternalError;
 061        }
 62
 63        public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage)
 64        {
 065            var amqpResponseStatusCode = AmqpResponseStatusCode.Unused;
 066            object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode]
 067            if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue))
 68            {
 069                amqpResponseStatusCode = (AmqpResponseStatusCode)statusCodeValue;
 70            }
 71
 072            return amqpResponseStatusCode;
 73        }
 74
 75        public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode st
 76        {
 077            AmqpSymbol errorCondition = AmqpExceptionHelper.GetResponseErrorCondition(responseMessage, statusCode);
 078            var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescrip
 079            return AmqpExceptionHelper.ToMessagingContractException(errorCondition.Value, statusDescription);
 80        }
 81
 82        public static Exception ToMessagingContractException(this Error error, bool connectionError = false)
 83        {
 084            if (error == null)
 85            {
 086                return new ServiceBusException(true, "Unknown error.");
 87            }
 88
 089            return ToMessagingContractException(error.Condition.Value, error.Description, connectionError);
 90        }
 91
 92        static Exception ToMessagingContractException(string condition, string message, bool connectionError = false)
 93        {
 094            if (string.Equals(condition, AmqpClientConstants.TimeoutError.Value))
 95            {
 096                return new ServiceBusTimeoutException(message);
 97            }
 98
 099            if (string.Equals(condition, AmqpErrorCode.NotFound.Value))
 100            {
 0101                if (connectionError)
 102                {
 0103                    return new ServiceBusCommunicationException(message, null);
 104                }
 105
 0106                return new MessagingEntityNotFoundException(message, null);
 107            }
 108
 0109            if (string.Equals(condition, AmqpErrorCode.NotImplemented.Value))
 110            {
 0111                return new NotSupportedException(message);
 112            }
 113
 0114            if (string.Equals(condition, AmqpErrorCode.NotAllowed.Value))
 115            {
 0116                return new InvalidOperationException(message);
 117            }
 118
 0119            if (string.Equals(condition, AmqpErrorCode.UnauthorizedAccess.Value) ||
 0120                string.Equals(condition, AmqpClientConstants.AuthorizationFailedError.Value))
 121            {
 0122                return new UnauthorizedException(message);
 123            }
 124
 0125            if (string.Equals(condition, AmqpClientConstants.ServerBusyError.Value))
 126            {
 0127                return new ServerBusyException(message);
 128            }
 129
 0130            if (string.Equals(condition, AmqpClientConstants.ArgumentError.Value))
 131            {
 0132                return new ArgumentException(message);
 133            }
 134
 0135            if (string.Equals(condition, AmqpClientConstants.ArgumentOutOfRangeError.Value))
 136            {
 0137                return new ArgumentOutOfRangeException(message);
 138            }
 139
 0140            if (string.Equals(condition, AmqpClientConstants.EntityDisabledError.Value))
 141            {
 0142                return new MessagingEntityDisabledException(message, null);
 143            }
 144
 0145            if (string.Equals(condition, AmqpClientConstants.MessageLockLostError.Value))
 146            {
 0147                return new MessageLockLostException(message);
 148            }
 149
 0150            if (string.Equals(condition, AmqpClientConstants.SessionLockLostError.Value))
 151            {
 0152                return new SessionLockLostException(message);
 153            }
 154
 0155            if (string.Equals(condition, AmqpErrorCode.ResourceLimitExceeded.Value))
 156            {
 0157                return new QuotaExceededException(message);
 158            }
 159
 0160            if (string.Equals(condition, AmqpErrorCode.MessageSizeExceeded.Value))
 161            {
 0162                return new MessageSizeExceededException(message);
 163            }
 164
 0165            if (string.Equals(condition, AmqpClientConstants.MessageNotFoundError.Value))
 166            {
 0167                return new MessageNotFoundException(message);
 168            }
 169
 0170            if (string.Equals(condition, AmqpClientConstants.SessionCannotBeLockedError.Value))
 171            {
 0172                return new SessionCannotBeLockedException(message);
 173            }
 174
 0175            return new ServiceBusException(true, message);
 176        }
 177
 178        public static Exception GetClientException(Exception exception, string referenceId = null, Exception innerExcept
 179        {
 0180            var stringBuilder = new StringBuilder();
 0181            stringBuilder.AppendFormat(CultureInfo.InvariantCulture, exception.Message);
 0182            if (referenceId != null)
 183            {
 0184                stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $"Reference: {referenceId}, {DateTime.UtcNow}")
 185            }
 186
 0187            var message = stringBuilder.ToString();
 0188            var aggregateException = innerException == null ? exception : new AggregateException(exception, innerExcepti
 189
 0190            switch (exception)
 191            {
 192                case SocketException _:
 0193                    message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {((SocketException)
 0194                    return new ServiceBusCommunicationException(message, aggregateException);
 195
 196                case IOException _:
 0197                    if (exception.InnerException is SocketException socketException)
 198                    {
 0199                        message = stringBuilder.AppendFormat(CultureInfo.InvariantCulture, $" ErrorCode: {socketExceptio
 200                    }
 0201                    return new ServiceBusCommunicationException(message, aggregateException);
 202
 203                case AmqpException amqpException:
 0204                    return amqpException.Error.ToMessagingContractException(connectionError);
 205
 0206                case OperationCanceledException operationCanceledException when operationCanceledException.InnerExceptio
 0207                    return amqpException.Error.ToMessagingContractException(connectionError);
 208
 0209                case OperationCanceledException _ when connectionError:
 0210                    return new ServiceBusCommunicationException(message, aggregateException);
 211
 212                case OperationCanceledException _:
 0213                    return new ServiceBusException(true, message, aggregateException);
 214
 215                case TimeoutException _:
 0216                    return new ServiceBusTimeoutException(message, aggregateException);
 217
 0218                case InvalidOperationException _ when connectionError:
 0219                    return new ServiceBusCommunicationException(message, aggregateException);
 220            }
 221
 0222            if (connectionError)
 223            {
 0224                return new ServiceBusCommunicationException(message, aggregateException);
 225            }
 226
 0227            return aggregateException;
 228        }
 229
 230        public static string GetTrackingId(this AmqpLink link)
 231        {
 0232            if (link.Settings.Properties != null &&
 0233                link.Settings.Properties.TryGetValue<string>(AmqpClientConstants.TrackingIdName, out var trackingContext
 234            {
 0235                return trackingContext;
 236            }
 237
 0238            return null;
 239        }
 240
 241        public static Exception GetInnerException(this AmqpObject amqpObject)
 242        {
 0243            var connectionError = false;
 244            Exception innerException;
 245            switch (amqpObject)
 246            {
 247                case AmqpSession amqpSession:
 0248                    innerException = amqpSession.TerminalException ?? amqpSession.Connection.TerminalException;
 0249                    break;
 250
 251                case AmqpLink amqpLink:
 0252                    connectionError = amqpLink.Session.IsClosing();
 0253                    innerException = amqpLink.TerminalException ?? amqpLink.Session.TerminalException ?? amqpLink.Sessio
 0254                    break;
 255
 256                case RequestResponseAmqpLink amqpReqRespLink:
 0257                    innerException = amqpReqRespLink.TerminalException ?? amqpReqRespLink.Session.TerminalException ?? a
 0258                    break;
 259
 260                default:
 0261                    return null;
 262            }
 263
 0264            return innerException == null ? null : GetClientException(innerException, null, null, connectionError);
 265        }
 266    }
 267}