public static Error ToAmqpError(Exception exception)
        {
            bool flag;

            if (exception == null)
            {
                return(null);
            }
            Error error = new Error()
            {
                Description = ExceptionHelper.GetExceptionMessage(exception, out flag)
            };

            if (exception is AmqpException)
            {
                AmqpException amqpException = (AmqpException)exception;
                error.Condition = amqpException.Error.Condition;
                error.Info      = amqpException.Error.Info;
            }
            else if (exception is UnauthorizedAccessException)
            {
                error.Condition = AmqpError.UnauthorizedAccess.Condition;
            }
            else if (exception is TransactionAbortedException)
            {
                error.Condition = AmqpError.TransactionRollback.Condition;
            }
            else if (exception is NotSupportedException)
            {
                error.Condition = AmqpError.NotImplemented.Condition;
            }
            else if (exception is MessagingEntityNotFoundException)
            {
                error.Condition = AmqpError.NotFound.Condition;
            }
            else if (exception is MessagingEntityAlreadyExistsException)
            {
                error.Condition = ClientConstants.EntityAlreadyExistsError;
            }
            else if (exception is AddressAlreadyInUseException)
            {
                error.Condition = ClientConstants.AddressAlreadyInUseError;
            }
            else if (exception is AuthorizationFailedException)
            {
                error.Condition = ClientConstants.AuthorizationFailedError;
            }
            else if (exception is MessageLockLostException)
            {
                error.Condition = ClientConstants.MessageLockLostError;
            }
            else if (exception is SessionLockLostException)
            {
                error.Condition = ClientConstants.SessionLockLostError;
            }
            else if (exception is Microsoft.ServiceBus.Messaging.QuotaExceededException || exception is System.ServiceModel.QuotaExceededException)
            {
                error.Condition = AmqpError.ResourceLimitExceeded.Condition;
            }
            else if (exception is TimeoutException)
            {
                error.Condition = ClientConstants.TimeoutError;
            }
            else if (exception is NoMatchingSubscriptionException)
            {
                error.Condition = ClientConstants.NoMatchingSubscriptionError;
            }
            else if (exception is ServerBusyException)
            {
                error.Condition = ClientConstants.ServerBusyError;
            }
            else if (exception is MessageStoreLockLostException)
            {
                error.Condition = ClientConstants.StoreLockLostError;
            }
            else if (exception is SessionCannotBeLockedException)
            {
                error.Condition = ClientConstants.SessionCannotBeLockedError;
            }
            else if (exception is PartitionNotOwnedException)
            {
                error.Condition = ClientConstants.PartitionNotOwnedError;
            }
            else if (exception is MessagingEntityDisabledException)
            {
                error.Condition = ClientConstants.EntityDisabledError;
            }
            else if (exception is OperationCanceledException)
            {
                error.Condition = ClientConstants.OperationCancelledError;
            }
            else if (exception is RelayNotFoundException)
            {
                error.Condition = ClientConstants.RelayNotFoundError;
            }
            else if (exception is MessageSizeExceededException)
            {
                error.Condition = AmqpError.MessageSizeExceeded.Condition;
            }
            else if (!(exception is ReceiverDisconnectedException))
            {
                error.Condition = AmqpError.InternalError.Condition;
                if (flag)
                {
                    if (exception is InvalidOperationException)
                    {
                        error.Condition = AmqpError.NotAllowed.Condition;
                    }
                    else if (exception is ArgumentOutOfRangeException)
                    {
                        error.Condition = ClientConstants.ArgumentOutOfRangeError;
                    }
                    else if (exception is ArgumentException)
                    {
                        error.Condition = ClientConstants.ArgumentError;
                    }
                }
                error.Description = (ExceptionHelper.IncludeExceptionDetails || flag ? error.Description : SRClient.InternalServerError);
            }
            else
            {
                error.Condition = AmqpError.Stolen.Condition;
            }
            if (ExceptionHelper.IncludeExceptionDetails)
            {
                string stackTrace = exception.StackTrace;
                if (stackTrace != null)
                {
                    if (stackTrace.Length > 32768)
                    {
                        stackTrace = stackTrace.Substring(0, 32768);
                    }
                    if (error.Info == null)
                    {
                        error.Info = new Fields();
                    }
                    error.Info.Add(ClientConstants.StackTraceName, stackTrace);
                }
            }
            return(error);
        }