예제 #1
0
        public static Exception GetExceptionFromOutcome(Outcome outcome)
        {
            Exception retException = null;

            if (outcome == null)
            {
                retException = new IotHubException("Unknown error.");
                return(retException);
            }

            if (outcome.DescriptorCode == Rejected.Code)
            {
                var rejected = (Rejected)outcome;
                retException = AmqpIoTErrorAdapter.ToIotHubClientContract(rejected.Error);
            }
            else if (outcome.DescriptorCode == Released.Code)
            {
                retException = new OperationCanceledException("AMQP link released.");
            }
            else
            {
                retException = new IotHubException("Unknown error.");
            }

            return(retException);
        }
        internal static Exception ConvertToIoTHubException(Exception exception)
        {
            if (exception is TimeoutException)
            {
                return(new IotHubCommunicationException(exception.Message, exception));
            }
            else if (exception is UnauthorizedAccessException)
            {
                return(new UnauthorizedException(exception.Message, exception));
            }
            else
            {
                var amqpException = exception as AmqpException;
                if (amqpException != null)
                {
                    return(AmqpIoTErrorAdapter.ToIotHubClientContract(amqpException));
                }

                return(exception);
            }
        }
        public void ThrowIfError()
        {
            if (Outcome.DescriptorCode != Accepted.Code)
            {
                if (Outcome.DescriptorCode == Rejected.Code)
                {
                    var rejected = (Rejected)Outcome;

                    // Special treatment for NotFound amqp rejected error code in case of DisposeMessage
                    if (rejected.Error != null && rejected.Error.Condition.Equals(AmqpErrorCode.NotFound))
                    {
                        Error error = new Error
                        {
                            Condition = AmqpIoTErrorAdapter.MessageLockLostError
                        };
                        throw AmqpIoTErrorAdapter.ToIotHubClientContract(error);
                    }
                }

                throw AmqpIoTErrorAdapter.GetExceptionFromOutcome(Outcome);
            }
        }