public ChainTableBatchException(int failedOpIndex, StorageException storageEx)
     : base(storageEx.RequestInformation,
           string.Format("{0} at operation {1}", storageEx.Message, failedOpIndex),
           storageEx.InnerException)
 {
     this.FailedOpIndex = failedOpIndex;
 }
 public TestableStorageClientException(StorageException ex)
 {
     if (ex.RequestInformation != null)
     {
         HttpStatusCode = (HttpStatusCode)ex.RequestInformation.HttpStatusCode;
     }
 }
 private static int HttpStatusCode(StorageException e)
 {
     if (e == null || e.RequestInformation == null)
     {
         return 0;
     }
     return e.RequestInformation.HttpStatusCode;
 }
 public void IsNotFoundExceptionWithoutStatusTest()
 {
     RequestResult result = new RequestResult();
     string message = string.Empty;
     ResourceAlreadyExistException innerException = new ResourceAlreadyExistException(message);
     StorageException exception = new StorageException(result, message, innerException);
     Assert.IsFalse(exception.IsNotFoundException());
 }
Example #5
0
 public static HandlerResult FromException(StorageException ex)
 {
     return new HandlerResult
     {
         StatusCode = (HttpStatusCode)ex.RequestInformation.HttpStatusCode,
         ReasonPhrase = ex.RequestInformation.HttpStatusMessage,
         ErrorInformation = DashErrorInformation.Create(ex.RequestInformation.ExtendedErrorInformation),
     };
 }
        public static bool TryHandleStorageException(AzureStorageException ex)
        {
            if (IsDoesntExist(ex))
            {
                throw new StorageException(ErrorCode.NotFound, ex);
            }

            return(false);
        }
        private static bool TryHandleStorageException(AzureStorageException ex)
        {
            if (ex.RequestInformation?.HttpStatusCode == 404)
            {
                throw new StorageException(ErrorCode.NotFound, ex);
            }

            return(false);
        }
 /// <summary>
 /// Helper to remat exceptions
 /// </summary>
 /// <param name="ex"></param>
 /// <returns></returns>
 private static Exception HandleAndRemapCommonExceptions(Microsoft.WindowsAzure.Storage.StorageException ex)
 {
     if (ex.Message.Contains("412"))
     {
         return(new ConcurrencyException("concurrency detected.  see inner exception for details", ex));
     }
     else
     {
         return(ex);
     }
 }
 public void RepackStorageExceptionWithoutStatusMessageTest()
 {
     RequestResult result = new RequestResult()
     {
         HttpStatusCode = 500
     };
     string message = string.Empty;
     ResourceAlreadyExistException innerException = new ResourceAlreadyExistException(message);
     StorageException exception = new StorageException(result, message, innerException);
     Assert.IsNotNull(exception.RepackStorageException());
 }
 public void RepackStorageExceptionTest()
 {
     string status = String.Empty;
     RequestResult result = new RequestResult()
     {
         HttpStatusCode = 404
     };
     string message = "storage exception";
     ResourceAlreadyExistException innerException = new ResourceAlreadyExistException(message);
     StorageException exception = new StorageException(result, message, innerException);
     exception = exception.RepackStorageException();
     Assert.IsNotNull(exception);
 }
 public static void HardStorageExceptionCaughtWritingToBlob(
     this ILogger logger,
     StorageException storageException,
     CloudBlobClient blobClient,
     string containerName)
 {
     logger.Error(
         storageException,
         "Error attempting storage operation on container {1} for storage account {0}: {2}",
         GetStorageAccountName(blobClient),
         containerName,
         storageException.RequestInformation.ExtendedErrorInformation.ErrorCode);
 }
        /// <summary>
        /// Returns the view with an updated model in the event of a storage error.
        /// </summary>
        /// <param name="row">The maps row key.</param>
        /// <param name="partition">The maps partition.</param>
        /// <param name="error">The storage error.</param>
        /// <returns>An updated view.</returns>
        protected ActionResult AzureStorageErrorResponse(string row, string partition, StorageException error)
        {
            Guard.NotNullOrEmpty(() => row);
            Guard.NotNullOrEmpty(() => partition);
            Guard.NotNull(() => error);

            ViewBag.Alerts = AlertModel.CreateSingle(AlertType.Danger, error.Message);

            var entityTask = TableStorage.Get(partition, row);

            Task.WaitAll(entityTask);

            return this.View(entityTask.Result);
        }
        /// <summary>
        /// Replace storage exception to expose more information in Message.
        /// </summary>
        /// <param name="e">StorageException from storage client</param>
        /// <returns>A new storage exception with detailed error message</returns>
        public static StorageException RepackStorageException(this StorageException e)
        {
            if (null != e.RequestInformation &&
                null != e.RequestInformation.HttpStatusMessage)
            {
                String msg = string.Format(
                    "{0} HTTP Status Code: {1} - HTTP Error Message: {2}",
                    e.Message,
                    e.RequestInformation.HttpStatusCode,
                    e.RequestInformation.HttpStatusMessage);
                e = new StorageException(e.RequestInformation, msg, e);
            }

            return e;
        }
        public void IsNotFoundExceptionTest()
        {
            RequestResult result = new RequestResult()
            {
                HttpStatusCode = 500
            };
            string message = string.Empty;
            ResourceAlreadyExistException innerException = new ResourceAlreadyExistException(message);
            StorageException exception = new StorageException(result, message, innerException);
            Assert.IsFalse(exception.IsNotFoundException());

            result = new RequestResult()
            {
                HttpStatusCode = 404
            };
            exception = new StorageException(result, message, innerException);
            Assert.IsTrue(exception.IsNotFoundException());
        }
        /// <summary>
        /// Translates the specified exception into a storage exception.
        /// </summary>
        /// <param name="ex">The exception to translate.</param>
        /// <param name="reqResult">The request result.</param>
        /// <param name="parseError">The delegate used to parse the error to get extended error information.</param>
        /// <returns>The storage exception.</returns>
        public static StorageException TranslateException(Exception ex, RequestResult reqResult, Func <Stream, StorageExtendedErrorInformation> parseError)
        {
            StorageException storageException;

            try
            {
                if ((storageException = CoreTranslate(ex, reqResult, ref parseError)) != null)
                {
                    return(storageException);
                }

#if !ASPNET_K
                WebException we = ex as WebException;
                if (we != null)
                {
                    HttpWebResponse response = we.Response as HttpWebResponse;
                    if (response != null)
                    {
                        StorageException.PopulateRequestResult(reqResult, response);
#if WINDOWS_RT
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream().AsInputStream());
#else
                        reqResult.ExtendedErrorInformation = parseError(response.GetResponseStream());
#endif
                    }
                }
#endif
            }
            catch (Exception)
            {
                // if there is an error thrown while parsing the service error, just wrap the service error in a StorageException.
                // no op
            }

            // Just wrap in StorageException
            return(new StorageException(reqResult, ex.Message, ex));
        }
        public void 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 = XmlReader.Create(new StringReader(sb.ToString())))
                {
                    retrErrorInfo.ReadXml(reader);
                }

                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorCode, retrErrorInfo.ErrorCode);
                Assert.AreEqual(e.RequestInformation.ExtendedErrorInformation.ErrorMessage, retrErrorInfo.ErrorMessage);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void 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 = XmlReader.Create(new StringReader(sb.ToString())))
                    {
                        requestResult.ReadXml(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")),
                    "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 = XmlReader.Create(new StringReader(sb.ToString())))
                {
                    requestResult.ReadXml(reader);
                }

                Assert.AreEqual(ctx.RequestResults[0].ErrorCode, requestResult.ErrorCode);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        private bool IsHardStorageException(StorageException ex)
        {
            var errorCode = ex.RequestInformation.ExtendedErrorInformation.ErrorCode;
            if (errorCode == StorageErrorCodeStrings.ContainerBeingDeleted
                || errorCode == StorageErrorCodeStrings.ContainerDisabled
                || errorCode == StorageErrorCodeStrings.ContainerNotFound
                || errorCode == StorageErrorCodeStrings.AuthenticationFailed
                || errorCode == StorageErrorCodeStrings.AccountIsDisabled)
            {
                return true;
            }

            return false;
        }
 protected override void DoHandleGetDestinationException(StorageException se)
 {
     if (null != se)
     {
         if (0 == string.Compare(se.Message, Constants.BlobTypeMismatch, StringComparison.OrdinalIgnoreCase))
         {
             // Current use error message to decide whether it caused by blob type mismatch,
             // We should ask xscl to expose an error code for this..
             // Opened workitem 1487579 to track this.
             throw new InvalidOperationException(Resources.DestinationBlobTypeNotMatch);
         }
     }
     else
     {
         if (null != this.SourceBlob && this.SourceBlob.Properties.BlobType != this.destBlob.Properties.BlobType)
         {
             throw new InvalidOperationException(Resources.SourceAndDestinationBlobTypeDifferent);
         }
     }
 }
 private static bool IsErrorStringMatch(StorageException ex, params string[] errorStrings)
 {
     return ex != null && ex.RequestInformation.ExtendedErrorInformation != null && errorStrings.Contains(ex.RequestInformation.ExtendedErrorInformation.ErrorCode);
 }
Example #21
0
 private bool _handleWebException(ICloudBlob blob, StorageException err)
 {
     if (err.RequestInformation.HttpStatusCode == 404)
     {
         _azureDirectory.CreateContainer();
         using (var stream = new MemoryStream())
         using (var writer = new StreamWriter(stream))
         {
             writer.Write(_lockFile);
             blob.UploadFromStream(stream);
         }
         return true;
     }
     return false;
 }
 public BlobStorageException(String messsage, StorageException innerException)
     : base(messsage, innerException)
 {
 }
        private static Mock<ICloudBlob> SetupBlobMock(bool? isFetchSuccess = null)
        {
            Dictionary<string, string> metadata = new Dictionary<string, string>();
            var blobMock = new Mock<ICloudBlob>(MockBehavior.Strict);
            blobMock.Setup(s => s.Metadata).Returns(metadata);
            
            if (isFetchSuccess.HasValue)
            {
                var fetchAttributesSetup = blobMock.Setup(s => s.FetchAttributesAsync(It.IsAny<CancellationToken>()));
                if (isFetchSuccess.Value)
                {
                    fetchAttributesSetup.Returns(Task.FromResult(0));
                }
                else
                {
                    RequestResult requestResult = new RequestResult();
                    requestResult.HttpStatusCode = 404;
                    StorageException blobNotFoundException = new StorageException(requestResult, String.Empty, null);
                    fetchAttributesSetup.Throws(blobNotFoundException);
                }
                fetchAttributesSetup.Verifiable();
            }

            return blobMock;
        }
 public void IsNotFoundExceptionWithoutInfoTest()
 {
     StorageException exception = new StorageException(null, string.Empty, null);
     Assert.IsFalse(exception.IsNotFoundException());
 }
        public async Task TryLockAsync_CreatesBlob_WhenItDoesNotExist()
        {
            CancellationToken cancellationToken = new CancellationToken();
            RequestResult storageResult = new RequestResult
            {
                HttpStatusCode = 404
            };
            StorageException storageException = new StorageException(storageResult, null, null);

            int count = 0;
            _mockStorageBlob.Setup(p => p.AcquireLeaseAsync(_singletonConfig.LockPeriod, null, cancellationToken)).Returns(() =>
            {
                if (count++ == 0)
                {
                    throw storageException;
                }
                return Task.FromResult(TestLeaseId);
            });

            _mockStorageBlob.Setup(p => p.UploadTextAsync(string.Empty, null, null, null, null, cancellationToken)).Returns(Task.FromResult(true));
            _mockStorageBlob.SetupGet(p => p.Metadata).Returns(_mockBlobMetadata);
            _mockStorageBlob.Setup(p => p.SetMetadataAsync(It.Is<AccessCondition>(q => q.LeaseId == TestLeaseId), null, null, cancellationToken)).Returns(Task.FromResult(true));
            _mockStorageBlob.Setup(p => p.ReleaseLeaseAsync(It.Is<AccessCondition>(q => q.LeaseId == TestLeaseId), null, null, cancellationToken)).Returns(Task.FromResult(true));

            SingletonAttribute attribute = new SingletonAttribute();
            SingletonManager.SingletonLockHandle lockHandle = (SingletonManager.SingletonLockHandle)await _singletonManager.TryLockAsync(TestLockId, TestInstanceId, attribute, cancellationToken);

            Assert.Same(_mockStorageBlob.Object, lockHandle.Blob);
            Assert.Equal(TestLeaseId, lockHandle.LeaseId);
            Assert.Equal(1, _mockStorageBlob.Object.Metadata.Keys.Count);
            Assert.Equal(_mockStorageBlob.Object.Metadata[SingletonManager.FunctionInstanceMetadataKey], TestInstanceId);
        }
 public void RepackStorageExceptionWithoutInfoTest()
 {
     StorageException exception = new StorageException(null, string.Empty, null);
     Assert.IsNotNull(exception.RepackStorageException());
 }
Example #27
0
        static bool RecoverReplicationError(StorageException ex, ICloudBlob destBlob, string exceptionMessage)
        {
            bool retval = true;
            switch ((HttpStatusCode)ex.RequestInformation.HttpStatusCode)
            {
                case HttpStatusCode.Conflict:
                case HttpStatusCode.PreconditionFailed:
                    // Destination has been modified - no retry
                    if (destBlob != null)
                    {
                        DashTrace.TraceInformation("A pre-condition was not met attempting to replicate or cleanup blob [{0}] in account [{1}]. This operation will be aborted",
                            destBlob.Name,
                            destBlob.ServiceClient.Credentials.AccountName);
                    }
                    else
                    {
                        DashTrace.TraceWarning("A pre-condition was not met attempting to replicate or cleanup an unknown blob. This operation will be aborted.");
                    }
                    retval = false;
                    break;

                case HttpStatusCode.NotFound:
                    // The source blob could not be found - delete the target to prevent orphaning
                    if (destBlob != null)
                    {
                        DashTrace.TraceInformation("Replication of blob [{0}] to account [{1}] cannot be completed because the source blob does not exist.",
                            destBlob.Name,
                            destBlob.ServiceClient.Credentials.AccountName);
                        CleanupAbortedBlobReplication(destBlob);
                    }
                    else
                    {
                        DashTrace.TraceWarning("Replication of unknown blob cannot be completed because the source blob does not exist.");
                    }
                    retval = false;
                    break;

                default:
                    // Unexpected exceptions are warnings
                    DashTrace.TraceWarning(exceptionMessage);
                    break;
            }
            return retval;
        }
Example #28
0
        /// <summary>
        /// Translates the specified exception into a storage exception.
        /// </summary>
        /// <param name="ex">The exception to translate.</param>
        /// <param name="reqResult">The request result.</param>
        /// <returns>The storage exception.</returns>
        public static StorageException TranslateException(Exception ex, RequestResult reqResult)
        {
            // Dont re-wrap storage exceptions
            if (ex is StorageException)
            {
                return((StorageException)ex);
            }
            else if (ex is TimeoutException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = (int)HttpStatusCode.Unused;
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex));
            }
            else if (ex is ArgumentException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = (int)HttpStatusCode.Unused;
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex)
                {
                    IsRetryable = false
                });
            }
