예제 #1
0
        private static void ParseWellKnownErrorNoContent(IRestResponse response)
        {
            MinioException error         = 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();
                }

                if (parameter.Name.Equals("x-amz-bucket-region", StringComparison.CurrentCultureIgnoreCase))
                {
                    errorResponse.BucketRegion = parameter.Value.ToString();
                }
            }

            errorResponse.Resource = response.Request.Resource;

            // zero, one or two segments
            var resourceSplits = response.Request.Resource.Split(new[] { '/' }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (HttpStatusCode.NotFound.Equals(response.StatusCode))
            {
                int  pathLength = resourceSplits.Length;
                bool isAWS      = response.ResponseUri.Host.EndsWith("s3.amazonaws.com");
                bool isVirtual  = isAWS && !response.ResponseUri.Host.StartsWith("s3.amazonaws.com");

                if (pathLength > 1)
                {
                    var objectName = resourceSplits[1];
                    errorResponse.Code = "NoSuchKey";
                    error = new ObjectNotFoundException(objectName, "Not found.");
                }
                else if (pathLength == 1)
                {
                    var resource = resourceSplits[0];

                    if (isAWS && isVirtual && response.Request.Resource != string.Empty)
                    {
                        errorResponse.Code = "NoSuchKey";
                        error = new ObjectNotFoundException(resource, "Not found.");
                    }
                    else
                    {
                        errorResponse.Code = "NoSuchBucket";
                        BucketRegionCache.Instance.Remove(resource);
                        error = new BucketNotFoundException(resource, "Not found.");
                    }
                }
                else
                {
                    error = new InternalClientException("404 without body resulted in path with less than two components", response);
                }
            }
            else if (HttpStatusCode.BadRequest.Equals(response.StatusCode))
            {
                int pathLength = resourceSplits.Length;

                if (pathLength > 1)
                {
                    var objectName = resourceSplits[1];
                    errorResponse.Code = "InvalidObjectName";
                    error = new InvalidObjectNameException(objectName, "Invalid object name.");
                }
                else
                {
                    error = new InternalClientException("400 without body resulted in path with less than two components", response);
                }
            }
            else if (HttpStatusCode.Forbidden.Equals(response.StatusCode))
            {
                errorResponse.Code = "Forbidden";
                error = new AccessDeniedException("Access denied on the resource: " + response.Request.Resource);
            }

            error.Response = errorResponse;
            throw error;
        }
예제 #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.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);
        }
