| | | 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 Azure.Core; |
| | | 9 | | |
| | | 10 | | namespace Azure.Storage |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provide helpful information about errors calling Azure Storage endpoints. |
| | | 14 | | /// </summary> |
| | | 15 | | internal static class StorageExceptionExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Attempt to get the error code from a response if it's not provided. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="response">The response.</param> |
| | | 21 | | /// <param name="errorCode">An optional error code.</param> |
| | | 22 | | /// <returns>The response's error code.</returns> |
| | | 23 | | public static string GetErrorCode(this Response response, string errorCode) |
| | | 24 | | { |
| | 174 | 25 | | if (string.IsNullOrEmpty(errorCode)) |
| | | 26 | | { |
| | 2 | 27 | | response.Headers.TryGetValue(Constants.HeaderNames.ErrorCode, out errorCode); |
| | | 28 | | } |
| | 174 | 29 | | return errorCode; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Check if a Response will throw an exception if you try to access |
| | | 34 | | /// its Value property. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <typeparam name="T">Type of the Response Value.</typeparam> |
| | | 37 | | /// <param name="response">The response to check.</param> |
| | | 38 | | /// <returns>True if the response will throw.</returns> |
| | | 39 | | public static bool IsUnavailable<T>(this Response<T> response) => |
| | 116 | 40 | | (response?.GetRawResponse().Status ?? 0) == 304; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Create a response that will throw an exception if you try to access |
| | | 44 | | /// its Value property. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <typeparam name="T">Type of the Response Value.</typeparam> |
| | | 47 | | /// <param name="rawResponse">The raw response.</param> |
| | | 48 | | /// <returns>A response that will throw if accessed.</returns> |
| | | 49 | | public static Response<T> AsNoBodyResponse<T>(this Response rawResponse) => |
| | 0 | 50 | | new NoBodyResponse<T>(rawResponse); |
| | | 51 | | } |
| | | 52 | | } |