/// <summary> /// Tries to deserialized instance of <see cref="ServiceResourceError"/> to its object equivalent. /// A return value indicates whether the conversion succeeded or failed. /// </summary> /// <param name="input">A stream that contains a serialized instance of <see cref="ServiceResourceError"/> to convert.</param> /// <param name="result">When the method returns, contains the deserialized <see cref="ServiceResourceError"/> object, /// if the conversion succeeded, or <c>null</c>, if the conversion failed.</param> /// <returns><c>true</c> if the input parameter is successfully converted; otherwise, <c>false</c>.</returns> public static bool TryParse(string input, out ServiceResourceError result) { result = null; if (input == null) { return false; } // Deserialize the stream using DataContractSerializer. try { using (XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateTextReader( Encoding.UTF8.GetBytes(input), new XmlDictionaryReaderQuotas())) { DataContractSerializer serializer = new DataContractSerializer(typeof(ServiceResourceError)); result = (ServiceResourceError)serializer.ReadObject(xmlReader, true); return true; } } catch (Exception) { } return false; }
/// <summary> /// Tries to deserialized instance of <see cref="ServiceResourceError"/> to its object equivalent. /// A return value indicates whether the conversion succeeded or failed. /// </summary> /// <param name="input">A stream that contains a serialized instance of <see cref="ServiceResourceError"/> to convert.</param> /// <param name="result">When the method returns, contains the deserialized <see cref="ServiceResourceError"/> object, /// if the conversion succeeded, or <c>null</c>, if the conversion failed.</param> /// <returns><c>true</c> if the input parameter is successfully converted; otherwise, <c>false</c>.</returns> public static bool TryParse(string input, out ServiceResourceError result) { result = null; if (input == null) { return(false); } // Deserialize the stream using DataContractSerializer. try { using (XmlDictionaryReader xmlReader = XmlDictionaryReader.CreateTextReader( Encoding.UTF8.GetBytes(input), new XmlDictionaryReaderQuotas())) { DataContractSerializer serializer = new DataContractSerializer(typeof(ServiceResourceError)); result = (ServiceResourceError)serializer.ReadObject(xmlReader, true); return(true); } } catch (Exception) { } return(false); }
/// <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> public static void RetrieveExceptionDetails(Exception exception, out ErrorRecord errorRecord, out string requestId) { errorRecord = null; requestId = null; // Look for known exceptions through the exceptions and inner exceptions Exception innerException = exception; while (innerException != null) { WebException webException = innerException as WebException; if ((webException != null) && (webException.Response != null)) { HttpWebResponse response = webException.Response as HttpWebResponse; // Extract the request Ids and write them as warnings if (response.Headers != null) { requestId = response.Headers[Constants.RequestIdHeaderName]; } using (Stream responseStream = response.GetResponseStream()) { responseStream.Seek(0, SeekOrigin.Begin); // Check if it's a service resource error message ServiceResourceError serviceResourceError; if (ServiceResourceError.TryParse(responseStream, out serviceResourceError)) { errorRecord = new ErrorRecord(new CommunicationException(serviceResourceError.Message), string.Empty, ErrorCategory.InvalidOperation, null); break; } responseStream.Seek(0, SeekOrigin.Begin); // Check if it's a database management error message SqlDatabaseManagementError databaseManagementError; if (SqlDatabaseManagementError.TryParse(responseStream, out databaseManagementError)) { string errorDetails = string.Format( CultureInfo.InvariantCulture, Resources.DatabaseManagementErrorFormat, databaseManagementError.Code, databaseManagementError.Message); errorRecord = new ErrorRecord(new CommunicationException(errorDetails), string.Empty, ErrorCategory.InvalidOperation, null); break; } } // Check if it's a not found message if (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), string.Empty, ErrorCategory.InvalidOperation, null); 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); } }