| | 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 | |
|
| | 4 | | namespace 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> |
| 31 | 21 | | 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) |
| 7 | 30 | | : base(message, inner) |
| | 31 | | { |
| 7 | 32 | | this.RequestInformation = requestInformation; |
| 7 | 33 | | } |
| | 34 | |
|
| | 35 | | private static string TryGetSingleHeaderOrDefault(string headerName, IDictionary<string, IEnumerable<string>> he |
| | 36 | | { |
| 9 | 37 | | string result = null; |
| | 38 | |
|
| 9 | 39 | | if (headers.ContainsKey(headerName)) |
| | 40 | | { |
| 2 | 41 | | if (headers[headerName] != null) |
| | 42 | | { |
| 2 | 43 | | result = headers[headerName].FirstOrDefault(); |
| | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| 9 | 47 | | 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> |
| 3 | 54 | | internal BatchException(BatchErrorException batchErrorException) : base(batchErrorException.Message, batchErrorE |
| | 55 | | { |
| | 56 | | //Process client request id header |
| 3 | 57 | | string clientRequestIdString = TryGetSingleHeaderOrDefault(InternalConstants.ClientRequestIdHeader, batchErr |
| | 58 | |
|
| | 59 | | //Process request id header |
| 3 | 60 | | string requestIdString = TryGetSingleHeaderOrDefault(InternalConstants.RequestIdHeader, batchErrorException. |
| | 61 | |
|
| | 62 | | //Process retry-after header |
| 3 | 63 | | string retryAfterString = TryGetSingleHeaderOrDefault(InternalConstants.RetryAfterHeader, batchErrorExceptio |
| 3 | 64 | | var retryAfter = ExtractRetryAfterHeader(retryAfterString); |
| | 65 | |
|
| 3 | 66 | | this.RequestInformation = new RequestInformation() |
| 3 | 67 | | { |
| 3 | 68 | | BatchError = batchErrorException.Body == null ? null : new BatchError(batchErrorException.Body), |
| 3 | 69 | | ClientRequestId = string.IsNullOrEmpty(clientRequestIdString) ? default(Guid?) : new Guid(clientRequestI |
| 3 | 70 | | HttpStatusCode = batchErrorException.Response.StatusCode, |
| 3 | 71 | | HttpStatusMessage = batchErrorException.Response.ReasonPhrase, //TODO: Is this right? |
| 3 | 72 | | ServiceRequestId = requestIdString, |
| 3 | 73 | | RetryAfter = retryAfter |
| 3 | 74 | | }; |
| 3 | 75 | | } |
| | 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 | | { |
| 1 | 83 | | StringBuilder sb = new StringBuilder(); |
| 1 | 84 | | sb.AppendLine(base.ToString()); |
| | 85 | |
|
| 1 | 86 | | if (this.RequestInformation != null) |
| | 87 | | { |
| 1 | 88 | | sb.AppendLine("Request Information"); |
| 1 | 89 | | sb.AppendLine("ClientRequestId:" + this.RequestInformation.ClientRequestId); |
| | 90 | | //if (this.RequestInformation.ServiceUri != null) |
| | 91 | | //{ |
| | 92 | | // sb.AppendLine("ServiceUrl:" + this.RequestInformation.ServiceUri.ToString()); |
| | 93 | | //} |
| 1 | 94 | | sb.AppendLine("RequestId:" + this.RequestInformation.ServiceRequestId); |
| | 95 | | //sb.AppendLine("RequestDate:" + this.RequestInformation.RequestDate); //TODO: How to get this field? |
| 1 | 96 | | sb.AppendLine("HttpStatusCode:" + this.RequestInformation.HttpStatusCode); |
| 1 | 97 | | sb.AppendLine("StatusMessage:" + this.RequestInformation.HttpStatusMessage); |
| | 98 | |
|
| 1 | 99 | | if (this.RequestInformation.BatchError != null) |
| | 100 | | { |
| 1 | 101 | | sb.Append($"\n\nError Code = {this.RequestInformation.BatchError.Code},"); |
| 1 | 102 | | if (this.RequestInformation.BatchError.Message != null) |
| | 103 | | { |
| 0 | 104 | | sb.Append($" Lang={this.RequestInformation.BatchError.Message.Language}, Message = {this.Request |
| | 105 | | } |
| | 106 | |
|
| 1 | 107 | | if (this.RequestInformation.BatchError.Values != null) |
| | 108 | | { |
| 0 | 109 | | foreach (var item in this.RequestInformation.BatchError.Values) |
| | 110 | | { |
| 0 | 111 | | sb.Append($"Error Details key={item.Key} value={item.Value}\n"); |
| | 112 | | } |
| | 113 | | } |
| | 114 | | } |
| | 115 | | } |
| | 116 | |
|
| 1 | 117 | | return sb.ToString(); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private static TimeSpan? ExtractRetryAfterHeader(string retryAfterHeader) |
| | 121 | | { |
| 3 | 122 | | TimeSpan? retryAfter = null; |
| 3 | 123 | | if (!string.IsNullOrEmpty(retryAfterHeader)) |
| | 124 | | { |
| | 125 | | int retryAfterSeconds; |
| 2 | 126 | | bool parsed = int.TryParse(retryAfterHeader, out retryAfterSeconds); |
| 2 | 127 | | if (parsed) |
| | 128 | | { |
| 1 | 129 | | retryAfter = TimeSpan.FromSeconds(retryAfterSeconds); |
| | 130 | | } |
| | 131 | | else |
| | 132 | | { |
| | 133 | | // Try RFC1123 format |
| | 134 | | DateTime retryAfterDate; |
| 1 | 135 | | parsed = DateTime.TryParse(retryAfterHeader, out retryAfterDate); |
| 1 | 136 | | if (parsed) |
| | 137 | | { |
| 1 | 138 | | DateTime now = DateTime.UtcNow; |
| 1 | 139 | | retryAfterDate = retryAfterDate.ToUniversalTime(); |
| 1 | 140 | | retryAfter = now >= retryAfterDate ? TimeSpan.Zero : retryAfterDate.Subtract(DateTime.UtcNow); |
| | 141 | | } |
| | 142 | | } |
| | 143 | | } |
| | 144 | |
|
| 3 | 145 | | return retryAfter; |
| | 146 | | } |
| | 147 | | } |
| | 148 | | } |