| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Globalization; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Reflection; |
| | 11 | | using System.Text; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | | using Azure.Core.Pipeline; |
| | 14 | |
|
| | 15 | | #nullable enable |
| | 16 | |
|
| | 17 | | namespace Azure.Core.Pipeline |
| | 18 | | { |
| | 19 | | internal sealed partial class ClientDiagnostics : DiagnosticScopeFactory |
| | 20 | | { |
| | 21 | | private const string DefaultMessage = "Service request failed."; |
| | 22 | |
|
| | 23 | | private readonly HttpMessageSanitizer _sanitizer; |
| 2 | 24 | | public ClientDiagnostics(ClientOptions options) : base( |
| 2 | 25 | | options.GetType().Namespace!, |
| 2 | 26 | | GetResourceProviderNamespace(options.GetType().Assembly), |
| 2 | 27 | | options.Diagnostics.IsDistributedTracingEnabled) |
| | 28 | | { |
| 2 | 29 | | _sanitizer = new HttpMessageSanitizer( |
| 2 | 30 | | options.Diagnostics.LoggedQueryParameters.ToArray(), |
| 2 | 31 | | options.Diagnostics.LoggedHeaderNames.ToArray()); |
| 2 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Partial method that can optionally be defined to extract the error |
| | 36 | | /// message, code, and details in a service specific manner. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="content">The error content.</param> |
| | 39 | | /// <param name="message">The error message.</param> |
| | 40 | | /// <param name="errorCode">The error code.</param> |
| | 41 | | /// <param name="additionalInfo">Additional error details.</param> |
| | 42 | | partial void ExtractFailureContent( |
| | 43 | | string? content, |
| | 44 | | ref string? message, |
| | 45 | | ref string? errorCode, |
| | 46 | | ref IDictionary<string, string>? additionalInfo); |
| | 47 | |
|
| | 48 | | public async ValueTask<RequestFailedException> CreateRequestFailedExceptionAsync(Response response, string? mess |
| | 49 | | { |
| 14 | 50 | | var content = await ReadContentAsync(response, true).ConfigureAwait(false); |
| | 51 | | ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo); |
| 14 | 52 | | return CreateRequestFailedExceptionWithContent(response, message, content, errorCode, additionalInfo, innerE |
| 14 | 53 | | } |
| | 54 | |
|
| | 55 | | public RequestFailedException CreateRequestFailedException(Response response, string? message = null, string? er |
| | 56 | | { |
| 0 | 57 | | string? content = ReadContentAsync(response, false).EnsureCompleted(); |
| | 58 | | ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo); |
| 0 | 59 | | return CreateRequestFailedExceptionWithContent(response, message, content, errorCode, additionalInfo, innerE |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public RequestFailedException CreateRequestFailedExceptionWithContent( |
| | 63 | | Response response, |
| | 64 | | string? message = null, |
| | 65 | | string? content = null, |
| | 66 | | string? errorCode = null, |
| | 67 | | IDictionary<string, string>? additionalInfo = null, |
| | 68 | | Exception? innerException = null) |
| | 69 | | { |
| 14 | 70 | | var formatMessage = CreateRequestFailedMessageWithContent(response, message, content, errorCode, additionalI |
| 14 | 71 | | var exception = new RequestFailedException(response.Status, formatMessage, errorCode, innerException); |
| | 72 | |
|
| 14 | 73 | | if (additionalInfo != null) |
| | 74 | | { |
| 12 | 75 | | foreach (KeyValuePair<string, string> keyValuePair in additionalInfo) |
| | 76 | | { |
| 4 | 77 | | exception.Data.Add(keyValuePair.Key, keyValuePair.Value); |
| | 78 | | } |
| | 79 | | } |
| | 80 | |
|
| 14 | 81 | | return exception; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public ValueTask<string> CreateRequestFailedMessageAsync(Response response, string? message = null, string? erro |
| | 85 | | { |
| 0 | 86 | | return CreateRequestFailedMessageAsync(response, message, errorCode, additionalInfo, true); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public string CreateRequestFailedMessage(Response response, string? message = null, string? errorCode = null, ID |
| | 90 | | { |
| 0 | 91 | | return CreateRequestFailedMessageAsync(response, message, errorCode, additionalInfo, false).EnsureCompleted( |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private async ValueTask<string> CreateRequestFailedMessageAsync(Response response, string? message, string? erro |
| | 95 | | { |
| 0 | 96 | | var content = await ReadContentAsync(response, async).ConfigureAwait(false); |
| | 97 | |
|
| 0 | 98 | | return CreateRequestFailedMessageWithContent(response, message, content, errorCode, additionalInfo); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public string CreateRequestFailedMessageWithContent(Response response, string? message, string? content, string? |
| | 102 | | { |
| 14 | 103 | | StringBuilder messageBuilder = new StringBuilder() |
| 14 | 104 | | .AppendLine(message ?? DefaultMessage) |
| 14 | 105 | | .Append("Status: ") |
| 14 | 106 | | .Append(response.Status.ToString(CultureInfo.InvariantCulture)); |
| | 107 | |
|
| 14 | 108 | | if (!string.IsNullOrEmpty(response.ReasonPhrase)) |
| | 109 | | { |
| 14 | 110 | | messageBuilder.Append(" (") |
| 14 | 111 | | .Append(response.ReasonPhrase) |
| 14 | 112 | | .AppendLine(")"); |
| | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 0 | 116 | | messageBuilder.AppendLine(); |
| | 117 | | } |
| | 118 | |
|
| 14 | 119 | | if (!string.IsNullOrWhiteSpace(errorCode)) |
| | 120 | | { |
| 2 | 121 | | messageBuilder.Append("ErrorCode: ") |
| 2 | 122 | | .Append(errorCode) |
| 2 | 123 | | .AppendLine(); |
| | 124 | | } |
| | 125 | |
|
| 14 | 126 | | if (additionalInfo != null && additionalInfo.Count > 0) |
| | 127 | | { |
| 2 | 128 | | messageBuilder |
| 2 | 129 | | .AppendLine() |
| 2 | 130 | | .AppendLine("Additional Information:"); |
| 12 | 131 | | foreach (KeyValuePair<string, string> info in additionalInfo) |
| | 132 | | { |
| 4 | 133 | | messageBuilder |
| 4 | 134 | | .Append(info.Key) |
| 4 | 135 | | .Append(": ") |
| 4 | 136 | | .AppendLine(info.Value); |
| | 137 | | } |
| | 138 | | } |
| | 139 | |
|
| 14 | 140 | | if (content != null) |
| | 141 | | { |
| 2 | 142 | | messageBuilder |
| 2 | 143 | | .AppendLine() |
| 2 | 144 | | .AppendLine("Content:") |
| 2 | 145 | | .AppendLine(content); |
| | 146 | | } |
| | 147 | |
|
| 14 | 148 | | messageBuilder |
| 14 | 149 | | .AppendLine() |
| 14 | 150 | | .AppendLine("Headers:"); |
| | 151 | |
|
| 80 | 152 | | foreach (HttpHeader responseHeader in response.Headers) |
| | 153 | | { |
| 26 | 154 | | string headerValue = _sanitizer.SanitizeHeader(responseHeader.Name, responseHeader.Value); |
| 26 | 155 | | messageBuilder.AppendLine($"{responseHeader.Name}: {headerValue}"); |
| | 156 | | } |
| | 157 | |
|
| 14 | 158 | | return messageBuilder.ToString(); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | private static async ValueTask<string?> ReadContentAsync(Response response, bool async) |
| | 162 | | { |
| 14 | 163 | | string? content = null; |
| | 164 | |
|
| 14 | 165 | | if (response.ContentStream != null && |
| 14 | 166 | | ContentTypeUtilities.TryGetTextEncoding(response.Headers.ContentType, out var encoding)) |
| | 167 | | { |
| 2 | 168 | | using (var streamReader = new StreamReader(response.ContentStream, encoding)) |
| | 169 | | { |
| 2 | 170 | | content = async ? await streamReader.ReadToEndAsync().ConfigureAwait(false) : streamReader.ReadToEnd |
| 2 | 171 | | } |
| | 172 | | } |
| | 173 | |
|
| 14 | 174 | | return content; |
| 14 | 175 | | } |
| | 176 | |
|
| | 177 | | internal static string? GetResourceProviderNamespace(Assembly assembly) |
| | 178 | | { |
| 714 | 179 | | foreach (var customAttribute in assembly.GetCustomAttributes(true)) |
| | 180 | | { |
| | 181 | | // Weak bind internal shared type |
| 326 | 182 | | var attributeType = customAttribute.GetType(); |
| 326 | 183 | | if (attributeType.Name == "AzureResourceProviderNamespaceAttribute") |
| | 184 | | { |
| 58 | 185 | | return attributeType.GetProperty("ResourceProviderNamespace")?.GetValue(customAttribute) as string; |
| | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| 2 | 189 | | return null; |
| | 190 | | } |
| | 191 | | } |
| | 192 | | } |