#if RT || XAMARIN
            else if (ex is OperationCanceledException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = 306; // unused
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex));
            }
#elif DNCP
            else
            {
                StorageException tableEx = TableUtilities.TranslateDataServiceClientException(ex, reqResult);

                if (tableEx != null)
                {
                    return(tableEx);
                }
            }
#endif

            WebException we = ex as WebException;
            if (we != null)
            {
                try
                {
                    HttpWebResponse response = we.Response as HttpWebResponse;
                    if (response != null)
                    {
                        reqResult.HttpStatusMessage = response.StatusDescription;
                        reqResult.HttpStatusCode    = (int)response.StatusCode;
                        if (response.Headers != null)
                        {
#if DNCP
                            reqResult.ServiceRequestID = HttpUtility.TryGetHeader(response, Constants.HeaderConstants.RequestIdHeader, null);
                            reqResult.ContentMd5       = HttpUtility.TryGetHeader(response, "Content-MD5", null);
                            string tempDate = HttpUtility.TryGetHeader(response, "Date", null);
                            reqResult.RequestDate = string.IsNullOrEmpty(tempDate) ? DateTime.Now.ToString("R", CultureInfo.InvariantCulture) : tempDate;
                            reqResult.Etag        = response.Headers[HttpResponseHeader.ETag];
#endif
                        }
#if RT
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream().AsInputStream());
#else
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream());
#endif
                    }
                }
                catch (Exception)
                {
                    // no op
                }
            }

            // Not WebException, just wrap in StorageException
            return(new StorageException(reqResult, ex.Message, ex));
        }
 private bool _handleWebException( ICloudBlob blob, StorageException err, string _lockFile )
 {
     if ( err.RequestInformation.HttpStatusCode == 404 ) {
         this.blobContainer.CreateIfNotExists();
         using ( MemoryStream stream = new MemoryStream() ) {
             using ( StreamWriter writer = new StreamWriter( stream ) ) {
                 writer.Write( _lockFile );
                 blob.UploadFromStream( stream );
             }
         }
         return true;
     }
     return false;
 }
 public BlobStorageException(String messsage, String name, String path, StorageException innerException)
     : base(messsage, innerException)
 {
     Name = name;
     Path = path;
 }
 public void HardStorageExceptionCaughtWritingToBlob(StorageException storageException, CloudBlobClient blobClient, string containerName)
 {
     HardStorageExceptionCaughtWritingToBlob_Inner(storageException.ToString(), GetStorageAccountName(blobClient), containerName, storageException.RequestInformation.ExtendedErrorInformation.ErrorCode);
 }
 protected override void DoHandleGetDestinationException(StorageException se)
 {
 }
 public TableStorageException(String messsage, StorageException innerException, string tableName)
     : base(messsage, innerException)
 {
     TableName = tableName;
 }
 public ConcurrentIndexException(string message, StorageException ex) : base(message, ex)
 {            
 }
 public static bool IsDoesntExist(AzureStorageException ex)
 {
     return(ex.RequestInformation?.HttpStatusCode == 404);
 }
 public TestableStorageClientException(StorageException ex)
 {
     ErrorCode = ex.RequestInformation.ExtendedErrorInformation.ErrorCode;
 }
        private static string GetStorageExceptionHttpStatusMessage(StorageException storageException)
        {
            var storageRequestInfo = storageException.RequestInformation;

            if (storageRequestInfo == null)
                return (string.Empty);

            return (storageRequestInfo.HttpStatusMessage);
        }