Esempio n. 1
0
        /// <summary>
        /// Process a <see cref="DataServiceRequestException"/> and converts it to a PowerShell
        /// <see cref="ErrorRecord"/>, or <c>null</c> if that is not availaible.
        /// </summary>
        /// <param name="ex">The <see cref="DataServiceRequestException"/> that was thrown.</param>
        private static ErrorRecord AsErrorRecord(this DataServiceRequestException ex)
        {
            ErrorRecord errorRecord      = null;
            Exception   exceptionToThrow = ex;

            // Look for Sql exception message in response, return that message if found.
            foreach (ChangeOperationResponse change in ex.Response)
            {
                // Try to parse the extended properties in the Exception
                ManagementServiceExceptionInfo info;
                if (ManagementServiceExceptionInfo.TryParse(change, out info))
                {
                    if (info.PropertyBag.Contains(DataServiceConstants.SqlMessageTextKey))
                    {
                        // Set the exception to throw as a new exception with the server message
                        string errorDetails =
                            info.PropertyBag[DataServiceConstants.SqlMessageTextKey].ToString();

                        errorRecord = new ErrorRecord(
                            new CommunicationException(errorDetails, ex),
                            string.Empty,
                            ErrorCategory.InvalidOperation,
                            null);
                        break;
                    }
                }
            }

            // Fall back to use the message in the message element if availiable
            if (errorRecord == null)
            {
                foreach (ChangeOperationResponse change in ex.Response)
                {
                    try
                    {
                        XElement errorRoot      = XElement.Parse(change.Error.Message);
                        XElement messageElement = errorRoot.Descendants().Single(x => string.Equals(x.Name.LocalName, "message", StringComparison.OrdinalIgnoreCase));

                        errorRecord = new ErrorRecord(
                            new CommunicationException(messageElement.Value, ex),
                            string.Empty,
                            ErrorCategory.InvalidOperation,
                            null);
                        break;
                    }
                    catch (Exception)
                    {
                        // we hide any parsing error that might have happened because there is no need to expose
                        // additional errors
                    }
                }
            }

            // Return the resulting error record
            return(errorRecord);
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to convert the given exception response to a <see cref="ManagementServiceExceptionInfo"/>.
        /// </summary>
        /// <param name="response">The response containing the xml serialized error information.</param>
        /// <param name="info">The converted <see cref="ManagementServiceExceptionInfo"/> containing errors from the exception.</param>
        /// <returns><c>true</c> if parsing succeeded.</returns>
        public static bool TryParse(OperationResponse response, out ManagementServiceExceptionInfo info)
        {
            if (response != null && response.Error != null)
            {
                return(ManagementServiceExceptionInfo.TryParse(response.Error.Message, out info));
            }

            info = default(ManagementServiceExceptionInfo);
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Converts the given exception message to a <see cref="ManagementServiceExceptionInfo"/>.
        /// </summary>
        /// <param name="message">The message containing the xml serialized error information.</param>
        /// <returns>The converted <see cref="ManagementServiceExceptionInfo"/> containing errors from the exception</returns>
        public static ManagementServiceExceptionInfo Parse(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("message");
            }

            ManagementServiceExceptionInfo info;

            if (ManagementServiceExceptionInfo.TryParse(message, out info))
            {
                return(info);
            }
            else
            {
                throw new FormatException(Resources.InvalidExceptionMessageFormat);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Converts the given exception response to a <see cref="ManagementServiceExceptionInfo"/>.
        /// </summary>
        /// <param name="response">The response containing the xml serialized error information.</param>
        /// <returns>The converted <see cref="ManagementServiceExceptionInfo"/> containing errors from the exception.</returns>
        public static ManagementServiceExceptionInfo Parse(OperationResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (response.Error == null)
            {
                throw new ArgumentException(Resources.InvalidErrorInResponse, "response");
            }

            ManagementServiceExceptionInfo info;

            if (ManagementServiceExceptionInfo.TryParse(response, out info))
            {
                return(info);
            }
            else
            {
                throw new FormatException(Resources.InvalidExceptionMessageFormat);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Process a <see cref="WebException"/> and converts it to a PowerShell
        /// <see cref="ErrorRecord"/>, or <c>null</c> if that is not availaible.
        /// </summary>
        /// <param name="ex">The <see cref="WebException"/> that was thrown.</param>
        /// <param name="requestId">The request Id from the response, if it's availiable.</param>
        /// <returns>An <see cref="ErrorRecord"/> containing the exception details,
        /// or <c>null</c> if the exception cannot be parsed.</returns>
        private static ErrorRecord AsErrorRecord(
            this WebException ex,
            out string requestId)
        {
            ErrorRecord errorRecord = null;

            requestId = null;

            if (ex.Response != null)
            {
                HttpWebResponse response = ex.Response as HttpWebResponse;

                // Extract the request Ids
                if (response.Headers != null)
                {
                    requestId = response.Headers[Constants.RequestIdHeaderName];
                }

                // Retrieve the full exception response
                Stream responseStream = response.GetResponseStream();
                string exceptionResponse;
                using (StreamReader responseReader = new StreamReader(responseStream))
                {
                    exceptionResponse = responseReader.ReadToEnd();
                }

                // Check if it's a service resource error message
                ServiceResourceError serviceResourceError;
                if (errorRecord == null &&
                    ServiceResourceError.TryParse(exceptionResponse, out serviceResourceError))
                {
                    errorRecord = new ErrorRecord(
                        new CommunicationException(serviceResourceError.Message, ex),
                        string.Empty,
                        ErrorCategory.InvalidOperation,
                        null);
                }

                // Check if it's a management service error message
                ManagementServiceExceptionInfo info;
                if (errorRecord == null &&
                    ManagementServiceExceptionInfo.TryParse(exceptionResponse, out info))
                {
                    if (info.PropertyBag.Contains(DataServiceConstants.SqlMessageTextKey))
                    {
                        // Set the exception to throw as a new exception with the server message
                        string errorDetails =
                            info.PropertyBag[DataServiceConstants.SqlMessageTextKey].ToString();

                        errorRecord = new ErrorRecord(
                            new CommunicationException(errorDetails, ex),
                            string.Empty,
                            ErrorCategory.InvalidOperation,
                            null);
                    }
                }

                // Check if it's a database management error message
                SqlDatabaseManagementError databaseManagementError;
                if (errorRecord == null &&
                    SqlDatabaseManagementError.TryParse(exceptionResponse, out databaseManagementError))
                {
                    string errorDetails = string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.DatabaseManagementErrorFormat,
                        databaseManagementError.Code,
                        databaseManagementError.Message);

                    errorRecord = new ErrorRecord(
                        new CommunicationException(errorDetails, ex),
                        string.Empty,
                        ErrorCategory.InvalidOperation,
                        null);
                }

                // Check if it's a not found message
                if (errorRecord == null &&
                    response.StatusCode == HttpStatusCode.NotFound)
                {
                    string message = string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.UriDoesNotExist,
                        response.ResponseUri.AbsoluteUri);
                    string errorDetails = string.Format(
                        CultureInfo.InvariantCulture,
                        Resources.DatabaseManagementErrorFormat,
                        response.StatusCode.ToString(),
                        message);

                    errorRecord = new ErrorRecord(
                        new CommunicationException(errorDetails, ex),
                        string.Empty,
                        ErrorCategory.InvalidOperation,
                        null);
                }
            }

            // Return the resulting error record
            return(errorRecord);
        }