Ejemplo n.º 1
0
        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());
        }
Ejemplo n.º 2
0
        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);
            }
        }