Example #1
0
 private void populateFromMWSException(MwsException e)
 {
     if (e.StatusCode != null)
     {
         this.statusCode = (int)e.StatusCode;
     }
     if (e.Message != null)
     {
         this.message = e.Message;
     }
     if (e.errorCode != null)
     {
         this.errorCode = e.ErrorCode;
     }
     if (e.errorType != null)
     {
         this.errorType = e.ErrorType;
     }
     if (e.detail != null)
     {
         this.detail = e.Detail;
     }
     if (e.xml != null)
     {
         this.xml = e.XML;
     }
     if (e.ResponseHeaderMetadata != null)
     {
         this.rhmd = e.ResponseHeaderMetadata;
     }
 }
Example #2
0
        /// <summary>
        /// Initializes exception with information available from the service
        /// </summary>
        /// <param name="statusCode">HTTP status code for error response (as an int)</param>
        /// <param name="message">Error message from HTTP header</param>
        /// <param name="errorCode">Error code from response XML</param>
        /// <param name="errorType">Error type from response XML</param>
        /// <param name="xml">Compete XML found in response</param>
        /// <param name="rhmd">Response header information</param>
        public MwsException(int statusCode, string message, string errorCode, string errorType, string xml, MwsResponseHeaderMetadata rhmd)
            : base()
        {
            this.statusCode = statusCode;
            this.rhmd       = rhmd;
            this.xml        = xml;

            this.errorType = errorType;
            this.errorCode = errorCode;
            this.message   = message;

            if (xml != null)
            {
                try
                {
                    MwsXmlReader    r      = new MwsXmlReader(xml);
                    XmlMwsException parsed = r.Read <XmlMwsException>("Error");

                    this.errorType = parsed.ErrorType;
                    this.errorCode = parsed.ErrorCode;
                    this.message   = parsed.Message;
                    this.detail    = parsed.Detail;
                }
                catch (Exception)
                {
                    // Just eat it
                }
            }
        }
Example #3
0
        /// <summary>
        /// Initializes exception with information available from the service plus from an exception
        /// Needed for backwards compatibility
        /// </summary>
        /// <param name="statusCode">HTTP status code for error response (as an int)</param>
        /// <param name="message">Error message from HTTP header</param>
        /// <param name="errorCode">Error code from response XML</param>
        /// <param name="errorType">Error type from response XML</param>
        /// <param name="xml">Compete XML found in response</param>
        /// <param name="rhmd">Response header information</param>
        /// <param name="cause">Wrapped exception</param>
        public MwsException(int statusCode, string message, string errorCode, string errorType, string xml, MwsResponseHeaderMetadata rhmd, Exception cause)
            : base()
        {
            this.statusCode = statusCode;
            this.rhmd = rhmd;
            this.xml = xml;
            this.errorType = errorType;
            this.errorCode = errorCode;
            this.message = message;

            if (xml != null)
            {
                populateFromXML(xml);
            }

            if (cause is MwsException)
            {
                populateFromMWSException((MwsException)cause);
            }
        }
Example #4
0
        /// <summary>
        /// Initializes exception with information available from the service plus from an exception
        /// Needed for backwards compatibility
        /// </summary>
        /// <param name="statusCode">HTTP status code for error response (as an int)</param>
        /// <param name="message">Error message from HTTP header</param>
        /// <param name="errorCode">Error code from response XML</param>
        /// <param name="errorType">Error type from response XML</param>
        /// <param name="xml">Compete XML found in response</param>
        /// <param name="rhmd">Response header information</param>
        /// <param name="cause">Wrapped exception</param>
        public MwsException(int statusCode, string message, string errorCode, string errorType, string xml, MwsResponseHeaderMetadata rhmd, Exception cause)
            : base()
        {
            this.statusCode = statusCode;
            this.rhmd       = rhmd;
            this.xml        = xml;
            this.errorType  = errorType;
            this.errorCode  = errorCode;
            this.message    = message;

            if (xml != null)
            {
                populateFromXML(xml);
            }

            if (cause is MwsException)
            {
                populateFromMWSException((MwsException)cause);
            }
        }
Example #5
0
 /// <summary>
 /// Initialize and set cause from another exception.
 /// <para>
 /// If cause is an MwsException, copies exception's attributes to this
 /// one, ignoring the statusCode and message parameters that were passed in.
 /// </para>
 /// </summary>
 /// <param name="statusCode">HTTP status code for error response.</param>
 /// <param name="message">Error message from HTTP header.</param>
 /// <param name="cause">Inner exception.</param>
 ////
 public MwsException(int statusCode, string message, Exception cause)
     : base(message, cause)
 {
     if (cause is MwsException)
     {
         MwsException e = (MwsException)cause;
         this.statusCode = (int)e.StatusCode;
         this.message    = e.Message;
         this.errorCode  = e.ErrorCode;
         this.errorType  = e.ErrorType;
         this.detail     = e.Detail;
         this.xml        = e.XML;
         this.rhmd       = e.ResponseHeaderMetadata;
     }
     else
     {
         this.statusCode = statusCode;
         this.message    = message;
     }
 }
