public void RWSErrorResponse_correctly_reads_an_error_response() { string errorResponse = @"<Response ReferenceNumber=""0b47fe86-542f-4070-9e7d-16396a5ef08a"" InboundODMFileOID = ""Not Supplied"" IsTransactionSuccessful = ""0"" ReasonCode = ""RWS00092"" ErrorClientResponseMessage = ""CRF version not found"" > </Response>"; var response = new RWSErrorResponse(errorResponse); Assert.AreEqual(false, response.IsTransactionSuccessful); Assert.AreEqual("CRF version not found", response.ErrorDescription); Assert.AreEqual("Not Supplied", response.InboundODMFileOID); Assert.AreEqual("RWS00092", response.ReasonCode); Assert.AreEqual("0b47fe86-542f-4070-9e7d-16396a5ef08a", response.ReferenceNumber); }
/// <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="errorDescription">The error description.</param> /// <param name="rws_error_response">The RWS error response.</param> public RWSException(string errorDescription, RWSErrorResponse rws_error_response) : base(errorDescription) { this.errorDescription = errorDescription; this.rws_error_response = rws_error_response; }