Inheritance: System.Exception
Exemple #1
0
        public XElement Execute(IEnumerable <KeyValuePair <string, string> > parameters, Uri baseUri)
        {
            XElement responseElement;

            WebRequest request = HttpWebRequest.Create(BuildUri(baseUri, parameters));

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                    {
                        responseElement = XElement.Load(reader);
                    }
                }
            }
            catch (WebException webException)
            {
                AmazonException serviceException;

                if (AmazonException.TryCreate(webException, out serviceException))
                {
                    throw serviceException;
                }

                throw;
            }

            return(responseElement);
        }
        public static bool TryCreate(WebException webException, out AmazonException amazonException)
        {
            if (webException.Response != null &&
                webException.Response.ContentLength != 0)
            {
                using (XmlReader reader = XmlReader.Create(webException.Response.GetResponseStream()))
                {
                    XElement response = XElement.Load(reader);
                    XElement code     = response.Descendants().Where(e => e.Name.LocalName == "Code").FirstOrDefault();
                    XElement message  = response.Descendants().Where(e => e.Name.LocalName == "Message").FirstOrDefault();

                    if (code != null && message != null)
                    {
                        amazonException = new AmazonException(message.Value, code.Value, webException);
                        return(true);
                    }
                }
            }

            amazonException = null;
            return(false);
        }
        public static bool TryCreate(WebException webException, out AmazonException amazonException)
        {
            if (webException.Response != null &&
                webException.Response.ContentLength != 0)
            {
                using (XmlReader reader = XmlReader.Create(webException.Response.GetResponseStream()))
                {
                    XElement response = XElement.Load(reader);
                    XElement code = response.Descendants().Where(e => e.Name.LocalName == "Code").FirstOrDefault();
                    XElement message = response.Descendants().Where(e => e.Name.LocalName == "Message").FirstOrDefault();

                    if (code != null && message != null)
                    {
                        amazonException = new AmazonException(message.Value, code.Value, webException);
                        return true;
                    }
                }
            }

            amazonException = null;
            return false;
        }