Exemplo n.º 1
0
        private ClientException ParseError(IRestResponse response)
        {
            if (response == null)
            {
                return(new ConnectionException());
            }
            if (HttpStatusCode.Redirect.Equals(response.StatusCode) || HttpStatusCode.TemporaryRedirect.Equals(response.StatusCode) || HttpStatusCode.MovedPermanently.Equals(response.StatusCode))
            {
                return(new RedirectionException());
            }

            if (string.IsNullOrWhiteSpace(response.Content))
            {
                if (HttpStatusCode.Forbidden.Equals(response.StatusCode) || HttpStatusCode.NotFound.Equals(response.StatusCode) ||
                    HttpStatusCode.MethodNotAllowed.Equals(response.StatusCode) || HttpStatusCode.NotImplemented.Equals(response.StatusCode))
                {
                    ClientException e             = null;
                    ErrorResponse   errorResponse = new ErrorResponse();

                    foreach (Parameter parameter in response.Headers)
                    {
                        if (parameter.Name.Equals("x-amz-id-2", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.HostID = parameter.Value.ToString();
                        }
                        if (parameter.Name.Equals("x-amz-request-id", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.RequestID = parameter.Value.ToString();
                        }
                    }

                    errorResponse.Resource = response.Request.Resource;

                    if (HttpStatusCode.NotFound.Equals(response.StatusCode))
                    {
                        int pathLength = response.Request.Resource.Split('/').Count();
                        if (pathLength > 1)
                        {
                            errorResponse.Code = "NoSuchKey";
                            e = new ObjectNotFoundException();
                        }
                        else if (pathLength == 1)
                        {
                            errorResponse.Code = "NoSuchBucket";
                            e = new BucketNotFoundException();
                        }
                        else
                        {
                            e = new InternalClientException("404 without body resulted in path with less than two components");
                        }
                    }
                    else if (HttpStatusCode.Forbidden.Equals(response.StatusCode))
                    {
                        errorResponse.Code = "Forbidden";
                        e = new AccessDeniedException();
                    }
                    else if (HttpStatusCode.MethodNotAllowed.Equals(response.StatusCode))
                    {
                        errorResponse.Code = "MethodNotAllowed";
                        e = new MethodNotAllowedException();
                    }
                    else
                    {
                        errorResponse.Code = "MethodNotAllowed";
                        e = new MethodNotAllowedException();
                    }
                    e.Response = errorResponse;
                    return(e);
                }
                throw new InternalClientException("Unsuccessful response from server without XML error: " + response.StatusCode);
            }

            var           contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
            var           stream       = new MemoryStream(contentBytes);
            ErrorResponse errResponse  = (ErrorResponse)(new XmlSerializer(typeof(ErrorResponse)).Deserialize(stream));
            string        code         = errResponse.Code;

            ClientException clientException;

            if ("NoSuchBucket".Equals(code))
            {
                clientException = new BucketNotFoundException();
            }
            else if ("NoSuchKey".Equals(code))
            {
                clientException = new ObjectNotFoundException();
            }
            else if ("InvalidBucketName".Equals(code))
            {
                clientException = new InvalidKeyNameException();
            }
            else if ("InvalidObjectName".Equals(code))
            {
                clientException = new InvalidKeyNameException();
            }
            else if ("AccessDenied".Equals(code))
            {
                clientException = new AccessDeniedException();
            }
            else if ("InvalidAccessKeyId".Equals(code))
            {
                clientException = new AccessDeniedException();
            }
            else if ("BucketAlreadyExists".Equals(code))
            {
                clientException = new BucketExistsException();
            }
            else if ("ObjectAlreadyExists".Equals(code))
            {
                clientException = new ObjectExistsException();
            }
            else if ("InternalError".Equals(code))
            {
                clientException = new InternalServerException();
            }
            else if ("KeyTooLong".Equals(code))
            {
                clientException = new InvalidKeyNameException();
            }
            else if ("TooManyBuckets".Equals(code))
            {
                clientException = new MaxBucketsReachedException();
            }
            else if ("PermanentRedirect".Equals(code))
            {
                clientException = new RedirectionException();
            }
            else if ("MethodNotAllowed".Equals(code))
            {
                clientException = new ObjectExistsException();
            }
            else if ("BucketAlreadyOwnedByYou".Equals(code))
            {
                clientException = new BucketExistsException();
            }
            else
            {
                clientException = new InternalClientException(errResponse.ToString());
            }


            clientException.Response = errResponse;
            clientException.XmlError = response.Content;
            return(clientException);
        }
Exemplo n.º 2
0
        private ClientException ParseError(IRestResponse response)
        {
            if (response == null)
            {
                return new ConnectionException();
            }
            if (HttpStatusCode.Redirect.Equals(response.StatusCode) || HttpStatusCode.TemporaryRedirect.Equals(response.StatusCode) || HttpStatusCode.MovedPermanently.Equals(response.StatusCode))
            {
                return new RedirectionException();
            }

            if (string.IsNullOrWhiteSpace(response.Content))
            {
                if (HttpStatusCode.Forbidden.Equals(response.StatusCode) || HttpStatusCode.NotFound.Equals(response.StatusCode) ||
                    HttpStatusCode.MethodNotAllowed.Equals(response.StatusCode) || HttpStatusCode.NotImplemented.Equals(response.StatusCode))
                {
                    ClientException e = null;
                    ErrorResponse errorResponse = new ErrorResponse();

                    foreach (Parameter parameter in response.Headers)
                    {
                        if (parameter.Name.Equals("x-amz-id-2", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.XAmzID2 = parameter.Value.ToString();
                        }
                        if (parameter.Name.Equals("x-amz-request-id", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.RequestID = parameter.Value.ToString();
                        }
                    }

                    errorResponse.Resource = response.Request.Resource;

                    if (HttpStatusCode.NotFound.Equals(response.StatusCode))
                    {
                        int pathLength = response.Request.Resource.Split('/').Count();
                        if (pathLength > 1)
                        {
                            errorResponse.Code = "NoSuchKey";
                            e = new ObjectNotFoundException();
                        }
                        else if (pathLength == 1)
                        {
                            errorResponse.Code = "NoSuchBucket";
                            e = new BucketNotFoundException();
                        }
                        else
                        {
                            e = new InternalClientException("404 without body resulted in path with less than two components");
                        }
                    }
                    else if (HttpStatusCode.Forbidden.Equals(response.StatusCode))
                    {
                        errorResponse.Code = "Forbidden";
                        e = new AccessDeniedException();
                    }
                    else if (HttpStatusCode.MethodNotAllowed.Equals(response.StatusCode))
                    {
                        errorResponse.Code = "MethodNotAllowed";
                        e = new MethodNotAllowedException();
                    }
                    else
                    {
                        errorResponse.Code = "MethodNotAllowed";
                        e = new MethodNotAllowedException();
                    }
                    e.Response = errorResponse;
                    return e;
                }
                throw new InternalClientException("Unsuccessful response from server without XML error: " + response.StatusCode);
            }

            var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
            var stream = new MemoryStream(contentBytes);
            ErrorResponse errResponse = (ErrorResponse)(new XmlSerializer(typeof(ErrorResponse)).Deserialize(stream));
            string code = errResponse.Code;

            ClientException clientException;

            if ("NoSuchBucket".Equals(code)) clientException = new BucketNotFoundException();
            else if ("NoSuchKey".Equals(code)) clientException = new ObjectNotFoundException();
            else if ("InvalidBucketName".Equals(code)) clientException = new InvalidKeyNameException();
            else if ("InvalidObjectName".Equals(code)) clientException = new InvalidKeyNameException();
            else if ("AccessDenied".Equals(code)) clientException = new AccessDeniedException();
            else if ("InvalidAccessKeyId".Equals(code)) clientException = new AccessDeniedException();
            else if ("BucketAlreadyExists".Equals(code)) clientException = new BucketExistsException();
            else if ("ObjectAlreadyExists".Equals(code)) clientException = new ObjectExistsException();
            else if ("InternalError".Equals(code)) clientException = new InternalServerException();
            else if ("KeyTooLong".Equals(code)) clientException = new InvalidKeyNameException();
            else if ("TooManyBuckets".Equals(code)) clientException = new MaxBucketsReachedException();
            else if ("PermanentRedirect".Equals(code)) clientException = new RedirectionException();
            else if ("MethodNotAllowed".Equals(code)) clientException = new ObjectExistsException();
            else if ("BucketAlreadyOwnedByYou".Equals(code)) clientException = new BucketExistsException();
            else clientException = new InternalClientException(errResponse.ToString());

            clientException.Response = errResponse;
            clientException.XmlError = response.Content;
            return clientException;
        }