public static string ParseWebException(WebException webException) { if (webException == null) return string.Empty; var responseContent = GetResponseString(webException.Response); var response = webException.Response as HttpWebResponse; if (response != null) return string.Format(CultureInfo.InvariantCulture, "{0} {1}", response.StatusCode, responseContent).Trim(); if (string.IsNullOrWhiteSpace(responseContent)) return responseContent; return webException.GetBaseException().Message; }
/// <summary> /// Will attempt to wrap the exception, returning true if the exception was wrapped, or returning false if it was not (in which case /// the original exception should be thrown). /// </summary> /// <param name="requestContext"></param> /// <param name="webException"></param> /// <param name="wrappedException">The wrapped exception (will be set to the webException, if this method returns <c>false</c></param> /// <returns><c>true</c>, if the authException should be throw, <c>false</c> if the wrapped web exception should be thrown</returns> public static bool TryWrapException(IOAuthContext requestContext, WebException webException, out Exception wrappedException) { try { string content = webException.Response.ReadToEnd(); if (content.Contains(Parameters.OAuth_Problem)) { var report = new OAuthProblemReport(content); wrappedException = new OAuthException(report.ProblemAdvice ?? report.Problem, webException) { Context = requestContext, Report = report }; } else { wrappedException = new ParsedWebException(content, webException.Message, webException.GetBaseException(), webException.Status, webException.Response); } return true; } catch { wrappedException = webException; return false; } }
private void FillStatus(ReplicationInfoStatus replicationInfoStatus, WebException e) { if (e.GetBaseException() is WebException) e = (WebException) e.GetBaseException(); var response = e.Response as HttpWebResponse; if (response == null) { replicationInfoStatus.Status = e.Message; replicationInfoStatus.Code = -1 * (int)e.Status; return; } switch (response.StatusCode) { case HttpStatusCode.BadRequest: string error = GetErrorStringFromException(e, response); replicationInfoStatus.Status = error.Contains("Could not figure out what to do") ? "Replication Bundle not activated." : error; replicationInfoStatus.Code = (int)response.StatusCode; break; case HttpStatusCode.PreconditionFailed: replicationInfoStatus.Status = "Could not authenticate using OAuth's API Key"; replicationInfoStatus.Code = (int)response.StatusCode; break; case HttpStatusCode.Forbidden: case HttpStatusCode.Unauthorized: replicationInfoStatus.Status = "Could not authenticate using Windows Auth"; replicationInfoStatus.Code = (int)response.StatusCode; break; default: replicationInfoStatus.Status = response.StatusDescription; replicationInfoStatus.Code = (int)response.StatusCode; break; } }
public static Exception ParseStringWebException(WebException webException) { if (webException == null) { return null; } try { var serializer = new DataContractSerializer(typeof(string)); var stream = webException.Response.GetResponseStream(); var errorMessage = serializer.ReadObject(stream) as string; return new Exception(errorMessage); } catch { if (webException.Response is HttpWebResponse) { var response = webException.Response as HttpWebResponse; return new HttpWebException( string.IsNullOrWhiteSpace(response.StatusDescription) ? webException.Message : response.StatusDescription, response.StatusCode, webException); } else { return webException.GetBaseException(); } } }
public static Exception ParseXmlWebException(WebException webException) { if (webException == null) { return null; } try { var stream = webException.Response.GetResponseStream(); var streamReader = new StreamReader(stream); var doc = XDocument.Parse(streamReader.ReadToEnd()); return ParseXmlExceptionDocument(doc.Root); } catch { if (webException.Response is HttpWebResponse) { var response = webException.Response as HttpWebResponse; return new HttpWebException( string.IsNullOrWhiteSpace(response.StatusDescription) ? webException.Message : response.StatusDescription, response.StatusCode, webException); } else { return webException.GetBaseException(); } } }