Exemplo n.º 1
0
 private bool _handleWebException(ICloudBlob blob, StorageException err)
 {
     if (err.RequestInformation.HttpStatusCode == 404 || err.RequestInformation.HttpStatusCode == 409)
     {
         _azureDirectory.CreateContainer();
         using (var stream = new MemoryStream())
             using (var writer = new StreamWriter(stream))
             {
                 writer.Write(_lockFile);
                 try
                 {
                     blob.UploadFromStream(stream);
                 }
                 catch (Exception ex) {
                     return(false);
                 }
             }
         return(true);
     }
     return(false);
 }
Exemplo n.º 2
0
            /// <summary>
            /// Invoke invalidQueueHandler if failed to access the queue.
            /// </summary>
            /// <remarks>
            /// Notice: Only handle the case that queue is not found now. May
            /// add more error handlings for specific queue issues if necessary.
            /// </remarks>
            /// <param name="e">exception happens when access the queue</param>
            private void HandleInvalidQueue(StorageException e)
            {
                SessionBase.TraceSource.TraceEvent(
                    TraceEventType.Error,
                    0,
                    "StorageException, worker {0}, queue {1}, error code {2}, {3}",
                    this.workerId,
                    this.queue.Name,
                    BurstUtility.GetStorageErrorCode(e),
                    e);

                if (BurstUtility.IsQueueNotFound(e))
                {
                    // Invoke invalidQueueHandler if the exception indicates
                    // that the queue is not found.
                    if (this.invalidQueueHandler != null)
                    {
                        this.invalidQueueHandler(e);
                    }
                }
            }
Exemplo n.º 3
0
            /// <summary>
            /// Invoke invalidQueueHandler if failed to access the queue.
            /// </summary>
            /// <remarks>
            /// Notice: Only handle the case that queue is not found now. May
            /// add more error handlings for specific queue issues if necessary.
            /// </remarks>
            /// <param name="e">exception happens when access the queue</param>
            private void HandleInvalidQueue(StorageException e)
            {
                TraceUtils.TraceError(
                    "MessageRetriever.Worker",
                    "HandleInvalidQueue",
                    "StorageException, worker {0}, queue {1}, error code {2}, {3}",
                    this.workerId,
                    this.queue.Name,
                    BurstUtility.GetStorageErrorCode(e),
                    e);

                if (BurstUtility.IsQueueNotFound(e))
                {
                    // Invoke invalidQueueHandler if the exception indicates
                    // that the queue is not found.
                    if (this.invalidQueueHandler != null)
                    {
                        this.invalidQueueHandler(e);
                    }
                }
            }
Exemplo n.º 4
0
        private static void BeginGetResponse <T>(ExecutionState <T> executionState)
        {
            executionState.CurrentOperation = ExecutorOperation.BeginGetResponse;
            Logger.LogInformational(executionState.OperationContext, SR.TraceGetResponse);

            try
            {
                APMWithTimeout.RunWithTimeout(
                    executionState.Req.BeginGetResponse,
                    Executor.EndGetResponse <T>,
                    Executor.AbortRequest <T>,
                    executionState,
                    executionState.RemainingTimeout);
            }
            catch (Exception ex)
            {
                Logger.LogWarning(executionState.OperationContext, SR.TraceGetResponseError, ex.Message);
                executionState.ExceptionRef = StorageException.TranslateException(ex, executionState.Cmd.CurrentResult);
                Executor.EndOperation(executionState);
            }
        }
        /// <summary>
        /// Handles the Azure Storage exceptions
        /// </summary>
        /// <param name="partitionId">The partition id</param>
        /// <param name="exception">The storage exception that was raised</param>
        /// <param name="logger">The logger to write debugging and diagnostics information to</param>
        /// <returns>The exception that was generated or passed in</returns>
        public static Exception CheckForLeaseLostException(string partitionId, StorageException exception, ILogger logger)
        {
            Exception results = exception;

            if (exception.RequestInformation.HttpStatusCode == 409 || exception.RequestInformation.HttpStatusCode == 412)
            {
                logger.LogError(exception, "HandleStorageException (conflict or precondition failed) - HttpStatusCode: Partition Id: {partitionId}, HttpStatusCode: {status}, errorCode: {errorCode}, errorMessage: {errorMessage}", partitionId, exception.RequestInformation.HttpStatusCode, exception.RequestInformation.ErrorCode, exception.RequestInformation.ExtendedErrorInformation?.ErrorMessage);

                if (exception.RequestInformation.ErrorCode == null || exception.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseLost || exception.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithLeaseOperation ||
                    exception.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithBlobOperation)
                {
                    results = new LeaseLostException(partitionId, exception);
                }
            }
            else
            {
                logger.LogError(exception, "HandleStorageException - HttpStatusCode: Partition Id: {partitionId}, HttpStatusCode: {status}, errorCode: {errorCode}, errorMessage: {errorMessage}", partitionId, exception.RequestInformation.HttpStatusCode, exception.RequestInformation.ErrorCode, exception.RequestInformation.ExtendedErrorInformation?.ErrorMessage);
            }

            return(results);
        }
