public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails) { HttpStatusCode httpStatusCode; string operationId; return(TryGetExceptionDetails(exception, out errorDetails, out httpStatusCode, out operationId)); }
protected virtual void WriteErrorDetails(CommunicationException exception) { ServiceManagementError error = null; ServiceManagementHelper.TryGetExceptionDetails(exception, out error); if (error == null) { SafeWriteError(new ErrorRecord(exception, string.Empty, ErrorCategory.CloseError, null)); } else { string errorDetails = string.Format( CultureInfo.InvariantCulture, "HTTP Status Code: {0} - HTTP Error Message: {1}", error.Code, error.Message); SafeWriteError(new ErrorRecord(new CommunicationException(errorDetails), string.Empty, ErrorCategory.CloseError, null)); } }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return(false); } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return(true); } WebException wex = exception.InnerException as WebException; if (wex == null) { return(false); } HttpWebResponse response = wex.Response as HttpWebResponse; if (response == null) { return(false); } //httpStatusCode = response.StatusCode; //if (httpStatusCode == HttpStatusCode.Forbidden) //{ // return true; //} if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } // Don't wrap responseStream in a using statement to prevent it // from being disposed twice (as it's disposed by reader if it is // successfully disposed). Stream responseStream = null; try { responseStream = response.GetResponseStream(); if (responseStream.Length == 0) { return(false); } try { using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(responseStream, new XmlDictionaryReaderQuotas())) { // Release the reference to responseStream since it // will be closed when the reader is diposed responseStream = null; DataContractSerializer ser = new DataContractSerializer(typeof(ServiceManagementError)); errorDetails = (ServiceManagementError)ser.ReadObject(reader, true); } } catch (SerializationException) { return(false); } } finally { if (responseStream != null) { responseStream.Dispose(); } } return(true); }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails, out HttpStatusCode httpStatusCode, out string operationId) { errorDetails = null; httpStatusCode = 0; operationId = null; if (exception == null) { return false; } if (exception.Message == "Internal Server Error") { httpStatusCode = HttpStatusCode.InternalServerError; return true; } WebException wex = exception.InnerException as WebException; if (wex == null) { return false; } HttpWebResponse response = wex.Response as HttpWebResponse; if (response == null) { return false; } //httpStatusCode = response.StatusCode; //if (httpStatusCode == HttpStatusCode.Forbidden) //{ // return true; //} if (response.Headers != null) { operationId = response.Headers[Constants.OperationTrackingIdHeader]; } // Don't wrap responseStream in a using statement to prevent it // from being disposed twice (as it's disposed by reader if it is // successfully disposed). Stream responseStream = null; try { responseStream = response.GetResponseStream(); if (responseStream.Length == 0) { return false; } try { using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(responseStream, new XmlDictionaryReaderQuotas())) { // Release the reference to responseStream since it // will be closed when the reader is diposed responseStream = null; DataContractSerializer ser = new DataContractSerializer(typeof(ServiceManagementError)); errorDetails = (ServiceManagementError)ser.ReadObject(reader, true); } } catch (SerializationException) { return false; } } finally { if (responseStream != null) { responseStream.Dispose(); } } return true; }
public static bool TryGetExceptionDetails(CommunicationException exception, out ServiceManagementError errorDetails) { HttpStatusCode httpStatusCode; string operationId; return TryGetExceptionDetails(exception, out errorDetails, out httpStatusCode, out operationId); }