internal static async Task <StorageException> TranslateExceptionWithPreBufferedStreamAsync(Exception ex, RequestResult reqResult, Stream responseStream, HttpResponseMessage response, Func <Stream, HttpResponseMessage, string, CancellationToken, Task <StorageExtendedErrorInformation> > parseErrorAsync, CancellationToken cancellationToken)
 {
     try
     {
         StorageException result;
         if ((result = CoreTranslateAsync(ex, reqResult, CancellationToken.None)) != null)
         {
             return(result);
         }
         if (response != null)
         {
             PopulateRequestResult(reqResult, response);
             if (parseErrorAsync == null)
             {
                 reqResult.ExtendedErrorInformation = await StorageExtendedErrorInformationRestHelper.ReadFromStreamAsync(responseStream, cancellationToken);
             }
             else
             {
                 reqResult.ExtendedErrorInformation = parseErrorAsync(responseStream, response, response.Content.Headers.ContentType.ToString(), cancellationToken).Result;
             }
         }
     }
     catch (Exception)
     {
     }
     return(new StorageException(reqResult, ex.Message, ex));
 }
        internal static async Task <StorageException> PopulateStorageExceptionFromHttpResponseMessage(HttpResponseMessage response, RequestResult currentResult, CancellationToken token, Func <Stream, HttpResponseMessage, string, CancellationToken, Task <StorageExtendedErrorInformation> > parseErrorAsync)
        {
            if (response.IsSuccessStatusCode)
            {
                return(null);
            }
            try
            {
                currentResult.HttpStatusCode    = (int)response.StatusCode;
                currentResult.HttpStatusMessage = (currentResult.HttpStatusCode.Equals(400) ? "The remote server returned an error: (400) Bad Request." : response.ReasonPhrase);
                currentResult.ServiceRequestID  = response.Headers.GetHeaderSingleValueOrDefault("x-ms-request-id");
                string headerSingleValueOrDefault = response.Headers.GetHeaderSingleValueOrDefault("x-ms-date");
                currentResult.RequestDate = (string.IsNullOrEmpty(headerSingleValueOrDefault) ? DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture) : headerSingleValueOrDefault);
                if (response.Headers.ETag != null)
                {
                    currentResult.Etag = response.Headers.ETag.ToString();
                }
                if (response.Content != null && response.Content.Headers.ContentMD5 != null)
                {
                    currentResult.ContentMd5 = Convert.ToBase64String(response.Content.Headers.ContentMD5);
                }
                currentResult.ErrorCode = response.Headers.GetHeaderSingleValueOrDefault("x-ms-error-code");
            }
            catch (Exception)
            {
            }
            try
            {
                Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(continueOnCapturedContext: false);

                if (parseErrorAsync != null)
                {
                    currentResult.ExtendedErrorInformation = await parseErrorAsync(stream, response, response.Content.Headers.ContentType.ToString(), token).ConfigureAwait(continueOnCapturedContext: false);
                }
                else
                {
                    currentResult.ExtendedErrorInformation = await StorageExtendedErrorInformationRestHelper.ReadFromStreamAsync(stream, token).ConfigureAwait(continueOnCapturedContext: false);
                }
            }
            catch (Exception)
            {
            }
            return(new StorageException(currentResult, currentResult.HttpStatusMessage, null));
        }