Exemplo n.º 6
0
        internal RetriableOperationExceptionHandler.Response CreateTableExceptionHandler(Exception e)
        {
            this.traceSource.WriteError(
                this.logSourceId,
                "Exception encountered when attempting to create table {0} in storage account {1}. Exception information: {2}",
                this.tableUploadSettings.TableName,
                this.tableUploadSettings.StorageAccountFactory.Connection.AccountName,
                e);

            if (e is StorageException)
            {
                // If this was a network error, we'll retry later. Else, we'll
                // just give up.
                StorageException eSpecific = (StorageException)e;
                if (Utility.IsNetworkError(eSpecific.RequestInformation.HttpStatusCode))
                {
                    return(RetriableOperationExceptionHandler.Response.Retry);
                }
            }
            return(RetriableOperationExceptionHandler.Response.Abort);
        }
Exemplo n.º 7
0
        private static bool IgnoreDirectoryCreationError(StorageException se)
        {
            if (null == se)
            {
                return(false);
            }

            if (Utils.IsExpectedHttpStatusCodes(se, HttpStatusCode.Forbidden))
            {
                return(true);
            }

            if (null != se.RequestInformation &&
                se.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict &&
                string.Equals(se.RequestInformation.ErrorCode, "ResourceAlreadyExists"))
            {
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Attempts to return a HttpStatusCode that represents the information present in the
        /// <see cref="StorageException"/> object. If there is no information, returns a <see cref="HttpStatusCode.InternalServerError"/>.
        /// </summary>
        /// <param name="storageException"><see cref="StorageException"/> object that needs to be parsed.</param>
        /// <returns>A corresponding <see cref="HttpStatusCode"/></returns>
        public static HttpStatusCode ParseStorageException(StorageException storageException)
        {
            EnsureArg.IsNotNull(storageException, nameof(storageException));

            // Let's check whether there is a valid http status code that we can return.
            if (storageException.RequestInformation != null)
            {
                if (Enum.IsDefined(typeof(HttpStatusCode), storageException.RequestInformation.HttpStatusCode))
                {
                    return((HttpStatusCode)storageException.RequestInformation.HttpStatusCode);
                }
            }

            // We don't have a valid status code. Let's look at the message to try to get more information.
            if (storageException.Message.Contains("No such host is known", StringComparison.OrdinalIgnoreCase))
            {
                return(HttpStatusCode.BadRequest);
            }

            return(HttpStatusCode.InternalServerError);
        }
Exemplo n.º 9
0
        public static string GetStorageExceptionAdditionalDetails(this StorageException x)
        {
            var info = x.RequestInformation.ExtendedErrorInformation;

            if (info == null)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            sb.AppendLine("Request ID: " + x.RequestInformation.ServiceRequestID);
            sb.AppendLine("Error Code: " + info.ErrorCode);
            sb.Append("Error Message: " + info.ErrorMessage);
            foreach (var additionalDetail in info.AdditionalDetails)
            {
                sb.AppendLine();
                sb.Append(additionalDetail.Key + " : " + additionalDetail.Value);
            }
            return(sb.ToString());
        }
Exemplo n.º 10
0
        protected override async Task <Exception> GetExceptionAsync(HttpResponseMessage response)
        {
            string responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                string key = response.RequestMessage.RequestUri.AbsolutePath;

                if (key.Length > 0 && key[0] == '/')
                {
                    key = key.Substring(1);
                }

                throw StorageException.NotFound(key);
            }

            try
            {
                // Wasabi returns a non-standard ErrorResponse
                if (responseText.Contains("<ErrorResponse"))
                {
                    var errorResponse = ResponseHelper <S3ErrorResponse> .ParseXml(responseText);

                    throw new S3Exception(
                              error: errorResponse.Error,
                              statusCode: response.StatusCode
                              );
                }
                if (responseText.Contains("<Error>"))
                {
                    throw new S3Exception(
                              error: S3Error.ParseXml(responseText),
                              statusCode: response.StatusCode
                              );
                }
            }
            catch { }

            throw new S3Exception("Unexpected S3 error. " + response.StatusCode + ":" + responseText, response.StatusCode);
        }
Exemplo n.º 11
0
            public async Task ThrowsExceptionWhenValidationSetPackageAndDestinationPackageDoesNotMatchETag()
            {
                ValidationSet.PackageETag = "\"some-etag\"";
                Package.PackageStatusKey  = PackageStatus.Available;
                var expected = new StorageException(new RequestResult {
                    HttpStatusCode = (int)HttpStatusCode.PreconditionFailed
                }, "Changed!", null);

                PackageFileServiceMock
                .Setup(x => x.DoesValidationSetPackageExistAsync(It.IsAny <PackageValidationSet>()))
                .ReturnsAsync(true);
                PackageFileServiceMock
                .Setup(x => x.CopyValidationSetPackageToPackageFileAsync(It.IsAny <PackageValidationSet>(), It.IsAny <IAccessCondition>()))
                .Throws(expected);

                var actual = await Assert.ThrowsAsync <StorageException>(
                    () => Target.SetStatusAsync(PackageValidatingEntity, ValidationSet, PackageStatus.Available));

                Assert.Same(expected, actual);

                PackageFileServiceMock.Verify(
                    x => x.UpdatePackageBlobMetadataInValidationSetAsync(It.IsAny <PackageValidationSet>()),
                    Times.Once);
                PackageFileServiceMock.Verify(
                    x => x.CopyValidationSetPackageToPackageFileAsync(ValidationSet, It.Is <IAccessCondition>(y => y.IfMatchETag == "\"some-etag\"")),
                    Times.Once);
                PackageFileServiceMock.Verify(
                    x => x.UpdatePackageBlobMetadataInValidationAsync(It.IsAny <PackageValidationSet>()),
                    Times.Never);
                PackageServiceMock.Verify(
                    x => x.UpdateStatusAsync(It.IsAny <Package>(), It.IsAny <PackageStatus>(), It.IsAny <bool>()),
                    Times.Never);
                PackageFileServiceMock.Verify(
                    x => x.DeleteValidationPackageFileAsync(It.IsAny <PackageValidationSet>()),
                    Times.Never);
                PackageFileServiceMock.Verify(
                    x => x.DeletePackageForValidationSetAsync(It.IsAny <PackageValidationSet>()),
                    Times.Never);
            }
Exemplo n.º 12
0
        public void LoadAssessmentSection_LoadingProjectThrowsException_ThrowsLoadAssessmentSectionExceptionAndLogsError()
        {
            // Setup
            const string exceptionMessage = "StorageException";
            var          storageException = new StorageException(exceptionMessage);

            var mocks        = new MockRepository();
            var storeProject = mocks.StrictMock <IStoreProject>();

            storeProject.Expect(sp => sp.LoadProject(null))
            .IgnoreArguments()
            .Throw(storageException);
            mocks.ReplayAll();

            var service = new LoadAssessmentSectionService(storeProject);

            LoadAssessmentSectionException exception = null;

            // Call
            void Call()
            {
                try
                {
                    service.LoadAssessmentSection(string.Empty);
                }
                catch (LoadAssessmentSectionException e)
                {
                    exception = e;
                }
            }

            // Assert
            var expectedLogMessage = new Tuple <string, LogLevelConstant>(exceptionMessage, LogLevelConstant.Error);

            TestHelper.AssertLogMessageWithLevelIsGenerated(Call, expectedLogMessage);
            Assert.AreEqual(storageException, exception.InnerException);
            Assert.AreEqual(storageException.Message, exception.Message);
            mocks.VerifyAll();
        }
Exemplo n.º 13
0
        public static string DumpStorageExceptionErrorDetails(StorageException storageException)
        {
            if (storageException == null)
            {
                return(string.Empty);
            }

            var message = new StringBuilder();

            message.AppendLine("StorageException details");
#if NETSTANDARD
            message.Append("Error.Code:").AppendLine(storageException.RequestInformation.ErrorCode);
#else
            message.Append("Error.Code:").AppendLine(storageException.RequestInformation.ExtendedErrorInformation.ErrorCode);
#endif
            message.Append("ErrorMessage:").AppendLine(storageException.RequestInformation.ExtendedErrorInformation.ErrorMessage);
            foreach (var key in storageException.RequestInformation.ExtendedErrorInformation.AdditionalDetails.Keys)
            {
                message.Append(key).Append(":").Append(storageException.RequestInformation.ExtendedErrorInformation.AdditionalDetails[key]);
            }
            return(message.ToString());
        }
        Exception HandleStorageException(string partitionId, StorageException se)
        {
            ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.HostName, partitionId, "HandleStorageException - HttpStatusCode " + se.RequestInformation.HttpStatusCode);
            if (se.RequestInformation.HttpStatusCode == 409 || // conflict
                se.RequestInformation.HttpStatusCode == 412)   // precondition failed
            {
                ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.HostName, partitionId,
                                                                 "HandleStorageException - Error code: " + se.RequestInformation.ErrorCode);
                ProcessorEventSource.Log.AzureStorageManagerInfo(this.host.HostName, partitionId,
                                                                 "HandleStorageException - Error message: " + se.RequestInformation.ExtendedErrorInformation?.ErrorMessage);

                if (se.RequestInformation.ErrorCode == null ||
                    se.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseLost ||
                    se.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithLeaseOperation ||
                    se.RequestInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMismatchWithBlobOperation)
                {
                    return(new LeaseLostException(partitionId, se));
                }
            }

            return(se);
        }
