/// <summary>
        /// Works with AggregateException or "Exception" just pass it in :)
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="correctCode"></param>
        public static void AssertIsBatchExceptionAndHasCorrectAzureErrorCode(Exception ex, string correctCode, ITestOutputHelper outputHelper)
        {
            Exception theOneInner = ex;

            if (ex is AggregateException)
            {
                AggregateException ae = (AggregateException)ex;

                // some ugly mechanics all for confirming we get the correct exception and it has the correct "message"
                // there can be only one
                Assert.Single(ae.InnerExceptions);

                // get the one exception
                theOneInner = ae.InnerExceptions[0];
            }

            if (!(theOneInner is Microsoft.Azure.Batch.Common.BatchException))
            {
                outputHelper.WriteLine(string.Format("AssertIsBatchExceptionAndHasCorrectAzureErrorCode: incorrect exception: {0}", ex.ToString()));
            }

            // it must have the correct type
            Assert.IsAssignableFrom <Microsoft.Azure.Batch.Common.BatchException>(theOneInner);

            Microsoft.Azure.Batch.Common.BatchException be = (Microsoft.Azure.Batch.Common.BatchException)theOneInner;

            // whew we got it!  check the message
            Assert.Equal(expected: correctCode, actual: be.RequestInformation.BatchError.Code);
        }
Ejemplo n.º 2
0
        internal static bool ShouldRetry(Exception exception, OperationContext operationContext, int maxRetries)
        {
            bool shouldRetry;
            int  currentRetryCount = operationContext.RequestResults.Count - 1;

            if (currentRetryCount < maxRetries)
            {
                BatchException batchException = exception as BatchException;
                if (batchException != null)
                {
                    int statusCode = (int)batchException.RequestInformation.HttpStatusCode;

                    if ((statusCode >= 400 && statusCode < 500 &&
                         statusCode != 408 && // Timeout
                         statusCode != 429   // Too many reqeuests
                         ) ||
                        statusCode == 501 || // Not Implemented
                        statusCode == 505    // Version Not Supported
                        )
                    {
                        shouldRetry = false;
                    }
                    else
                    {
                        shouldRetry = true;
                    }
                }
                else
                {
                    //Note that we use a disallow list here in order to have the most robust retry policy --
                    //attempting to guess all the possible exception types thrown by the network stack to craft a
                    //white list is error prone and so we avoid it.
                    if (exception is Microsoft.Rest.ValidationException)
                    {
                        //TODO: Consider adding SystemException to disallow list, but SystemException includes TimeoutException which it seems we probably want to retry on
                        shouldRetry = false;
                    }
                    else
                    {
                        //Retry on all other exceptions which aren't a BatchException since those exceptions
                        //mean we haven't heard back from the server
                        shouldRetry = true;
                    }
                }
            }
            else
            {
                shouldRetry = false; //We are out of retries to perform so we will not retry
            }

            return(shouldRetry);
        }