/// <summary>
        /// Retrieves the exception details contained in the exception and wrap it in a PowerShell <see cref="ErrorRecord"/>.
        /// </summary>
        /// <param name="exception">The exception containing the error details.</param>
        /// <param name="errorRecord">An output parameter for the error record containing the error details.</param>
        /// <param name="requestId">An output parameter for the request Id present in the reponse headers.</param>
        internal static ErrorRecord RetrieveExceptionDetails(
            Exception exception,
            out string requestId)
        {
            ErrorRecord errorRecord = null;

            requestId = null;

            // Look for known exceptions through the exceptions and inner exceptions
            Exception innerException = exception;

            while (innerException != null)
            {
                CloudException cloudException = innerException as CloudException;
                if (cloudException != null)
                {
                    errorRecord = cloudException.AsErrorRecord(out requestId);
                    break;
                }

                DataServiceRequestException dataServiceRequestException = innerException as DataServiceRequestException;
                if (dataServiceRequestException != null)
                {
                    errorRecord = dataServiceRequestException.AsErrorRecord();
                    break;
                }

                DataServiceClientException dataServiceClientException = innerException as DataServiceClientException;
                if (dataServiceClientException != null)
                {
                    errorRecord = dataServiceClientException.AsErrorRecord();
                    break;
                }

                WebException webException = innerException as WebException;
                if (webException != null)
                {
                    errorRecord = webException.AsErrorRecord(out requestId);
                    break;
                }

                innerException = innerException.InnerException;
            }

            // If it's here, it was an unknown exception, wrap the original exception as is.
            if (errorRecord == null)
            {
                errorRecord = new ErrorRecord(exception, string.Empty, ErrorCategory.NotSpecified, null);
            }

            return(errorRecord);
        }