Beispiel #1
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));
        }
        /// <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>
        /// <returns>The storage exception.</returns>
        public static StorageException TranslateException(Exception ex, RequestResult reqResult, Func <Stream, StorageExtendedErrorInformation> parseError)
        {
            StorageException storageException;

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

                WebException we = ex as WebException;
                if (we != null)
                {
                    HttpWebResponse response = we.Response as HttpWebResponse;
                    if (response != null)
                    {
                        StorageException.PopulateRequestResult(reqResult, response);
#if WINDOWS_RT
                        reqResult.ExtendedErrorInformation = StorageExtendedErrorInformation.ReadFromStream(response.GetResponseStream().AsInputStream());
#else
                        reqResult.ExtendedErrorInformation = parseError(response.GetResponseStream());
#endif
                    }
                }
            }
            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));
        }
Beispiel #3
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="parseErrorAsync">The delegate used to parse the error to get extended error information.</param>
        /// <param name="cancellationToken">cancellation token for the async operation</param>
        ///
        /// <returns>The storage exception.</returns>
        public static async Task <StorageException> TranslateExceptionAsync(Exception ex, RequestResult reqResult, Func <Stream, CancellationToken, Task <StorageExtendedErrorInformation> > parseErrorAsync, CancellationToken cancellationToken, HttpResponseMessage response)
        {
            StorageException storageException;

            try
            {
                if (parseErrorAsync == null)
                {
                    parseErrorAsync = StorageExtendedErrorInformation.ReadFromStreamAsync;
                }

                if ((storageException = CoreTranslateAsync(ex, reqResult, cancellationToken)) != null)
                {
                    return(storageException);
                }

                if (response != null)
                {
                    StorageException.PopulateRequestResult(reqResult, response);
                    reqResult.ExtendedErrorInformation = await parseErrorAsync(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception)
            {
                // if there is an error thrown while parsing the service error, just wrap the service error in a StorageException.
                // The idea is that it's more helpful for the user to get the actual service error than a "parsing failed" error.
            }

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