Exemplo n.º 15
0
        public async Task <Blob> GetObject(string key)
        {
            var url         = $"{GetUrl()}/{key}";
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url)
            {
                Headers =
                {
                    { "x-amz-date",           RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256", payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(requestMessage.Headers);

            var client = GetClient();

            client.DefaultRequestHeaders.Authorization = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            var response = await client.SendAsync(requestMessage, CancellationToken);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }

            if (response.IsSuccessStatusCode == false)
            {
                throw StorageException.FromResponseMessage(response);
            }

            var data = await response.Content.ReadAsStreamAsync();

            var metadataHeaders = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault());

            return(new Blob(data, metadataHeaders));
        }
Exemplo n.º 16
0
        private async Task <bool> VaultExists()
        {
            var url         = GetUrl();
            var now         = SystemTime.UtcNow;
            var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

            var content = new HttpRequestMessage(HttpMethods.Get, url)
            {
                Headers =
                {
                    { "x-amz-glacier-version", "2012-06-01"                        },
                    { "x-amz-date",            RavenAwsHelper.ConvertToString(now) },
                    { "x-amz-content-sha256",  payloadHash                         }
                }
            };

            var headers = ConvertToHeaders(content.Headers);

            var client = GetClient();
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Get, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = await client.SendAsync(content, CancellationToken);

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }

            await response.Content.ReadAsStringAsync();

            throw StorageException.FromResponseMessage(response);
        }
 private bool IsCircuitBreakerAzureBlobException(StorageException ex)
 {
     if (ex.RequestInformation.HttpStatusCode == 503)
     {
         /* Returned from blob in the following variations:
          *  "The server is busy."
          *  "The server is currently unable to receive requests. Please retry your request"
          *  "Ingress is over the account limit."
          *  "Egress is over the account limit."
          *  "Operations per second is over the account limit."
          */
         return(true);
     }
     else if (ex.RequestInformation.HttpStatusCode == 500)
     {
         /* Returned from blob in the following variations:
          *  "The operation could not be completed within the permitted time."
          */
         return(true);
     }
     return(false);
 }
