/// <summary>
        /// Translates the data service client exception.
        /// </summary>
        /// <param name="e">The exception.</param>
        /// <returns>The translated exception.</returns>
        internal static StorageException TranslateDataServiceClientException(Exception e, RequestResult reqResult)
        {
            DataServiceClientException dsce = FindInnerExceptionOfType <DataServiceClientException>(e);

            if (dsce == null)
            {
                InvalidOperationException ioe = TableUtilities.FindInnerExceptionOfType <InvalidOperationException>(e);

                if (ioe != null && !(ioe is WebException) && ioe.Source == "System.Data.Services.Client" && ioe.Message.Contains("type is not compatible with the expected"))
                {
                    return(new StorageException(reqResult, e.Message, e)
                    {
                        IsRetryable = false
                    });
                }

                return(null);
            }
            else
            {
                reqResult.ExtendedErrorInformation =
                    StorageExtendedErrorInformation.ReadFromStream(new MemoryStream(Encoding.UTF8.GetBytes(dsce.Message)));
                reqResult.HttpStatusCode = dsce.StatusCode;

                return(new StorageException(
                           reqResult,
                           reqResult.ExtendedErrorInformation != null ? reqResult.ExtendedErrorInformation.ErrorCode : dsce.Message,
                           dsce));
            }
        }
        /// <summary>
        /// Translates the data service exception.
        /// </summary>
        /// <param name="e">The exception.</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 translated exception.
        /// </returns>
        internal static StorageException TranslateDataServiceException(Exception e, RequestResult reqResult, Func <Stream, IDictionary <string, string>, StorageExtendedErrorInformation> parseError)
        {
            try
            {
                // The exception thrown is based on whether it is a change/query operation.
                DataServiceRequestException dsre = FindInnerExceptionOfType <DataServiceRequestException>(e);

                DataServiceQueryException dsqe = FindInnerExceptionOfType <DataServiceQueryException>(e);

                if (dsre == null && dsqe == null)
                {
                    InvalidOperationException ioe = TableUtilities.FindInnerExceptionOfType <InvalidOperationException>(e);

                    if (ioe != null && !(ioe is WebException) && string.CompareOrdinal(ioe.Source, "Microsoft.Data.Services.Client") == 0 && ioe.Message.Contains("type is not compatible with the expected"))
                    {
                        return(new StorageException(reqResult, e.Message, e)
                        {
                            IsRetryable = false
                        });
                    }

                    return(null);
                }
                else if (dsre != null)
                {
                    DataServiceResponse response = dsre.Response;

                    // Get the batch status code first in case batch does not contain any responses.
                    reqResult.HttpStatusCode = response.BatchStatusCode;

                    IDictionary <string, string> headers;
                    foreach (OperationResponse operationResponse in response)
                    {
                        reqResult.HttpStatusCode = operationResponse.StatusCode;

                        // The exception thrown will contain the first error in the group of requests.
                        if (reqResult.HttpStatusCode >= 300)
                        {
                            headers = operationResponse.Headers;

                            // Strip off the extra exception type at the beginning.
                            string innerException = dsre.InnerException.ToString().Replace("System.Data.Services.Client.DataServiceClientException: ", string.Empty);

                            using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(innerException)))
                            {
                                reqResult.ExtendedErrorInformation = parseError(stream, headers);
                            }

                            break;
                        }
                    }

                    return(new StorageException(
                               reqResult,
                               reqResult.ExtendedErrorInformation != null ? reqResult.ExtendedErrorInformation.ErrorCode : dsre.Message,
                               dsre));
                }
                else
                {
                    QueryOperationResponse response = dsqe.Response;

                    reqResult.HttpStatusCode = response.StatusCode;

                    string innerException = dsqe.InnerException.Message;

                    using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(innerException)))
                    {
                        reqResult.ExtendedErrorInformation = parseError(stream, response.Headers);
                    }

                    return(new StorageException(
                               reqResult,
                               reqResult.ExtendedErrorInformation != null ? reqResult.ExtendedErrorInformation.ErrorCode : dsqe.Message,
                               dsqe));
                }
            }
            catch (Exception)
            {
                return(new StorageException(reqResult, e.Message, e));
            }
        }