< Summary

Class:Azure.Core.ResponseClassifier
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\ResponseClassifier.cs
Covered lines:9
Uncovered lines:1
Coverable lines:10
Total lines:61
Line coverage:90% (9 of 10)
Covered branches:18
Total branches:20
Branch coverage:90% (18 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
IsRetriableResponse(...)-66.67%80%
IsRetriableException(...)-100%100%
IsRetriable(...)-100%100%
IsErrorResponse(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\ResponseClassifier.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.IO;
 6
 7namespace Azure.Core
 8{
 9    /// <summary>
 10    /// A type that analyzes HTTP responses and exceptions and determines if they should be retried.
 11    /// </summary>
 12    public class ResponseClassifier
 13    {
 14        /// <summary>
 15        /// Specifies if the request contained in the <paramref name="message"/> should be retried.
 16        /// </summary>
 17        public virtual bool IsRetriableResponse(HttpMessage message)
 18        {
 41419            switch (message.Response.Status)
 20            {
 21                case 408: // Request Timeout
 22                case 429: // Too Many Requests
 23                case 500: // Internal Server Error
 24                case 502: // Bad Gateway
 25                case 503: // Service Unavailable
 26                case 504: // Gateway Timeout
 41427                    return true;
 28                default:
 029                    return false;
 30            }
 31        }
 32
 33        /// <summary>
 34        /// Specifies if the operation that caused the exception should be retried.
 35        /// </summary>
 36        public virtual bool IsRetriableException(Exception exception)
 37        {
 6638            return (exception is IOException) ||
 6639                   (exception is RequestFailedException requestFailed && requestFailed.Status == 0);
 40        }
 41
 42        /// <summary>
 43        /// Specifies if the operation that caused the exception should be retried taking the <see cref="HttpMessage"/> 
 44        /// </summary>
 45        public virtual bool IsRetriable(HttpMessage message, Exception exception)
 46        {
 7447            return IsRetriableException(exception) ||
 7448                   // Retry non-user initiated cancellations
 7449                   (exception is OperationCanceledException && !message.CancellationToken.IsCancellationRequested);
 50        }
 51
 52        /// <summary>
 53        /// Specifies if the response contained in the <paramref name="message"/> is not successful.
 54        /// </summary>
 55        public virtual bool IsErrorResponse(HttpMessage message)
 56        {
 485657            var statusKind = message.Response.Status / 100;
 485658            return statusKind == 4 || statusKind == 5;
 59        }
 60    }
 61}