/// <summary>
        /// Translates the specified exception into a storage exception.
        /// </summary>
        /// <param name="ex">The exception to translate.</param>
        /// <param name="reqResult">The request result.</param>
        /// <returns>The storage exception.</returns>
        public static StorageException TranslateException(Exception ex, RequestResult reqResult)
        {
            // Dont re-wrap storage exceptions
            if (ex is StorageException)
            {
                return((StorageException)ex);
            }
            else if (ex is TimeoutException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = (int)HttpStatusCode.Unused;
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex));
            }
            else if (ex is ArgumentException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = (int)HttpStatusCode.Unused;
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex)
                {
                    IsRetryable = false
                });
            }
#if RT
            else if (ex is OperationCanceledException)
            {
                reqResult.HttpStatusMessage        = null;
                reqResult.HttpStatusCode           = 306; // unused
                reqResult.ExtendedErrorInformation = null;
                return(new StorageException(reqResult, ex.Message, ex));
            }
#elif DNCP
            else
            {
                StorageException tableEx = TableUtilities.TranslateDataServiceClientException(ex, reqResult);

                if (tableEx != null)
                {
                    return(tableEx);
                }
            }
#endif

            WebException we = ex as WebException;
            if (we != null)
            {
                try
                {
                    HttpWebResponse response = we.Response as HttpWebResponse;
                    if (response != null)
                    {
                        reqResult.HttpStatusMessage = response.StatusDescription;
                        reqResult.HttpStatusCode    = (int)response.StatusCode;
                        if (response.Headers != null)
                        {
#if DNCP
                            reqResult.ServiceRequestID = HttpUtility.TryGetHeader(response, Constants.HeaderConstants.RequestIdHeader, null);
                            reqResult.ContentMd5       = HttpUtility.TryGetHeader(response, "Content-MD5", null);
                            string tempDate = HttpUtility.TryGetHeader(response, "Date", null);
                            reqResult.RequestDate = string.IsNullOrEmpty(tempDate) ? DateTime.Now.ToString("R", CultureInfo.InvariantCulture) : tempDate;
                            reqResult.Etag        = response.Headers[HttpResponseHeader.ETag];
#endif
                        }
#if RT
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream().AsInputStream());
#else
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream());
#endif
                    }
                }
                catch (Exception)
                {
                    // no op
                }
            }

            // Not WebException, just wrap in StorageException
            return(new StorageException(reqResult, ex.Message, ex));
        }
示例#2
0
        /// <summary>
        /// Translates the specified exception into a storage exception.
        /// </summary>
        /// <param name="ex">The exception to translate.</param>
        /// <param name="reqResult">The request result.</param>
        /// <param name="parseError">The delegate used to parse the error to get extended error information.</param>
        /// <param name="response">HTTP response message</param>
        /// <returns>The storage exception.</returns>
        public static StorageException TranslateException(Exception ex, RequestResult reqResult, Func <Stream, StorageExtendedErrorInformation> parseError, HttpResponseMessage response)
        {
            StorageException storageException;

            try
            {
                if ((storageException = CoreTranslate(ex, reqResult, ref parseError)) != null)
                {
                    return(storageException);
                }

                if (response != null)
                {
                    StorageException.PopulateRequestResult(reqResult, response);
                    reqResult.ExtendedErrorInformation = CommonUtility.RunWithoutSynchronizationContext(() => StorageExtendedErrorInformation.ReadFromStream(response.Content.ReadAsStreamAsync().Result));
                }
            }
            catch (Exception)
            {
                // if there is an error thrown while parsing the service error, just wrap the service error in a StorageException.
                // no op
            }

            // Just wrap in StorageException
            return(new StorageException(reqResult, ex.Message, ex));
        }