| | 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.Globalization; |
| | 7 | | using System.Text; |
| | 8 | | using System.Text.Json; |
| | 9 | | using System.Xml.Linq; |
| | 10 | | using Azure.Core.Pipeline; |
| | 11 | | using Azure.Storage.Files.DataLake.Models; |
| | 12 | |
|
| | 13 | | namespace Azure.Storage.Files.DataLake |
| | 14 | | { |
| | 15 | | internal static class ErrorExtensions |
| | 16 | | { |
| | 17 | | // clientDiagnostics parameter is a pattern expected by the codegenerator |
| | 18 | | #pragma warning disable CA1801 |
| | 19 | | internal static Exception CreateException(this string body, ClientDiagnostics clientDiagnostics, Response respon |
| | 20 | | #pragma warning restore CA1801 |
| | 21 | | { |
| | 22 | | // xml |
| 432 | 23 | | if (response.Headers.ContentType != null |
| 432 | 24 | | && response.Headers.ContentType.Contains("application/xml")) |
| | 25 | | { |
| 4 | 26 | | XElement error = XDocument.Parse(body).Element("Error"); |
| 4 | 27 | | string code = error.Element("Code").Value.ToString(CultureInfo.InvariantCulture); |
| 4 | 28 | | string message = error.Element("Message").Value.ToString(CultureInfo.InvariantCulture); |
| 4 | 29 | | return clientDiagnostics.CreateRequestFailedExceptionWithContent( |
| 4 | 30 | | response: response, |
| 4 | 31 | | message: message, |
| 4 | 32 | | errorCode: code); |
| | 33 | | } |
| | 34 | | // json |
| 428 | 35 | | else if (response.Headers.ContentType != null |
| 428 | 36 | | && response.Headers.ContentType.Contains("application/json")) |
| | 37 | | { |
| 420 | 38 | | JsonDocument json = JsonDocument.Parse(body); |
| 420 | 39 | | JsonElement error = json.RootElement.GetProperty("error"); |
| | 40 | |
|
| 420 | 41 | | IDictionary<string, string> details = default; |
| 420 | 42 | | if (error.TryGetProperty("detail", out JsonElement detail)) |
| | 43 | | { |
| 4 | 44 | | details = new Dictionary<string, string>(); |
| 24 | 45 | | foreach (JsonProperty property in detail.EnumerateObject()) |
| | 46 | | { |
| 8 | 47 | | details[property.Name] = property.Value.GetString(); |
| | 48 | | } |
| | 49 | | } |
| | 50 | |
|
| 420 | 51 | | return clientDiagnostics.CreateRequestFailedExceptionWithContent( |
| 420 | 52 | | response: response, |
| 420 | 53 | | message: error.GetProperty("message").GetString(), |
| 420 | 54 | | errorCode: error.GetProperty("code").GetString(), |
| 420 | 55 | | additionalInfo: details); |
| | 56 | | } |
| | 57 | | else |
| | 58 | | { |
| 8 | 59 | | return new RequestFailedException( |
| 8 | 60 | | status: response.Status, |
| 8 | 61 | | errorCode: response.Status.ToString(CultureInfo.InvariantCulture), |
| 8 | 62 | | message: response.ReasonPhrase, |
| 8 | 63 | | innerException: new Exception()); |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| | 67 | | } |
| | 68 | | } |