Exemplo n.º 18
0
        public async Task ThrowStorageExceptionInDeleteLogsTime()
        {
            _mockIngestion.Setup(ingestion => ingestion.IsEnabled).Returns(true);
            var log = new TestLog();
            var storageException = new StorageException();

            // Make sure that the storage exception is "observed" to
            // avoid the exception propagating to the point where the
            // test fails.
            TaskScheduler.UnobservedTaskException += (sender, e) =>
            {
                if (e.Exception.InnerException == storageException)
                {
                    e.SetObserved();
                }
            };
            var storage = new Mock <IStorage>();

            storage.Setup(s => s.DeleteLogs(It.IsAny <string>(), It.IsAny <string>())).Throws(storageException);
            storage.Setup(s => s.GetLogsAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <List <Log> >()))
            .Callback((string channelName, int limit, List <Log> logs) => logs.Add(log))
            .Returns(() => Task.FromResult("test-batch-id"));

            var appSecret = Guid.NewGuid().ToString();

            _channel = new Channel(ChannelName, 1, TimeSpan.FromSeconds(1), 1, appSecret, _mockIngestion.Object, storage.Object);
            SetupEventCallbacks();

            // Shutdown channel and store some log
            await _channel.ShutdownAsync();

            await _channel.EnqueueAsync(log);

            _channel.SetEnabled(true);

            VerifySentLog(1);

            // Not throw any exception
        }
