| | | 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.Threading.Tasks; |
| | | 7 | | using Azure.AI.FormRecognizer.Models; |
| | | 8 | | using Azure.Core; |
| | | 9 | | using Azure.Core.Pipeline; |
| | | 10 | | |
| | | 11 | | namespace 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 | | { |
| | 268 | 29 | | guid = new Guid(modelId); |
| | 248 | 30 | | } |
| | 20 | 31 | | catch (Exception ex) when (ex is FormatException || ex is OverflowException) |
| | | 32 | | { |
| | 20 | 33 | | throw new ArgumentException($"The {paramName} must be a valid GUID.", paramName, ex); |
| | | 34 | | } |
| | | 35 | | |
| | 248 | 36 | | return guid; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public static string GetResponseHeader(ResponseHeaders responseHeaders, string headerName) |
| | | 40 | | { |
| | 220 | 41 | | if (responseHeaders.TryGetValue(headerName, out var headerValue)) |
| | | 42 | | { |
| | 220 | 43 | | return headerValue; |
| | | 44 | | } |
| | | 45 | | else |
| | | 46 | | { |
| | 0 | 47 | | 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 | | { |
| | 24 | 53 | | string errorCode = default; |
| | | 54 | | |
| | 24 | 55 | | if (errors.Count > 0) |
| | | 56 | | { |
| | 24 | 57 | | errorCode = errors[0].ErrorCode; |
| | 24 | 58 | | errorMessage ??= errors[0].Message; |
| | | 59 | | } |
| | | 60 | | |
| | 24 | 61 | | var errorInfo = new Dictionary<string, string>(); |
| | 24 | 62 | | int index = 0; |
| | 96 | 63 | | foreach (var error in errors) |
| | | 64 | | { |
| | 24 | 65 | | errorInfo.Add($"error-{index}", $"{error.ErrorCode}: {error.Message}"); |
| | 24 | 66 | | index++; |
| | | 67 | | } |
| | | 68 | | |
| | 24 | 69 | | return async |
| | 24 | 70 | | ? await diagnostics.CreateRequestFailedExceptionAsync(response, errorMessage, errorCode, errorInfo).Conf |
| | 24 | 71 | | : diagnostics.CreateRequestFailedException(response, errorMessage, errorCode, errorInfo); |
| | 24 | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |