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(); } }
public async Task RequestResultErrorCode() { 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 { RequestResult requestResult; XmlWriterSettings settings; StringBuilder sb; 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())); } // Verify the ErrorCode property is set and that it is serialized correctly 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.AreEqual(e.RequestInformation.ErrorCode, StorageErrorCodeStrings.Md5Mismatch); requestResult = new RequestResult(); settings = new XmlWriterSettings(); settings.Indent = true; sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, settings)) { e.RequestInformation.WriteXml(writer); } using (XmlReader reader = XMLReaderExtensions.CreateAsAsync(new MemoryStream(Encoding.Unicode.GetBytes(sb.ToString())))) { await requestResult.ReadXmlAsync(reader); } // ExtendedErrorInformation.ErrorCode will be depricated, but it should still match on a non HEAD request Assert.AreEqual(e.RequestInformation.ErrorCode, requestResult.ErrorCode); Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, requestResult.ErrorCode); } // Verify the ErrorCode property is set on a HEAD request CloudAppendBlob blob2 = container.GetAppendBlobReference("blob2"); blob2.CreateOrReplace(); StorageException e2 = TestHelper.ExpectedException <StorageException>( () => blob2.FetchAttributes(AccessCondition.GenerateIfMatchCondition("\"garbage\"")), // must supply our own quotes for a valid etag "Mismatched etag should fail"); Assert.AreEqual(e2.RequestInformation.ErrorCode, StorageErrorCodeStrings.ConditionNotMet); // Verify the ErrorCode property is not set on a successful request and that it is serialized correctly OperationContext ctx = new OperationContext(); blob2.FetchAttributes(operationContext: ctx); Assert.AreEqual(ctx.RequestResults[0].ErrorCode, null); requestResult = new RequestResult(); settings = new XmlWriterSettings(); settings.Indent = true; sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, settings)) { ctx.RequestResults[0].WriteXml(writer); } using (XmlReader reader = XMLReaderExtensions.CreateAsAsync(new MemoryStream(Encoding.Unicode.GetBytes(sb.ToString())))) { await requestResult.ReadXmlAsync(reader); } Assert.AreEqual(ctx.RequestResults[0].ErrorCode, requestResult.ErrorCode); } finally { container.DeleteIfExists(); } }