Inheritance: System.Exception, ISerializable
Ejemplo n.º 1
0
        void getResponseCallback <T>(IAsyncResult result) where T : new()
        {
            SDBAsyncResult sdbAsyncResult = result as SDBAsyncResult;

            if (null == sdbAsyncResult)
            {
                sdbAsyncResult = result.AsyncState as SDBAsyncResult;
            }

            sdbAsyncResult.RequestState.GetResponseCallbackCalled = true;
            bool shouldRetry = false;

            try
            {
                HttpStatusCode  statusCode;
                RequestState    state        = sdbAsyncResult.RequestState;
                HttpWebResponse httpResponse = null;
                try
                {
                    httpResponse = state.WebRequest.EndGetResponse(result) as HttpWebResponse;
                    sdbAsyncResult.FinalSDBResponse = handleHttpResponse <T>(httpResponse, out statusCode) as SimpleDBResponse;
                }
                catch (WebException we)
                {
                    shouldRetry = handleHttpWebErrorResponse(sdbAsyncResult, we, out statusCode);
                }

                if (shouldRetry)
                {
                    sdbAsyncResult.RequestState.WebRequest.Abort();
                    sdbAsyncResult.RetriesAttempt++;
                    handleRetry(sdbAsyncResult, statusCode);
                    invoke <T>(sdbAsyncResult);
                }
            }
            catch (Exception e)
            {
                AmazonSimpleDBException exception = e as AmazonSimpleDBException;

                if (null != exception)
                {
                    sdbAsyncResult.FinalSDBResponse = exception;
                    return;
                }
                sdbAsyncResult.RequestState.WebRequest.Abort();
                LOGGER.Error("Error for GetResponse", e);
                sdbAsyncResult.Exception = e;
                shouldRetry = false;
            }
            finally
            {
                if (!shouldRetry)
                {
                    if (null != OnSimpleDBResponse)
                    {
                        OnSimpleDBResponse.Invoke(this, new ResponseEventArgs(sdbAsyncResult.FinalSDBResponse));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /**
         * Look for additional error strings in the response and return formatted exception
         */
        private static AmazonSimpleDBException reportAnyErrors(string responseBody, HttpStatusCode status)
        {
            AmazonSimpleDBException ex = null;

            if (responseBody != null && responseBody.StartsWith("<", StringComparison.OrdinalIgnoreCase))
            {
                Match errorMatcherOne = Regex.Match(
                    responseBody,
                    "<RequestId>(.*)</RequestId>.*<Error><Code>(.*)</Code><Message>(.*)</Message></Error>.*(<Error>)?",
                    RegexOptions.Multiline
                    );
                Match errorMatcherTwo = Regex.Match(
                    responseBody,
                    "<Error><Code>(.*)</Code><Message>(.*)</Message></Error>.*(<Error>)?.*<RequestID>(.*)</RequestID>",
                    RegexOptions.Multiline
                    );
                Match errorMatcherThree = Regex.Match(
                    responseBody,
                    "<Error><Code>(.*)</Code><Message>(.*)</Message><BoxUsage>(.*)</BoxUsage></Error>.*(<Error>)?.*<RequestID>(.*)</RequestID>",
                    RegexOptions.Multiline);

                if (errorMatcherOne.Success)
                {
                    string requestId = errorMatcherOne.Groups[1].Value;
                    string code      = errorMatcherOne.Groups[2].Value;
                    string message   = errorMatcherOne.Groups[3].Value;

                    ex = new AmazonSimpleDBException(message, status, code, "Unknown", null, requestId, responseBody);
                }
                else if (errorMatcherTwo.Success)
                {
                    string code      = errorMatcherTwo.Groups[1].Value;
                    string message   = errorMatcherTwo.Groups[2].Value;
                    string requestId = errorMatcherTwo.Groups[4].Value;

                    ex = new AmazonSimpleDBException(message, status, code, "Unknown", null, requestId, responseBody);
                }
                else if (errorMatcherThree.Success)
                {
                    string code      = errorMatcherThree.Groups[1].Value;
                    string message   = errorMatcherThree.Groups[2].Value;
                    string boxUsage  = errorMatcherThree.Groups[3].Value;
                    string requestId = errorMatcherThree.Groups[5].Value;

                    ex = new AmazonSimpleDBException(message, status, code, "Unknown", boxUsage, requestId, responseBody);
                }
                else
                {
                    ex = new AmazonSimpleDBException("Internal Error", status);
                }
            }
            else
            {
                ex = new AmazonSimpleDBException("Internal Error", status);
            }
            return(ex);
        }
 /// <summary>
 /// Constructs AmazonSimpleDBException with message and wrapped exception
 /// </summary>
 /// <param name="message">Overview of error</param>
 /// <param name="t">Wrapped exception</param>
 public AmazonSimpleDBException(String message, Exception t) : base(message, t)
 {
     this.message = message;
     if (t is AmazonSimpleDBException)
     {
         AmazonSimpleDBException ex = (AmazonSimpleDBException)t;
         this.statusCode = ex.StatusCode;
         this.errorCode  = ex.ErrorCode;
         this.errorType  = ex.ErrorType;
         this.boxUsage   = ex.BoxUsage;
         this.requestId  = ex.RequestId;
         this.xml        = ex.XML;
     }
 }
        public AmazonSimpleDBException(string message, Exception innerException) : base(message, innerException)
        {
            this.message = message;
            AmazonSimpleDBException exception = innerException as AmazonSimpleDBException;

            if (exception != null)
            {
                this.statusCode = exception.StatusCode;
                this.errorCode  = exception.ErrorCode;
                this.errorType  = exception.ErrorType;
                this.boxUsage   = exception.BoxUsage;
                this.requestId  = exception.RequestId;
                this.xml        = exception.XML;
            }
        }
Ejemplo n.º 5
0
        /**
         * Invoke request and return response
         */
        private T Invoke <T>(IDictionary <String, String> parameters)
        {
            String         actionName   = parameters["Action"];
            T              response     = default(T);
            String         responseBody = null;
            HttpStatusCode statusCode   = default(HttpStatusCode);

            /* Add required request parameters */
            AddRequiredParameters(parameters);

            String queryString = GetParametersAsString(parameters);

            byte[] requestData = new UTF8Encoding().GetBytes(queryString);
            bool   shouldRetry = true;
            int    retries     = 0;

            do
            {
                HttpWebRequest request = ConfigureWebRequest(requestData.Length);
                /* Submit the request and read response body */
                try
                {
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(requestData, 0, requestData.Length);
                    }
                    using (HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse)
                    {
                        statusCode = httpResponse.StatusCode;
                        StreamReader reader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8);
                        responseBody = reader.ReadToEnd();
                    }

                    /* Attempt to deserialize response into <Action> Response type */
                    XmlSerializer serlizer = new XmlSerializer(typeof(T));
                    response    = (T)serlizer.Deserialize(new StringReader(responseBody));
                    shouldRetry = false;
                }
                /* Web exception is thrown on unsucessful responses */
                catch (WebException we)
                {
                    shouldRetry = false;
                    using (HttpWebResponse httpErrorResponse = (HttpWebResponse)we.Response as HttpWebResponse)
                    {
                        if (httpErrorResponse == null)
                        {
                            throw new AmazonSimpleDBException(we);
                        }
                        statusCode = httpErrorResponse.StatusCode;
                        StreamReader reader = new StreamReader(httpErrorResponse.GetResponseStream(), Encoding.UTF8);
                        responseBody = reader.ReadToEnd();
                    }

                    if (statusCode == HttpStatusCode.InternalServerError || statusCode == HttpStatusCode.ServiceUnavailable)
                    {
                        shouldRetry = true;
                        PauseOnRetry(++retries, statusCode);
                    }
                    else
                    {
                        /* Attempt to deserialize response into ErrorResponse type */
                        try
                        {
                            XmlSerializer serlizer      = new XmlSerializer(typeof(ErrorResponse));
                            ErrorResponse errorResponse = (ErrorResponse)serlizer.Deserialize(new StringReader(responseBody));
                            Error         error         = errorResponse.Error[0];

                            /* Throw formatted exception with information available from the error response */
                            throw new AmazonSimpleDBException(
                                      error.Message,
                                      statusCode,
                                      error.Code,
                                      error.Type,
                                      null,
                                      errorResponse.RequestId,
                                      errorResponse.ToXML());
                        }
                        /* Rethrow on deserializer error */
                        catch (Exception e)
                        {
                            if (e is AmazonSimpleDBException)
                            {
                                throw e;
                            }
                            else
                            {
                                AmazonSimpleDBException se = ReportAnyErrors(responseBody, statusCode, e);
                                throw se;
                            }
                        }
                    }
                }

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

            return(response);
        }
Ejemplo n.º 6
0
        void SimpleDBErrorResponse(AmazonSimpleDBException error)
        {
            StringBuilder errorBuilder = new StringBuilder();
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "ERROR CODE: {0}", error.ErrorCode));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "INNER EXCEPTION: {0}", error.InnerException));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "MESSAGE: {0}", error.Message));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "REQUEST ID: {0}", error.RequestId));
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "STATUS CODE: {0}", error.StatusCode)); ;
            errorBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "\nERROR RESPONSE:\n {0}", error.XML));

            this.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(errorBuilder.ToString(), "Error Occured", MessageBoxButton.OK);
            });
        }