Exemplo n.º 19
0
        public async Task HandleStorageExceptionWhenSendingLogsWithEvents()
        {
            try
            {
                var log = new TestLog();
                var storageException = new StorageException();

                // Make sure that the storage exception is "observed" to
                // avoid the exception propagating to the point where the
                // test fails.
                TaskScheduler.UnobservedTaskException += (sender, e) =>
                {
                    if (e.Exception.InnerException == storageException)
                    {
                        e.SetObserved();
                    }
                };
                var storage = new Mock <IStorage>();
                storage.Setup(s => s.DeleteLogs(It.IsAny <string>())).Throws(storageException);
                storage.Setup(s => s.GetLogsAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <List <Log> >()))
                .Callback((string channelName, int limit, List <Log> logs) => logs.Add(log))
                .Returns(() => Task.FromResult("test-batch-id"));

                // Prepare data.
                var     appSecret = Guid.NewGuid().ToString();
                Channel channel   = new Channel(ChannelName, MaxLogsPerBatch, new TimeSpan(), MaxParallelBatches,
                                                appSecret, _mockIngestion.Object, storage.Object);

                // Disable and enable channel.
                channel.FailedToSendLog += (sender, args) => { };
                channel.SetEnabled(false);
                channel.SetEnabled(true);
            }
            catch (StorageException e)
            {
                // Crash test if was generated StorageException error.
                Assert.Fail();
            }
        }
Exemplo n.º 20
0
        internal virtual async Task ExecuteBatchAndCreateTableIfNotExistsAsync(
            Dictionary <string, IStorageTableOperation> partition, CancellationToken cancellationToken)
        {
            IStorageTableBatchOperation batch = _table.CreateBatch();

            foreach (var operation in partition.Values)
            {
                batch.Add(operation);
            }

            if (batch.Count > 0)
            {
                StorageException exception = null;

                try
                {
                    // Commit the batch
                    await _table.ExecuteBatchAsync(batch, cancellationToken);
                }
                catch (StorageException e)
                {
                    if (!e.IsNotFoundTableNotFound())
                    {
                        throw;
                    }

                    exception = e;
                }

                if (exception != null)
                {
                    // Make sure the table exists
                    await _table.CreateIfNotExistsAsync(cancellationToken);

                    // Commit the batch
                    await _table.ExecuteBatchAsync(batch, cancellationToken);
                }
            }
        }
