< Summary

Class:Azure.Core.Pipeline.ClientDiagnostics
Assembly:Azure.Messaging.ServiceBus
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\ClientDiagnostics.cs
C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\ClientDiagnostics.cs
Covered lines:71
Uncovered lines:22
Coverable lines:93
Total lines:240
Line coverage:76.3% (71 of 93)
Covered branches:29
Total branches:42
Branch coverage:69% (29 of 42)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
CreateRequestFailedExceptionAsync()-100%100%
CreateRequestFailedException(...)-0%100%
CreateRequestFailedExceptionWithContent(...)-66.67%25%
CreateRequestFailedMessageAsync(...)-0%100%
CreateRequestFailedMessage(...)-0%100%
CreateRequestFailedMessageAsync()-0%100%
CreateRequestFailedMessageWithContent(...)-76.47%75%
ReadContentAsync()-100%83.33%
GetResourceProviderNamespace(...)-80%66.67%
CreateRequestFailedException(...)-100%100%
CreateRequestFailedExceptionWithContent(...)-100%100%
CreateRequestFailedMessageAsync(...)-100%100%
CreateRequestFailedMessage(...)-100%100%
CreateRequestFailedMessageWithContent(...)-100%100%
GetResourceProviderNamespace(...)-100%100%
CreateRequestFailedExceptionAsync()-100%100%
CreateRequestFailedMessageAsync()-100%100%
ReadContentAsync()-100%100%
ExtractFailureContent(...)-85%70%
.ctor(...)-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;
 4324        public ClientDiagnostics(ClientOptions options) : base(
 4325            options.GetType().Namespace!,
 4326            GetResourceProviderNamespace(options.GetType().Assembly),
 4327            options.Diagnostics.IsDistributedTracingEnabled)
 28        {
 4329            _sanitizer = new HttpMessageSanitizer(
 4330                options.Diagnostics.LoggedQueryParameters.ToArray(),
 4331                options.Diagnostics.LoggedHeaderNames.ToArray());
 4332        }
 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        {
 3450            var content = await ReadContentAsync(response, true).ConfigureAwait(false);
 3451            ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo);
 3452            return CreateRequestFailedExceptionWithContent(response, message, content, errorCode, additionalInfo, innerE
 3453        }
 54
 55        public RequestFailedException CreateRequestFailedException(Response response, string? message = null, string? er
 56        {
 057            string? content = ReadContentAsync(response, false).EnsureCompleted();
 058            ExtractFailureContent(content, ref message, ref errorCode, ref additionalInfo);
 059            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        {
 3470            var formatMessage = CreateRequestFailedMessageWithContent(response, message, content, errorCode, additionalI
 3471            var exception = new RequestFailedException(response.Status, formatMessage, errorCode, innerException);
 72
 3473            if (additionalInfo != null)
 74            {
 075                foreach (KeyValuePair<string, string> keyValuePair in additionalInfo)
 76                {
 077                    exception.Data.Add(keyValuePair.Key, keyValuePair.Value);
 78                }
 79            }
 80
 3481            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        {
 34103            StringBuilder messageBuilder = new StringBuilder()
 34104                .AppendLine(message ?? DefaultMessage)
 34105                .Append("Status: ")
 34106                .Append(response.Status.ToString(CultureInfo.InvariantCulture));
 107
 34108            if (!string.IsNullOrEmpty(response.ReasonPhrase))
 109            {
 1110                messageBuilder.Append(" (")
 1111                    .Append(response.ReasonPhrase)
 1112                    .AppendLine(")");
 113            }
 114            else
 115            {
 33116                messageBuilder.AppendLine();
 117            }
 118
 34119            if (!string.IsNullOrWhiteSpace(errorCode))
 120            {
 8121                messageBuilder.Append("ErrorCode: ")
 8122                    .Append(errorCode)
 8123                    .AppendLine();
 124            }
 125
 34126            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
 34140            if (content != null)
 141            {
 24142                messageBuilder
 24143                    .AppendLine()
 24144                    .AppendLine("Content:")
 24145                    .AppendLine(content);
 146            }
 147
 34148            messageBuilder
 34149                .AppendLine()
 34150                .AppendLine("Headers:");
 151
 332152            foreach (HttpHeader responseHeader in response.Headers)
 153            {
 132154                string headerValue = _sanitizer.SanitizeHeader(responseHeader.Name, responseHeader.Value);
 132155                messageBuilder.AppendLine($"{responseHeader.Name}: {headerValue}");
 156            }
 157
 34158            return messageBuilder.ToString();
 159        }
 160
 161        private static async ValueTask<string?> ReadContentAsync(Response response, bool async)
 162        {
 34163            string? content = null;
 164
 34165            if (response.ContentStream != null &&
 34166                ContentTypeUtilities.TryGetTextEncoding(response.Headers.ContentType, out var encoding))
 167            {
 24168                using (var streamReader = new StreamReader(response.ContentStream, encoding))
 169                {
 24170                    content = async ? await streamReader.ReadToEndAsync().ConfigureAwait(false) : streamReader.ReadToEnd
 24171                }
 172            }
 173
 34174            return content;
 34175        }
 176
 177        internal static string? GetResourceProviderNamespace(Assembly assembly)
 178        {
 645179            foreach (var customAttribute in assembly.GetCustomAttributes(true))
 180            {
 181                // Weak bind internal shared type
 301182                var attributeType = customAttribute.GetType();
 301183                if (attributeType.Name == "AzureResourceProviderNamespaceAttribute")
 184                {
 43185                    return attributeType.GetProperty("ResourceProviderNamespace")?.GetValue(customAttribute) as string;
 186                }
 187            }
 188
 0189            return null;
 190        }
 191    }
 192}

C:\Git\azure-sdk-for-net\sdk\servicebus\Azure.Messaging.ServiceBus\src\Management\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.Text.RegularExpressions;
 7using System.Xml.Linq;
 8
 9#nullable enable
 10
 11namespace Azure.Core.Pipeline
 12{
 13    internal sealed partial class ClientDiagnostics
 14    {
 15        partial void ExtractFailureContent(
 16            string? content,
 17            ref string? message,
 18            ref string? errorCode,
 19#pragma warning disable CA1801 // Remove unused parameter
 20            ref IDictionary<string, string>? additionalInfo)
 21#pragma warning restore CA1801 // Remove unused parameter
 22        {
 6823            if (string.IsNullOrWhiteSpace(content))
 4324            {
 6325                return;
 4326            }
 4327
 28            try
 4329            {
 9130                var errorContentXml = XElement.Parse(content);
 9131                XElement detail = errorContentXml.Element("Detail");
 4332
 4833                message = detail?.Value ?? content;
 4834                Match? match = Regex.Match(
 4835                    detail?.Value,
 4836                    "SubCode=(\\d+)\\.");
 4837                if (match.Success)
 38                {
 1639                    errorCode = match.Groups[1].Value;
 40                }
 4841            }
 042            catch (Exception)
 43            {
 044                message = content;
 045            }
 4846        }
 47    }
 48}