Example #6
0
        /// <summary>
        /// Creates a MwsCall and sends the request
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>

        public T Call <T>(IMwsRequestType <T> type,
                          IMwsObject requestData) where T : IMwsObject
        {
            IMwsReader responseReader = null;

            try
            {
                string   servicePath   = type.ServicePath;
                string   operationName = type.OperationName;
                IMwsCall mc            = NewCall(servicePath, operationName);
                requestData.WriteFragmentTo(mc);
                responseReader = mc.invoke();
                MwsResponseHeaderMetadata rhmd = mc.GetResponseMetadataHeader();
                T response = MwsUtil.NewInstance <T>();
                type.SetResponseHeaderMetadata(response, rhmd);
                response.ReadFragmentFrom(responseReader);
                return(response);
            }
            catch (Exception e)
            {
                throw type.WrapException(e);
            }
        }
 public ResponseHeaderMetadata(MwsResponseHeaderMetadata rhmd)
     : base(rhmd)
 {
 }
 public MwsResponseHeaderMetadata(MwsResponseHeaderMetadata rhmd)
     : this(rhmd.RequestId, rhmd.ResponseContext, rhmd.Timestamp, rhmd.QuotaMax, rhmd.QuotaRemaining, rhmd.QuotaResetsAt)
 {
 }
Example #9
0
 /// <summary>
 /// Initializes exception with information available from the service
 /// </summary>
 /// <param name="statusCode">HTTP status code for error response (as an int)</param>
 /// <param name="message">Error message from HTTP header</param>
 /// <param name="errorCode">Error code from response XML</param>
 /// <param name="errorType">Error type from response XML</param>
 /// <param name="xml">Compete XML found in response</param>
 /// <param name="rhmd">Response header information</param>
 public MwsException(int statusCode, string message, string errorCode, string errorType, string xml, MwsResponseHeaderMetadata rhmd)
     : this(statusCode, message, errorCode, errorType, xml, rhmd, null)
 {
 }
Example #10
0
 private void populateFromMWSException( MwsException e )
 {
     if (e.StatusCode != null) this.statusCode = (int)e.StatusCode;
     if (e.Message != null) this.message = e.Message;
     if (e.errorCode != null) this.errorCode = e.ErrorCode;
     if (e.errorType != null) this.errorType = e.ErrorType;
     if (e.detail != null) this.detail = e.Detail;
     if (e.xml != null) this.xml = e.XML;
     if (e.ResponseHeaderMetadata != null) this.rhmd = e.ResponseHeaderMetadata;
 }
 public MwsResponseHeaderMetadata(MwsResponseHeaderMetadata rhmd)
     : this(rhmd.RequestId, rhmd.ResponseContext, rhmd.Timestamp, rhmd.QuotaMax, rhmd.QuotaRemaining, rhmd.QuotaResetsAt)
 {
 }
Example #12
0
 /// <summary>
 /// Initializes exception with information available from the service
 /// </summary>
 /// <param name="statusCode">HTTP status code for error response (as an int)</param>
 /// <param name="message">Error message from HTTP header</param>
 /// <param name="errorCode">Error code from response XML</param>
 /// <param name="errorType">Error type from response XML</param>
 /// <param name="xml">Compete XML found in response</param>
 /// <param name="rhmd">Response header information</param>
 public MwsException(int statusCode, string message, string errorCode, string errorType, string xml, MwsResponseHeaderMetadata rhmd)
     : this(statusCode, message, errorCode, errorType, xml, rhmd, null)
 {
 }
Example #13
0
        /// <summary>
        /// Creates a request and invokes it
        /// </summary>
        /// <returns></returns>
        /// <exception cref="MwsException">Exceptions from invoking the request</exception>
        public IMwsReader invoke()
        {
            string         responseBody;
            string         message;
            HttpStatusCode statusCode = default(HttpStatusCode);

            /* Add required request parameters */
            AddRequiredParameters();
            string queryString = GetParametersAsString(parameters);
            int    retries     = 0;

            do
            {
                /* Submit the request and read response body */
                try
                {
                    request = connection.GetHttpClient(serviceEndPoint.URI);
                    byte[] requestData = new UTF8Encoding().GetBytes(queryString);
                    request.ContentLength = requestData.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(requestData, 0, requestData.Length);
                    }
                    using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse)
                    {
                        statusCode             = httpResponse.StatusCode;
                        message                = httpResponse.StatusDescription;
                        ResponseHeaderMetadata = GetResponseHeaderMetadata(httpResponse);
                        StreamReader reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
                        responseBody = reader.ReadToEnd();
                    }
                    if (statusCode == HttpStatusCode.OK)
                    {
                        return(new MwsXmlReader(responseBody));
                    }
                    MwsException e = new MwsException((int)statusCode, message, null, null, responseBody, ResponseHeaderMetadata);

                    if (statusCode == HttpStatusCode.InternalServerError)
                    {
                        if (PauseIfRetryNeeded(retries++))
                        {
                            continue;
                        }
                    }
                    throw e;
                }
                /* Web exception is thrown on unsuccessful responses */
                catch (WebException we)
                {
                    using (HttpWebResponse httpErrorResponse = (HttpWebResponse)we.Response as HttpWebResponse)
                    {
                        if (httpErrorResponse == null)
                        {
                            throw new MwsException(we);
                        }
                        statusCode = httpErrorResponse.StatusCode;
                        using (StreamReader reader = new StreamReader(httpErrorResponse.GetResponseStream(), Encoding.UTF8))
                        {
                            responseBody = reader.ReadToEnd();
                        }
                    }
                    //retry logic
                    if (PauseIfRetryNeeded(retries++))
                    {
                        continue;
                    }
                    throw new MwsException((int)statusCode, null, null, null, responseBody, null);
                }

                /* Catch other exceptions, attempt to convert to formatted exception,
                 * else rethrow wrapped exception */
                catch (Exception e)
                {
                    throw new MwsException(e);
                }
            } while (true);
        }