< Summary

Class:Microsoft.Azure.Batch.Common.BatchException
Assembly:Microsoft.Azure.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\Common\BatchException.cs
Covered lines:48
Uncovered lines:3
Coverable lines:51
Total lines:148
Line coverage:94.1% (48 of 51)
Covered branches:19
Total branches:26
Branch coverage:73% (19 of 26)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
get_RequestInformation()-100%100%
.ctor(...)-100%100%
TryGetSingleHeaderOrDefault(...)-100%100%
.ctor(...)-100%50%
ToString()-81.25%60%
ExtractRetryAfterHeader(...)-100%87.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\batch\Microsoft.Azure.Batch\src\Common\BatchException.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for license information.
 3
 4namespace Microsoft.Azure.Batch.Common
 5{
 6    using System;
 7    using System.Collections.Generic;
 8    using System.Linq;
 9    using System.Text;
 10    using Protocol.Models;
 11    using BatchError = Batch.BatchError;
 12
 13    /// <summary>
 14    /// Represents an exception for the Windows Azure Batch service.
 15    /// </summary>
 16    public class BatchException : Exception
 17    {
 18        /// <summary>
 19        /// Gets information about the request which failed.
 20        /// </summary>
 3121        public RequestInformation RequestInformation { get; private set; }
 22
 23        /// <summary>
 24        /// Initializes a new instance of the <see cref="BatchException"/> class by using the specified parameters.
 25        /// </summary>
 26        /// <param name="requestInformation">A <see cref="RequestInformation"/> object containing details about the requ
 27        /// <param name="message">The exception message.</param>
 28        /// <param name="inner">The inner exception.</param>
 29        public BatchException(RequestInformation requestInformation, string message, Exception inner)
 730            : base(message, inner)
 31        {
 732            this.RequestInformation = requestInformation;
 733        }
 34
 35        private static string TryGetSingleHeaderOrDefault(string headerName, IDictionary<string, IEnumerable<string>> he
 36        {
 937            string result = null;
 38
 939            if (headers.ContainsKey(headerName))
 40            {
 241                if (headers[headerName] != null)
 42                {
 243                    result = headers[headerName].FirstOrDefault();
 44                }
 45            }
 46
 947            return result;
 48        }
 49
 50        /// <summary>
 51        /// Constructs a <see cref="BatchException"/> as a wrapper for a <see cref="BatchErrorException"/>.
 52        /// </summary>
 53        /// <param name="batchErrorException">The exception to wrap.</param>
 354        internal BatchException(BatchErrorException batchErrorException) : base(batchErrorException.Message, batchErrorE
 55        {
 56            //Process client request id header
 357            string clientRequestIdString = TryGetSingleHeaderOrDefault(InternalConstants.ClientRequestIdHeader, batchErr
 58
 59            //Process request id header
 360            string requestIdString = TryGetSingleHeaderOrDefault(InternalConstants.RequestIdHeader, batchErrorException.
 61
 62            //Process retry-after header
 363            string retryAfterString = TryGetSingleHeaderOrDefault(InternalConstants.RetryAfterHeader, batchErrorExceptio
 364            var retryAfter = ExtractRetryAfterHeader(retryAfterString);
 65
 366            this.RequestInformation = new RequestInformation()
 367            {
 368                BatchError = batchErrorException.Body == null ? null : new BatchError(batchErrorException.Body),
 369                ClientRequestId = string.IsNullOrEmpty(clientRequestIdString) ? default(Guid?) : new Guid(clientRequestI
 370                HttpStatusCode = batchErrorException.Response.StatusCode,
 371                HttpStatusMessage = batchErrorException.Response.ReasonPhrase, //TODO: Is this right?
 372                ServiceRequestId = requestIdString,
 373                RetryAfter = retryAfter
 374            };
 375        }
 76
 77        /// <summary>
 78        /// Generates a string which describes the exception.
 79        /// </summary>
 80        /// <returns>A string that represents the exception.</returns>
 81        public override string ToString()
 82        {
 183            StringBuilder sb = new StringBuilder();
 184            sb.AppendLine(base.ToString());
 85
 186            if (this.RequestInformation != null)
 87            {
 188                sb.AppendLine("Request Information");
 189                sb.AppendLine("ClientRequestId:" + this.RequestInformation.ClientRequestId);
 90                //if (this.RequestInformation.ServiceUri != null)
 91                //{
 92                //    sb.AppendLine("ServiceUrl:" + this.RequestInformation.ServiceUri.ToString());
 93                //}
 194                sb.AppendLine("RequestId:" +  this.RequestInformation.ServiceRequestId);
 95                //sb.AppendLine("RequestDate:" + this.RequestInformation.RequestDate); //TODO: How to get this field?
 196                sb.AppendLine("HttpStatusCode:" + this.RequestInformation.HttpStatusCode);
 197                sb.AppendLine("StatusMessage:" + this.RequestInformation.HttpStatusMessage);
 98
 199                if (this.RequestInformation.BatchError != null)
 100                {
 1101                    sb.Append($"\n\nError Code = {this.RequestInformation.BatchError.Code},");
 1102                    if (this.RequestInformation.BatchError.Message != null)
 103                    {
 0104                        sb.Append($" Lang={this.RequestInformation.BatchError.Message.Language}, Message = {this.Request
 105                    }
 106
 1107                    if (this.RequestInformation.BatchError.Values != null)
 108                    {
 0109                        foreach (var item in this.RequestInformation.BatchError.Values)
 110                        {
 0111                            sb.Append($"Error Details key={item.Key} value={item.Value}\n");
 112                        }
 113                    }
 114                }
 115            }
 116
 1117            return sb.ToString();
 118        }
 119
 120        private static TimeSpan? ExtractRetryAfterHeader(string retryAfterHeader)
 121        {
 3122            TimeSpan? retryAfter = null;
 3123            if (!string.IsNullOrEmpty(retryAfterHeader))
 124            {
 125                int retryAfterSeconds;
 2126                bool parsed = int.TryParse(retryAfterHeader, out retryAfterSeconds);
 2127                if (parsed)
 128                {
 1129                    retryAfter = TimeSpan.FromSeconds(retryAfterSeconds);
 130                }
 131                else
 132                {
 133                    // Try RFC1123 format
 134                    DateTime retryAfterDate;
 1135                    parsed = DateTime.TryParse(retryAfterHeader, out retryAfterDate);
 1136                    if (parsed)
 137                    {
 1138                        DateTime now = DateTime.UtcNow;
 1139                        retryAfterDate = retryAfterDate.ToUniversalTime();
 1140                        retryAfter = now >= retryAfterDate ? TimeSpan.Zero : retryAfterDate.Subtract(DateTime.UtcNow);
 141                    }
 142                }
 143            }
 144
 3145            return retryAfter;
 146        }
 147    }
 148}