示例#1
0
        /// <summary>
        /// Gets the error details from an XML-formatted error stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The error details.</returns>
        public static async Task <StorageExtendedErrorInformation> ReadFromStreamAsync(Stream inputStream)
        {
            CommonUtility.AssertNotNull("inputStream", inputStream);

            if (inputStream.CanSeek && inputStream.Length < 1)
            {
                return(null);
            }

            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();

            try
            {
                using (XmlReader reader = XMLReaderExtensions.CreateAsAsync(inputStream))
                {
                    await reader.ReadAsync().ConfigureAwait(false);

                    await extendedErrorInfo.ReadXmlAsync(reader, CancellationToken.None).ConfigureAwait(false);
                }

                return(extendedErrorInfo);
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return(null);
            }
        }
示例#2
0
        /// <summary>
        /// Gets the error details from an XML-formatted error stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The error details.</returns>
        public static async Task <StorageExtendedErrorInformation> ReadFromStreamAsync(Stream inputStream, CancellationToken cancellationToken)
        {
            CommonUtility.AssertNotNull("inputStream", inputStream);

            if (inputStream.CanSeek && inputStream.Length < 1)
            {
                return(null);
            }

            StorageExtendedErrorInformation extendedErrorInfo = new StorageExtendedErrorInformation();

            try
            {
                using (XmlReader reader = XmlReader.Create(inputStream, new XmlReaderSettings {
                    Async = true
                }))
                {
                    await reader.ReadAsync().ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();

                    await extendedErrorInfo.ReadXmlAsync(reader, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();
                }

                return(extendedErrorInfo);
            }
            catch (XmlException)
            {
                // If there is a parsing error we cannot return extended error information
                return(null);
            }
        }
        public async Task ExtendedErrorInfoVerifyXmlWithAdditionalDetails()
        {
            Uri                baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient    client         = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container      = client.GetContainerReference(Guid.NewGuid().ToString("N"));

            byte[] buffer     = TestBase.GetRandomBuffer(4 * 1024 * 1024);
            MD5    md5        = MD5.Create();
            string contentMD5 = Convert.ToBase64String(md5.ComputeHash(buffer));

            try
            {
                container.Create();
                CloudBlockBlob blob   = container.GetBlockBlobReference("blob1");
                List <string>  blocks = new List <string>();
                for (int i = 0; i < 2; i++)
                {
                    blocks.Add(Convert.ToBase64String(Guid.NewGuid().ToByteArray()));
                }

                using (MemoryStream memoryStream = new MemoryStream(buffer))
                {
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    blob.PutBlock(blocks[0], memoryStream, contentMD5);

                    int offset = buffer.Length - 1024;
                    memoryStream.Seek(offset, SeekOrigin.Begin);
                    StorageException e = TestHelper.ExpectedException <StorageException>(
                        () => blob.PutBlock(blocks[1], memoryStream, contentMD5),
                        "Invalid MD5 should fail with mismatch");

                    Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                    StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    StringBuilder sb = new StringBuilder();
                    using (XmlWriter writer = XmlWriter.Create(sb, settings))
                    {
                        e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                    }

                    using (XmlReader reader = XMLReaderExtensions.CreateAsAsync(new MemoryStream(Encoding.Unicode.GetBytes(sb.ToString()))))
                    {
                        await retrErrorInfo.ReadXmlAsync(reader, CancellationToken.None);
                    }

                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
                    Assert.AreNotEqual(0, retrErrorInfo.AdditionalDetails.Count);
                    Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.AdditionalDetails.Count, retrErrorInfo.AdditionalDetails.Count);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public async Task ExtendedErrorInfoVerifyXml()
        {
            Uri                baseAddressUri = new Uri(TestBase.TargetTenantConfig.BlobServiceEndpoint);
            CloudBlobClient    client         = new CloudBlobClient(baseAddressUri, TestBase.StorageCredentials);
            CloudBlobContainer container      = client.GetContainerReference(Guid.NewGuid().ToString("N"));

            try
            {
                StorageException e = TestHelper.ExpectedException <StorageException>(
                    () => container.GetPermissions(),
                    "Try to get permissions on a non-existent container");

                Assert.IsNotNull(e.RequestInformation.ExtendedErrorInformation);

                StorageExtendedErrorInformation retrErrorInfo = new StorageExtendedErrorInformation();
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                StringBuilder sb = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, settings))
                {
                    e.RequestInformation.ExtendedErrorInformation.WriteXml(writer);
                }

                using (XmlReader reader = XMLReaderExtensions.CreateAsAsync(new MemoryStream(Encoding.Unicode.GetBytes(sb.ToString()))))
                {
                    await retrErrorInfo.ReadXmlAsync(reader, CancellationToken.None);
                }

                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }