Ejemplo n.º 1
0
        /// <summary>
        /// Parses the error details from the stream using OData library.
        /// </summary>
        /// <param name="responseMessage">The IODataResponseMessage to parse.</param>
        /// <returns>The error details.</returns>
        public static StorageExtendedErrorInformation ReadAndParseExtendedError(IODataResponseMessage responseMessage)
        {
            StorageExtendedErrorInformation storageExtendedError = null;

            using (ODataMessageReader reader = new ODataMessageReader(responseMessage))
            {
                try
                {
                    ODataError error = reader.ReadError();
                    if (error != null)
                    {
                        storageExtendedError = new StorageExtendedErrorInformation();
                        storageExtendedError.AdditionalDetails = new Dictionary <string, string>();
                        storageExtendedError.ErrorCode         = error.ErrorCode;
                        storageExtendedError.ErrorMessage      = error.Message;
                        if (error.InnerError != null)
                        {
                            storageExtendedError.AdditionalDetails[Constants.ErrorExceptionMessage]    = error.InnerError.Message;
                            storageExtendedError.AdditionalDetails[Constants.ErrorExceptionStackTrace] = error.InnerError.StackTrace;
                        }

#if !(WINDOWS_PHONE && WINDOWS_DESKTOP)
                        if (error.InstanceAnnotations.Count > 0)
                        {
                            foreach (ODataInstanceAnnotation annotation in error.InstanceAnnotations)
                            {
                                storageExtendedError.AdditionalDetails[annotation.Name] = annotation.Value.GetAnnotation <string>();
                            }
                        }
#endif
                    }
                }
                catch (Exception)
                {
                    // Error cannot be parsed.
                    return(null);
                }
            }

            return(storageExtendedError);
        }
Ejemplo n.º 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));
        }