< Summary

Class:Azure.Core.Pipeline.ClientDiagnostics
Assembly:Azure.AI.TextAnalytics
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\ClientDiagnostics.cs
C:\Git\azure-sdk-for-net\sdk\textanalytics\Azure.AI.TextAnalytics\src\ClientDiagnostics.cs
Covered lines:74
Uncovered lines:20
Coverable lines:94
Total lines:248
Line coverage:78.7% (74 of 94)
Covered branches:26
Total branches:36
Branch coverage:72.2% (26 of 36)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
CreateRequestFailedExceptionAsync()-100%100%
CreateRequestFailedException(...)-100%100%
CreateRequestFailedExceptionWithContent(...)-66.67%25%
CreateRequestFailedMessageAsync(...)-0%100%
CreateRequestFailedMessage(...)-0%100%
CreateRequestFailedMessageAsync()-0%100%
CreateRequestFailedMessageWithContent(...)-67.65%68.75%
ReadContentAsync()-100%100%
GetResourceProviderNamespace(...)-80%66.67%
CreateRequestFailedException(...)-100%100%
CreateRequestFailedExceptionWithContent(...)-100%100%
CreateRequestFailedMessageAsync(...)-100%100%
CreateRequestFailedMessage(...)-100%100%
CreateRequestFailedMessageWithContent(...)-100%100%
GetResourceProviderNamespace(...)-100%100%
CreateRequestFailedMessageAsync()-100%100%
ReadContentAsync()-100%100%
.ctor(...)-100%100%
ExtractFailureContent(...)-92.31%100%
CreateRequestFailedExceptionAsync()-100%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Globalization;
 8using System.IO;
 9using System.Linq;
 10using System.Reflection;
 11using System.Text;
 12using System.Threading.Tasks;
 13using Azure.Core.Pipeline;
 14
 15#nullable enable
 16
 17namespace 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;
 9224        public ClientDiagnostics(ClientOptions options) : base(
 9225            options.GetType().Namespace!,
 9226            GetResourceProviderNamespace(options.GetType().Assembly),
 9227            options.Diagnostics.IsDistributedTracingEnabled)
 28        {
 9229            _sanitizer = new HttpMessageSanitizer(
 9230                options.Diagnostics.LoggedQueryParameters.ToArray(),
 9231                options.Diagnostics.LoggedHeaderNames.ToArray());
 9232        }
 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        {
 350            var content = await ReadContentAsync(response, true).ConfigureAwait(false);
 351            ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo);
 352            return CreateRequestFailedExceptionWithContent(response, message, content, errorCode, additionalInfo, innerE
 353        }
 54
 55        public RequestFailedException CreateRequestFailedException(Response response, string? message = null, string? er
 56        {
 557            string? content = ReadContentAsync(response, false).EnsureCompleted();
 558            ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo);
 559            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        {
 870            var formatMessage = CreateRequestFailedMessageWithContent(response, message, content, errorCode, additionalI
 871            var exception = new RequestFailedException(response.Status, formatMessage, errorCode, innerException);
 72
 873            if (additionalInfo != null)
 74            {
 075                foreach (KeyValuePair<string, string> keyValuePair in additionalInfo)
 76                {
 077                    exception.Data.Add(keyValuePair.Key, keyValuePair.Value);
 78                }
 79            }
 80
 881            return exception;
 82        }
 83
 84        public ValueTask<string> CreateRequestFailedMessageAsync(Response response, string? message = null, string? erro
 85        {
 086            return CreateRequestFailedMessageAsync(response, message, errorCode, additionalInfo, true);
 87        }
 88
 89        public string CreateRequestFailedMessage(Response response, string? message = null, string? errorCode = null, ID
 90        {
 091            return CreateRequestFailedMessageAsync(response, message, errorCode, additionalInfo, false).EnsureCompleted(
 92        }
 93
 94        private async ValueTask<string> CreateRequestFailedMessageAsync(Response response, string? message, string? erro
 95        {
 096            var content = await ReadContentAsync(response, async).ConfigureAwait(false);
 97
 098            return CreateRequestFailedMessageWithContent(response, message, content, errorCode, additionalInfo);
 099        }
 100
 101        public string CreateRequestFailedMessageWithContent(Response response, string? message, string? content, string?
 102        {
 8103            StringBuilder messageBuilder = new StringBuilder()
 8104                .AppendLine(message ?? DefaultMessage)
 8105                .Append("Status: ")
 8106                .Append(response.Status.ToString(CultureInfo.InvariantCulture));
 107
 8108            if (!string.IsNullOrEmpty(response.ReasonPhrase))
 109            {
 0110                messageBuilder.Append(" (")
 0111                    .Append(response.ReasonPhrase)
 0112                    .AppendLine(")");
 113            }
 114            else
 115            {
 8116                messageBuilder.AppendLine();
 117            }
 118
 8119            if (!string.IsNullOrWhiteSpace(errorCode))
 120            {
 6121                messageBuilder.Append("ErrorCode: ")
 6122                    .Append(errorCode)
 6123                    .AppendLine();
 124            }
 125
 8126            if (additionalInfo != null && additionalInfo.Count > 0)
 127            {
 0128                messageBuilder
 0129                    .AppendLine()
 0130                    .AppendLine("Additional Information:");
 0131                foreach (KeyValuePair<string, string> info in additionalInfo)
 132                {
 0133                    messageBuilder
 0134                        .Append(info.Key)
 0135                        .Append(": ")
 0136                        .AppendLine(info.Value);
 137                }
 138            }
 139
 8140            if (content != null)
 141            {
 6142                messageBuilder
 6143                    .AppendLine()
 6144                    .AppendLine("Content:")
 6145                    .AppendLine(content);
 146            }
 147
 8148            messageBuilder
 8149                .AppendLine()
 8150                .AppendLine("Headers:");
 151
 108152            foreach (HttpHeader responseHeader in response.Headers)
 153            {
 46154                string headerValue = _sanitizer.SanitizeHeader(responseHeader.Name, responseHeader.Value);
 46155                messageBuilder.AppendLine($"{responseHeader.Name}: {headerValue}");
 156            }
 157
 8158            return messageBuilder.ToString();
 159        }
 160
 161        private static async ValueTask<string?> ReadContentAsync(Response response, bool async)
 162        {
 8163            string? content = null;
 164
 8165            if (response.ContentStream != null &&
 8166                ContentTypeUtilities.TryGetTextEncoding(response.Headers.ContentType, out var encoding))
 167            {
 6168                using (var streamReader = new StreamReader(response.ContentStream, encoding))
 169                {
 6170                    content = async ? await streamReader.ReadToEndAsync().ConfigureAwait(false) : streamReader.ReadToEnd
 6171                }
 172            }
 173
 8174            return content;
 8175        }
 176
 177        internal static string? GetResourceProviderNamespace(Assembly assembly)
 178        {
 1380179            foreach (var customAttribute in assembly.GetCustomAttributes(true))
 180            {
 181                // Weak bind internal shared type
 644182                var attributeType = customAttribute.GetType();
 644183                if (attributeType.Name == "AzureResourceProviderNamespaceAttribute")
 184                {
 92185                    return attributeType.GetProperty("ResourceProviderNamespace")?.GetValue(customAttribute) as string;
 186                }
 187            }
 188
 0189            return null;
 190        }
 191    }
 192}

C:\Git\azure-sdk-for-net\sdk\textanalytics\Azure.AI.TextAnalytics\src\ClientDiagnostics.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Collections.Generic;
 5using System.Text.Json;
 6using Azure.AI.TextAnalytics;
 7
 8#nullable enable
 9
 10namespace Azure.Core.Pipeline
 11{
 12    /// <summary>
 13    /// Extend ClientDiagnostics to customize the exceptions thrown by the
 14    /// generated code.
 15    /// </summary>
 16    internal sealed partial class ClientDiagnostics
 17    {
 18        /// <summary>
 19        /// Customize the exception messages we throw from the protocol layer by
 20        /// attempting to parse them as <see cref="TextAnalyticsError"/>s.
 21        /// </summary>
 22        /// <param name="content">The error content.</param>
 23        /// <param name="message">The error message.</param>
 9224        /// <param name="errorCode">The error code.</param>
 9225        /// <param name="additionalInfo">Additional error details.</param>
 9226        partial void ExtractFailureContent(
 9227            string? content,
 28            ref string? message,
 9229            ref string? errorCode,
 9230#pragma warning disable CA1801 // Remove unused parameter
 9231            ref IDictionary<string, string>? additionalInfo)
 9232#pragma warning restore CA1801 // Remove unused parameter
 33        {
 1634            if (!string.IsNullOrEmpty(content))
 35            {
 36                try
 37                {
 38                    // Try to parse the failure content and use that as the
 39                    // default value for the message, error code, etc.
 840                    using JsonDocument doc = JsonDocument.Parse(content);
 841                    if (doc.RootElement.TryGetProperty("error", out JsonElement errorElement))
 42                    {
 843                        TextAnalyticsError error = TextAnalyticsServiceSerializer.ReadTextAnalyticsError(errorElement);
 844                        message = error.Message;
 845                        errorCode = error.ErrorCode.ToString();
 46                    }
 847                }
 048                catch (JsonException)
 49                {
 350                    // Ignore any failures - unexpected content will be
 351                    // included verbatim in the detailed error message
 352                }
 353            }
 1654        }
 55    }
 56}