Exemplo n.º 21
0
        protected Exception HandleStorageException(StorageException exception, IReadOnlyList <StateEntry> stateEntries)
        {
            var statusCode = exception.RequestInformation.HttpStatusCode;

            if (statusCode == (int)HttpStatusCode.PreconditionFailed)
            {
                return(new DbUpdateConcurrencyException(Strings.ETagPreconditionFailed, stateEntries));
            }
            if (statusCode == (int)HttpStatusCode.NotFound)
            {
                var extendedErrorCode = exception.RequestInformation.ExtendedErrorInformation.ErrorCode;
                if (extendedErrorCode == StorageErrorCodes.ResourceNotFound)
                {
                    return(new DbUpdateConcurrencyException(Strings.ResourceNotFound, stateEntries));
                }
                if (extendedErrorCode == StorageErrorCodes.TableNotFoundError)
                {
                    return(new DbUpdateException(Strings.TableNotFound, stateEntries));
                }
            }
            return(new DbUpdateException(Strings.SaveChangesFailed, exception, stateEntries));
        }
Exemplo n.º 22
0
        public void DeleteBlobs(List <string> blobs)
        {
            if (blobs.Count == 0)
            {
                return;
            }

            var client = GetClient();

            foreach (var blob in blobs)
            {
                var url = GetUrl($"{_serverUrlForContainer}/{blob}");

                var now = SystemTime.UtcNow;

                var requestMessage = new HttpRequestMessage(HttpMethods.Delete, url)
                {
                    Headers =
                    {
                        { "x-ms-date",    now.ToString("R")   },
                        { "x-ms-version", AzureStorageVersion }
                    }
                };

                SetAuthorizationHeader(client, HttpMethods.Delete, url, requestMessage.Headers);
                var response = client.SendAsync(requestMessage, CancellationToken).Result;
                if (response.IsSuccessStatusCode)
                {
                    continue;
                }

                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    continue;
                }

                throw StorageException.FromResponseMessage(response);
            }
        }
        /// <summary>
        /// Validates the transfer location.
        /// </summary>
        public override void Validate()
        {
            try
            {
                this.Blob.Container.FetchAttributesAsync(null, Transfer_RequestOptions.DefaultBlobRequestOptions, Utils.GenerateOperationContext(null)).Wait();
            }
            catch (AggregateException e)
            {
                StorageException innnerException = e.Flatten().InnerExceptions[0] as StorageException;

                // If doesn't have permission to access the container, it might still have proper permission to acess blobs in the container.
                // Here swallows the errors that could be possible thrown out when it cannot access the container.
                // With some older version of SAS token, it reports error of NotFound (404),
                // with other newer version of SAS token, it reports error of Forbidden (403)
                // swallows both here.
                if (this.Blob.Container.ServiceClient.Credentials.IsSharedKey ||
                    !Utils.IsExpectedHttpStatusCodes(innnerException, HttpStatusCode.Forbidden, HttpStatusCode.NotFound))
                {
                    throw;
                }
            }
        }
Exemplo n.º 24
0
        /// <summary>
        ///     Enqueue a message async
        /// </summary>
        /// <param name="message">
        ///     a message
        /// </param>
        /// <returns>
        ///     true on success
        /// </returns>
        public Task EnqueueAsync(CloudQueueMessage message)
        {
            Task task = Task.Factory.FromAsync(this.cloudQueue.BeginAddMessage, this.cloudQueue.EndAddMessage, message, this.cloudQueue).ContinueWith(
                (contTask) =>
            {
                Interlocked.Decrement(ref this.pendingWrites);
                StorageException storageException = (contTask.Exception != null) ? contTask.Exception.InnerException as StorageException : null;
                if (storageException != null && storageException.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound)
                {
                    // attempt to recreate the queue
                    this.cloudQueue.CreateIfNotExists();
                    this.cloudQueue.AddMessage(message);
                }
                else if (contTask.Exception != null)
                {
                    throw contTask.Exception.InnerException;
                }
            });

            Interlocked.Increment(ref this.pendingWrites);
            return(task);
        }
Exemplo n.º 25
0
        private bool ContainerExists()
        {
            var url            = GetUrl(_serverUrlForContainer, "restype=container");
            var now            = SystemTime.UtcNow;
            var requestMessage = new HttpRequestMessage(HttpMethods.Get, url)
            {
                Headers =
                {
                    { "x-ms-date",    now.ToString("R")   },
                    { "x-ms-version", AzureStorageVersion }
                }
            };

            var client = GetClient();

            SetAuthorizationHeader(client, HttpMethods.Get, url, requestMessage.Headers);

            var response = client.SendAsync(requestMessage, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return(true);
            }

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }

            var error = StorageException.FromResponseMessage(response);

            if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                throw new UnauthorizedAccessException(error.ResponseString);
            }

            throw error;
        }
