< Summary

Class:Azure.AI.FormRecognizer.ClientCommon
Assembly:Azure.AI.FormRecognizer
File(s):C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\ClientCommon.cs
Covered lines:20
Uncovered lines:1
Coverable lines:21
Total lines:74
Line coverage:95.2% (20 of 21)
Covered branches:8
Total branches:10
Branch coverage:80% (8 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ValidateModelId(...)-100%100%
GetResponseHeader(...)-66.67%50%
CreateExceptionForFailedOperationAsync()-100%87.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\formrecognizer\Azure.AI.FormRecognizer\src\ClientCommon.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.Threading.Tasks;
 7using Azure.AI.FormRecognizer.Models;
 8using Azure.Core;
 9using Azure.Core.Pipeline;
 10
 11namespace Azure.AI.FormRecognizer
 12{
 13    internal static class ClientCommon
 14    {
 15        /// <summary>
 16        /// Used as part of argument validation. Attempts to create a <see cref="Guid"/> from a <c>string</c> and
 17        /// throws an <see cref="ArgumentException"/> in case of failure.
 18        /// </summary>
 19        /// <param name="modelId">The model identifier to be parsed into a <see cref="Guid"/>.</param>
 20        /// <param name="paramName">The original parameter name of the <paramref name="modelId"/>. Used to create except
 21        /// <returns>The <see cref="Guid"/> instance created from the <paramref name="modelId"/>.</returns>
 22        /// <exception cref="ArgumentException">Thrown when parsing fails.</exception>
 23        public static Guid ValidateModelId(string modelId, string paramName)
 24        {
 25            Guid guid;
 26
 27            try
 28            {
 26829                guid = new Guid(modelId);
 24830            }
 2031            catch (Exception ex) when (ex is FormatException || ex is OverflowException)
 32            {
 2033                throw new ArgumentException($"The {paramName} must be a valid GUID.", paramName, ex);
 34            }
 35
 24836            return guid;
 37        }
 38
 39        public static string GetResponseHeader(ResponseHeaders responseHeaders, string headerName)
 40        {
 22041            if (responseHeaders.TryGetValue(headerName, out var headerValue))
 42            {
 22043                return headerValue;
 44            }
 45            else
 46            {
 047                throw new KeyNotFoundException($"Header '{headerName}' was not present in the response sent by the serve
 48            }
 49        }
 50
 51        public static async ValueTask<RequestFailedException> CreateExceptionForFailedOperationAsync(bool async, ClientD
 52        {
 2453            string errorCode = default;
 54
 2455            if (errors.Count > 0)
 56            {
 2457                errorCode = errors[0].ErrorCode;
 2458                errorMessage ??= errors[0].Message;
 59            }
 60
 2461            var errorInfo = new Dictionary<string, string>();
 2462            int index = 0;
 9663            foreach (var error in errors)
 64            {
 2465                errorInfo.Add($"error-{index}", $"{error.ErrorCode}: {error.Message}");
 2466                index++;
 67            }
 68
 2469            return async
 2470                ? await diagnostics.CreateRequestFailedExceptionAsync(response, errorMessage, errorCode, errorInfo).Conf
 2471                : diagnostics.CreateRequestFailedException(response, errorMessage, errorCode, errorInfo);
 2472        }
 73    }
 74}