Beispiel #1
0
        public static Error ToAmqpError(Exception exception, bool includeStackTrace)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            Error error = new Error();

            error.Description = exception.Message;

            if (exception is AmqpException)
            {
                AmqpException amqpException = (AmqpException)exception;
                error.Condition = amqpException.Error.Condition;
                error.Info      = amqpException.Error.Info;
            }
            else if (exception is UnauthorizedAccessException || exception is UnauthorizedException)
            {
                error.Condition = AmqpErrorCode.UnauthorizedAccess;
            }
            else if (exception is NotSupportedException)
            {
                error.Condition = AmqpErrorCode.NotImplemented;
            }
            else if (exception is DeviceNotFoundException)
            {
                error.Condition = AmqpErrorCode.NotFound;
            }
            else if (exception is IotHubNotFoundException)
            {
                error.Condition = IotHubAmqpErrorCode.IotHubNotFoundError;
            }
            else if (exception is DeviceMessageLockLostException)
            {
                error.Condition = IotHubAmqpErrorCode.MessageLockLostError;
            }
            else if (exception is MessageTooLargeException)
            {
                error.Condition = AmqpErrorCode.MessageSizeExceeded;
            }
            else if (exception is DeviceMaximumQueueDepthExceededException)
            {
                error.Condition = AmqpErrorCode.ResourceLimitExceeded;
            }
            else if (exception is TimeoutException)
            {
                error.Condition = IotHubAmqpErrorCode.TimeoutError;
            }
            else if (exception is InvalidOperationException)
            {
                error.Condition = AmqpErrorCode.NotAllowed;
            }
            else if (exception is ArgumentOutOfRangeException)
            {
                error.Condition = IotHubAmqpErrorCode.ArgumentOutOfRangeError;
            }
            else if (exception is ArgumentException)
            {
                error.Condition = IotHubAmqpErrorCode.ArgumentError;
            }
            else if (exception is PreconditionFailedException)
            {
                error.Condition = IotHubAmqpErrorCode.PreconditionFailed;
            }
            else if (exception is IotHubSuspendedException)
            {
                error.Condition = IotHubAmqpErrorCode.IotHubSuspended;
            }
            else if (exception is QuotaExceededException)
            {
                error.Condition = IotHubAmqpErrorCode.QuotaExceeded;
            }
            else if (exception is TimeoutException)
            {
                error.Condition = IotHubAmqpErrorCode.TimeoutError;
            }
            else
            {
                error.Condition   = AmqpErrorCode.InternalError;
                error.Description = error.Description;
            }
            // we will always need this to add trackingId
            if (error.Info == null)
            {
                error.Info = new Fields();
            }

            string stackTrace;

            if (includeStackTrace && !string.IsNullOrEmpty(stackTrace = exception.StackTrace))
            {
                if (stackTrace.Length > MaxSizeInInfoMap)
                {
                    stackTrace = stackTrace.Substring(0, MaxSizeInInfoMap);
                }

                // error.Info came from AmqpExcpetion then it contains StackTraceName already.
                string dummy;
                if (!error.Info.TryGetValue(IotHubAmqpProperty.StackTraceName, out dummy))
                {
                    error.Info.Add(IotHubAmqpProperty.StackTraceName, stackTrace);
                }
            }

            string trackingId;

            error.Info.TryGetValue(IotHubAmqpProperty.TrackingId, out trackingId);
            trackingId = TrackingHelper.CheckAndAddGatewayIdToTrackingId(trackingId);
            error.Info[IotHubAmqpProperty.TrackingId] = trackingId;

            return(error);
        }