Exemplo n.º 26
0
            public async Task IgnoresPackageNotFoundExceptions()
            {
                var leaf               = CreateCatalogLeaf("packageid", "1.2.3");
                var packageUri         = new Uri("https://package/url");
                var cloudBlockBlobMock = new Mock <ICloudBlockBlob>();

                PackageStorageMock
                .Setup(ps => ps.ResolveUri(It.IsAny <string>()))
                .Returns(packageUri);
                PackageStorageMock
                .Setup(ps => ps.GetCloudBlockBlobReferenceAsync(packageUri))
                .ReturnsAsync(cloudBlockBlobMock.Object);
                var exception = new StorageException(new RequestResult {
                    HttpStatusCode = 404
                }, message: "Exception!!1", inner: null);

                cloudBlockBlobMock
                .Setup(cbb => cbb.GetStreamAsync(It.IsAny <CancellationToken>()))
                .ThrowsAsync(exception);

                await Target.ProcessPackageDetailsLeafAsync(
                    DestinationStorageMock.Object,
                    IconCacheStorageMock.Object,
                    leaf,
                    null,
                    "icon.png",
                    CancellationToken.None);

                IconProcessorMock
                .Verify(ip => ip.CopyEmbeddedIconFromPackageAsync(
                            It.IsAny <Stream>(),
                            It.IsAny <string>(),
                            It.IsAny <IStorage>(),
                            It.IsAny <string>(),
                            It.IsAny <CancellationToken>(),
                            It.IsAny <string>(),
                            It.IsAny <string>()), Times.Never);
            }
        public async Task ExecuteAsync_WithConflictingInsert_ThrowsException()
        {
            // ARRANGE
            var ts = new TestState();
            await ts.Table.ExecuteAsync(TableOperation.Insert(ts.EntityA), CancellationToken.None);

            ts.EntityA.ETag = null;

            // ACT
            Func <Task> action = () => ts.Table.ExecuteAsync(TableOperation.Insert(ts.EntityA), CancellationToken.None);

            // ASSERT
            StorageException exception = action.ShouldThrow <StorageException>().And;

            exception.InnerException.Should().NotBeNull();
            exception.InnerException.Should().BeOfType <WebException>();
            var innerException = ((WebException)exception.InnerException);

            innerException.Response.Should().BeOfType <HttpWebResponse>();
            var response = ((HttpWebResponse)innerException.Response);

            response.StatusCode.Should().Be(HttpStatusCode.Conflict);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Returns the error code from storage exception, or null.
        /// </summary>
        /// <param name="exception">The storage exception.</param>
        /// <returns>The error code, or null.</returns>
        public static string GetErrorCode(this StorageException exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            RequestResult result = exception.RequestInformation;

            if (result == null)
            {
                return(null);
            }

            StorageExtendedErrorInformation extendedInformation = result.ExtendedErrorInformation;

            if (extendedInformation == null)
            {
                return(null);
            }

            return(extendedInformation.ErrorCode);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Helper to unwrap the StorageException and log additional details
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="logger"></param>
        internal static void LogStorageException(string context, StorageException ex, ILogger logger, bool retryable)
        {
            var exMsg = ex.RequestInformation.ExtendedErrorInformation == null?
                        ex.RequestInformation.Exception.ToString() : ex.RequestInformation.ExtendedErrorInformation.ErrorMessage;

            var errCode = ex.RequestInformation.ErrorCode;

            logger.Log(retryable ? Extensions.Logging.LogLevel.Warning : Extensions.Logging.LogLevel.Error,
                       $"StorageException occured in context {context}. Exception message is {ex.Message} with Error code {errCode} and Extended Error Message {exMsg}.");

            if (!(ex.RequestInformation.ExtendedErrorInformation is null))
            {
                foreach (var addDetails in ex.RequestInformation.ExtendedErrorInformation.AdditionalDetails)
                {
                    logger.Log(retryable ? Extensions.Logging.LogLevel.Warning : Extensions.Logging.LogLevel.Error,
                               $"{addDetails.Key}: {addDetails.Value}");
                }
            }

            logger.LogDebug(ex, $"Storage Exception details for context {context}", null);

            return;
        }
Exemplo n.º 30
0
        public void PutVault()
        {
            var url     = GetUrl();
            var now     = SystemTime.UtcNow;
            var content = new HttpRequestMessage(HttpMethods.Put, url);

            UpdateHeaders(content.Headers, now, null);

            var headers = ConvertToHeaders(content.Headers);
            var client  = GetClient();
            var authorizationHeaderValue = CalculateAuthorizationHeaderValue(HttpMethods.Put, url, now, headers);

            client.DefaultRequestHeaders.Authorization = authorizationHeaderValue;

            var response = client.SendAsync(content, CancellationToken).Result;

            if (response.IsSuccessStatusCode)
            {
                return;
            }

            throw StorageException.FromResponseMessage(response);
        }
        private bool HandleStartCopyResult(StorageException se)
        {
            if (null != se)
            {
                if (null != se.RequestInformation
                && null != se.RequestInformation.ExtendedErrorInformation
                && BlobErrorCodeStrings.PendingCopyOperation == se.RequestInformation.ExtendedErrorInformation.ErrorCode)
                {
                    CopyState copyState = this.FetchCopyStateAsync().Result;

                    if (null == copyState)
                    {
                        return false;
                    }

                    string baseUriString = copyState.Source.GetComponents(
                        UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);

                    Uri sourceUri = this.GetSourceUri();

                    string ourBaseUriString = sourceUri.GetComponents(UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);

                    DateTimeOffset? baseSnapshot = null;
                    DateTimeOffset? ourSnapshot = null == this.SourceBlob ? null : this.SourceBlob.SnapshotTime;

                    string snapshotString;
                    if (ParseQueryString(copyState.Source.Query).TryGetValue("snapshot", out snapshotString))
                    {
                        if (!string.IsNullOrEmpty(snapshotString))
                        {
                            DateTimeOffset snapshotTime;
                            if (DateTimeOffset.TryParse(
                                snapshotString,
                                CultureInfo.CurrentCulture,
                                DateTimeStyles.AdjustToUniversal,
                                out snapshotTime))
                            {
                                baseSnapshot = snapshotTime;
                            }
                        }
                    }

                    if (!baseUriString.Equals(ourBaseUriString) ||
                        !baseSnapshot.Equals(ourSnapshot))
                    {
                        return false;
                    }

                    if (string.IsNullOrEmpty(this.TransferJob.CopyId))
                    {
                        this.TransferJob.CopyId = copyState.CopyId;
                    }
                }
                else
                {
                    return false;
                }
            }

            this.TransferJob.Status = TransferJobStatus.Monitor;
            this.state = State.GetCopyState;
            this.hasWork = true;
            return true;
        }
        private void VerifyBackoffTimeOverflow(IRetryPolicy retryPolicy, int maxAttempts)
        {
            StorageException e = new StorageException();
            OperationContext context = new OperationContext();
            TimeSpan retryInterval;
            TimeSpan previousRetryInterval = TimeSpan.FromMilliseconds(1); // larger than zero to ensure we never get zero back

            for (int i = 0; i < maxAttempts; i++)
            {
                Assert.IsTrue(retryPolicy.ShouldRetry(i, (int)HttpStatusCode.InternalServerError, e, out retryInterval, context), string.Format("Attempt: {0}", i));
                Assert.IsTrue(retryInterval >= previousRetryInterval, string.Format("Retry Interval: {0}, Previous Retry Interval: {1}, Attempt: {2}", retryInterval, previousRetryInterval, i));
                previousRetryInterval = retryInterval;
            }

            Assert.IsFalse(retryPolicy.ShouldRetry(maxAttempts, (int)HttpStatusCode.InternalServerError, e, out retryInterval, context));
        }
 private static void HandleFetchSourceAttributesException(StorageException e)
 {
     // Getting a storage exception is expected if the source doesn't
     // exist. For those cases that indicate the source doesn't exist
     // we will set a specific error state.
     if (null != e.RequestInformation &&
         e.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotFound)
     {
         throw new InvalidOperationException(Resources.SourceDoesNotExistException);
     }
 }
 protected abstract void DoHandleGetDestinationException(StorageException se);