public void RwsError_has_an_ErrorDescription() { var error = new RwsError(new HttpResponseMessage { Content = new StringContent(@"<?xml version=""1.0"" encoding=""utf-8""?> <ODM xmlns:mdsol=""http://www.mdsol.com/ns/odm/metadata"" FileType=""Snapshot"" CreationDateTime=""2013-04-08T10:28:49.578-00:00"" FileOID=""4d13722a-ceb6-4419-a917-b6ad5d0bc30e"" ODMVersion=""1.3"" mdsol:ErrorDescription=""Incorrect login and password combination. [RWS00008]"" xmlns=""http://www.cdisc.org/ns/odm/v1.3"" />", Encoding.UTF8, "text/xml") }); Assert.AreEqual("Incorrect login and password combination. [RWS00008]", error.GetErrorDescription()); }
public void RWSError_correctly_reads_an_error_response() { string errorResponse = @"<?xml version=""1.0"" encoding=""utf-8""?> <ODM xmlns:mdsol=""http://www.mdsol.com/ns/odm/metadata"" FileType=""Snapshot"" CreationDateTime=""2013-04-08T10:28:49.578-00:00"" FileOID=""4d13722a-ceb6-4419-a917-b6ad5d0bc30e"" ODMVersion=""1.3"" mdsol:ErrorDescription=""Incorrect login and password combination. [RWS00008]"" xmlns=""http://www.cdisc.org/ns/odm/v1.3"" /> "; var error = new RwsError(errorResponse); Assert.AreEqual("4d13722a-ceb6-4419-a917-b6ad5d0bc30e", error.FileOID); Assert.AreEqual("Incorrect login and password combination. [RWS00008]", error.ErrorDescription); Assert.AreEqual(DateTime.Parse("2013-04-08T10:28:49.578-00:00"), error.CreationDateTime); Assert.AreEqual(Schema.ODMVersion.Item13, error.ODMVersion); Assert.AreEqual(Schema.FileType.Snapshot, error.FileType); }
/// <summary> /// Sends a request to RWS. /// </summary> /// <param name="rws_request">The RWS request.</param> /// <param name="timeout">The timeout.</param> /// <returns></returns> /// <exception cref="RWSException"> /// IIS Error /// or /// Server Error (500) /// or /// Unauthorized. /// or /// Unspecified Error. /// </exception> public IRWSResponse SendRequest(RWSRequest rws_request, int?timeout = null) { this.client = new RestClient(base_url); if (rws_request.RequiresAuthentication) { client.Authenticator = auth; if (timeout != null) { client.Timeout = (int)timeout; } } var request = new RestRequest(rws_request.UrlPath(), rws_request.HttpMethod); //Add post body if the request is a POST if (rws_request.HttpMethod == Method.POST) { request.AddParameter("text/xml; charset=utf-8", rws_request.RequestBody, ParameterType.RequestBody); } foreach (var header in rws_request.Headers) { request.AddHeader(header.Key, header.Value); } var start_time = DateTime.UtcNow; var response = client.Execute(request); //keep track of last response this.last_result = response; request_time = DateTime.UtcNow.Subtract(start_time); //Based on the response code... switch (response.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.NotFound: if (response.Content.StartsWith("<Response")) { var error = new RWSErrorResponse(response.Content); throw new RWSException(error.ErrorDescription, error); } else if (response.Content.Contains("<html")) { throw new RWSException("IIS Error", response.Content); } else { var error = new RwsError(response.Content); throw new RWSException(error.ErrorDescription, error); } case HttpStatusCode.InternalServerError: throw new RWSException("Server Error (500)", response.Content); case HttpStatusCode.Forbidden: if (response.Content.Contains("<h2>HTTP Error 401.0 - Unauthorized</h2>")) { throw new RWSException("Unauthorized.", response.Content); } dynamic _error; if (response.Headers.Any(x => x.ContentType.StartsWith("text/xml"))) { if (response.Content.StartsWith("<Response")) { _error = new RWSErrorResponse(response.Content); } else if (response.Content.Contains("ODM")) { _error = new RwsError(response.Content); } else { throw new RWSException("Unspecified Error.", response.Content); } } else { _error = new RWSErrorResponse(response.Content); } throw new RWSException(_error.ErrorDescription, _error); } if (response.StatusCode != HttpStatusCode.OK) { dynamic _error; if (response.Content.Contains("<")) { if (response.Content.Trim().StartsWith("<Response")) { _error = new RWSErrorResponse(response.Content); } else if (response.Content.Contains("ODM")) { _error = new RwsError(response.Content); } else { throw new RWSException(string.Format("Unexpected Status Code ({0})", response.StatusCode.ToString()), response.Content); } } else { throw new RWSException(string.Format("Unexpected Status Code ({0})", response.StatusCode.ToString()), response.Content); } throw new RWSException(_error.ErrorDescription, _error); } return(rws_request.Result(response)); }
/// <summary> /// Initializes a new instance of the <see cref="RWSException"/> class. /// </summary> /// <param name="message">The message.</param> /// <param name="rws_error">The RWS error.</param> public RWSException(string message, RwsError rws_error) : this(message) { this.rws_error = rws_error; }
public static void Parse(HttpResponseMessage response) { string content = response.Content.ReadAsStringAsync().Result; var responseContentTypeHeader = response.Content.Headers.FirstOrDefault(x => x.Key == "Content-Type"); switch (response.StatusCode) { case HttpStatusCode.BadRequest: case HttpStatusCode.NotFound: if (content.StartsWith("<Response", StringComparison.CurrentCulture)) { var error = GenerateResponse(response); throw new RwsException(error.GetErrorDescription(), error); } if (content.Contains("<html")) { throw new RwsException("IIS Error", response); } else { var error = new RwsError(response); throw new RwsException(error.GetErrorDescription(), response); } case HttpStatusCode.InternalServerError: throw new RwsException("Server Error (500)", response); case HttpStatusCode.Unauthorized: if (content.Contains("Authorization Header not provided")) { throw new RwsAuthorizationException(content); } if (content.Contains("<h2>HTTP Error 401.0 - Unauthorized</h2>")) { throw new RwsException("Unauthorized.", response); } IRwsError _error; if (responseContentTypeHeader.Value.Any(x => x.StartsWith("text/xml", StringComparison.CurrentCulture))) { if (content.StartsWith("<Response", StringComparison.CurrentCulture)) { _error = GenerateResponse(response); } else if (content.Contains("ODM")) { _error = new RwsError(response); } else { throw new RwsException("Unspecified Error.", response); } } else { _error = GenerateResponse(response); } throw new RwsException(_error.GetErrorDescription(), _error); } if (!response.IsSuccessStatusCode) { IRwsError _error; if (content.Contains("<")) { if (content.Trim().StartsWith("<Response", StringComparison.CurrentCulture)) { _error = GenerateResponse(response); } else if (content.Contains("ODM")) { _error = new RwsError(response); } else { throw new RwsException($"Unexpected Status Code ({(int)response.StatusCode})", response); } } else { throw new RwsException($"Unexpected Status Code ({(int)response.StatusCode})", response); } throw new RwsException(_error.GetErrorDescription(), _error); } }