예제 #3
0
        /// <summary>
        /// Parse response errors if any and return relevant error messages
        /// </summary>
        /// <param name="response"></param>
        internal static void ParseError(IRestResponse response)
        {
            if (response == null)
            {
                throw new ConnectionException("Response is nil. Please report this issue https://github.com/minio/minio-dotnet/issues");
            }
            if (HttpStatusCode.Redirect.Equals(response.StatusCode) || HttpStatusCode.TemporaryRedirect.Equals(response.StatusCode) || HttpStatusCode.MovedPermanently.Equals(response.StatusCode))
            {
                throw new RedirectionException("Redirection detected. Please report this issue https://github.com/minio/minio-dotnet/issues");
            }

            if (string.IsNullOrWhiteSpace(response.Content))
            {
                ErrorResponse errorResponse = new ErrorResponse();

                if (HttpStatusCode.Forbidden.Equals(response.StatusCode) || HttpStatusCode.NotFound.Equals(response.StatusCode) ||
                    HttpStatusCode.MethodNotAllowed.Equals(response.StatusCode) || HttpStatusCode.NotImplemented.Equals(response.StatusCode))
                {
                    MinioException e = null;

                    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();
                        }
                        if (parameter.Name.Equals("x-amz-bucket-region", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.BucketRegion = parameter.Value.ToString();
                        }
                    }

                    errorResponse.Resource = response.Request.Resource;

                    if (HttpStatusCode.NotFound.Equals(response.StatusCode))
                    {
                        int  pathLength = response.Request.Resource.Split('/').Count();
                        bool isAWS      = response.ResponseUri.Host.EndsWith("s3.amazonaws.com");
                        bool isVirtual  = isAWS && !(response.ResponseUri.Host.StartsWith("s3.amazonaws.com"));

                        if (pathLength > 1)
                        {
                            errorResponse.Code = "NoSuchKey";
                            var bucketName = response.Request.Resource.Split('/')[0];
                            var objectName = response.Request.Resource.Split('/')[1];
                            if (objectName == "")
                            {
                                e = new BucketNotFoundException(bucketName, "Not found.");
                            }
                            else
                            {
                                e = new ObjectNotFoundException(objectName, "Not found.");
                            }
                        }
                        else if (pathLength == 1)
                        {
                            var resource = response.Request.Resource.Split('/')[0];

                            if (isAWS && isVirtual && response.Request.Resource != "")
                            {
                                errorResponse.Code = "NoSuchKey";
                                e = new ObjectNotFoundException(resource, "Not found.");
                            }
                            else
                            {
                                errorResponse.Code = "NoSuchBucket";
                                BucketRegionCache.Instance.Remove(resource);
                                e = new BucketNotFoundException(resource, "Not found.");
                            }
                        }
                        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("Access denied on the resource: " + response.Request.Resource);
                    }
                    e.Response = errorResponse;
                    throw e;
                }
                throw new InternalClientException("Unsuccessful response from server without XML error: " + response.ErrorMessage);
            }

            if (response.StatusCode.Equals(HttpStatusCode.NotFound) && response.Request.Resource.EndsWith("?location") &&
                response.Request.Method.Equals(Method.GET))
            {
                var bucketName = response.Request.Resource.Split('?')[0];
                BucketRegionCache.Instance.Remove(bucketName);
                throw new BucketNotFoundException(bucketName, "Not found.");
            }

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

            // Handle XML response for Bucket Policy not found case
            if (response.StatusCode.Equals(HttpStatusCode.NotFound) && response.Request.Resource.EndsWith("?policy") &&
                response.Request.Method.Equals(Method.GET) && (errResponse.Code.Equals("NoSuchBucketPolicy")))
            {
                ErrorResponseException ErrorException = new ErrorResponseException(errResponse.Message, errResponse.Code);
                ErrorException.Response = errResponse;
                ErrorException.XmlError = response.Content;
                throw ErrorException;
            }

            MinioException MinioException = new MinioException(errResponse.Message);

            MinioException.Response = errResponse;
            MinioException.XmlError = response.Content;
            throw MinioException;
        }
예제 #4
0
        private ClientException ParseError(IRestResponse response)
        {
            if (response == null)
            {
                return(new ConnectionException("Response is nil. Please report this issue https://github.com/minio/minio-dotnet/issues"));
            }
            if (HttpStatusCode.Redirect.Equals(response.StatusCode) || HttpStatusCode.TemporaryRedirect.Equals(response.StatusCode) || HttpStatusCode.MovedPermanently.Equals(response.StatusCode))
            {
                return(new RedirectionException("Redirection detected. Please report this issue https://github.com/minio/minio-dotnet/issues"));
            }

            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();
                        }
                        if (parameter.Name.Equals("x-amz-bucket-region", StringComparison.CurrentCultureIgnoreCase))
                        {
                            errorResponse.BucketRegion = 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";
                            var objectName = response.Request.Resource.Split('/')[1];
                            e = new ObjectNotFoundException(objectName, "Not found.");
                        }
                        else if (pathLength == 1)
                        {
                            errorResponse.Code = "NoSuchBucket";
                            var bucketName = response.Request.Resource.Split('/')[0];
                            e = new BucketNotFoundException(bucketName, "Not found.");
                        }
                        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("Access denied on the resource: " + response.Request.Resource);
                    }
                    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));

            ClientException clientException = new ClientException(errResponse.Message);

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