private HttpResponseMessage HandleBucketNotFoundEx(BucketNotFoundException ex) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("Bucket not found!"), ReasonPhrase = "bucket_not_found" }; return(resp); }
public void When_InstantiatingBucketNotFoundExceptionWithBucketId_Expect_BucketIdInMessage() { // Arrange int bucketId = new Random().Next(int.MaxValue); BucketNotFoundException exception; // Act exception = new BucketNotFoundException(bucketId); // Assert Assert.Equal($"Bucket '{bucketId}' not found.", exception.Message); }
/// <summary> /// Tests the object's existence and returns metadata about existing objects. /// </summary> /// <param name="bucket">Bucket to test object in</param> /// <param name="key">Key of object to stat</param> /// <returns>Facts about the object</returns> public ObjectStat StatObject(string bucket, string key) { var request = new RestRequest(bucket + "/" + UrlEncode(key), Method.HEAD); var response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { long size = 0; DateTime lastModified = new DateTime(); string etag = ""; string contentType = null; foreach (Parameter parameter in response.Headers) { if (parameter.Name == "Content-Length") { size = long.Parse(parameter.Value.ToString()); } if (parameter.Name == "Last-Modified") { DateTime.Parse(parameter.Value.ToString()); } if (parameter.Name == "ETag") { etag = parameter.Value.ToString().Replace("\"", ""); } if (parameter.Name == "Content-Type") { contentType = parameter.Value.ToString(); } } return(new ObjectStat(key, size, lastModified, etag, contentType)); } ClientException ex = ParseError(response); if (ex.GetType() == typeof(ObjectNotFoundException)) { if (!this.BucketExists(bucket)) { var bnfe = new BucketNotFoundException(); bnfe.Response = ex.Response; throw bnfe; } } throw ex; }
public void When_DeserializingBucketNotFoundException_Expect_SerializedBucketNotFoundException() { // Arrange BucketNotFoundException exception = new BucketNotFoundException(new Random().Next(int.MaxValue)); BucketNotFoundException result; // Act using (Stream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, exception); stream.Position = 0; result = (BucketNotFoundException)formatter.Deserialize(stream); } // Assert Assert.Equal(exception.Message, result.Message); Assert.Equal(exception.InnerException, result.InnerException); Assert.Null(result.InnerException); }
public override void OnException(HttpActionExecutedContext actionExecutedContext) { Exception ex = actionExecutedContext.Exception; BucketNotFoundException bEx = ex as BucketNotFoundException; if (bEx != null) { actionExecutedContext.Response = HandleBucketNotFoundEx(bEx); return; } DocumentNotFoundException dEx = ex as DocumentNotFoundException; if (dEx != null) { actionExecutedContext.Response = HandleDocNotFoundEx(dEx); return; } InvalidVersionFormatException ivEx = ex as InvalidVersionFormatException; if (ivEx != null) { actionExecutedContext.Response = HandleInvalidVersionEx(ivEx); return; } ConflictException cfEx = ex as ConflictException; if (cfEx != null) { actionExecutedContext.Response = HandleConflictEx(cfEx); return; } actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("An error occurred, please try again or contact the administrator."), ReasonPhrase = "internal_error" }; }
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; }
/// <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; }
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); }
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; }
/// <summary> /// Tests the object's existence and returns metadata about existing objects. /// </summary> /// <param name="bucket">Bucket to test object in</param> /// <param name="key">Key of object to stat</param> /// <returns>Facts about the object</returns> public ObjectStat StatObject(string bucket, string key) { var request = new RestRequest(bucket + "/" + UrlEncode(key), Method.HEAD); var response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { long size = 0; DateTime lastModified = new DateTime(); string etag = ""; string contentType = null; foreach (Parameter parameter in response.Headers) { if (parameter.Name == "Content-Length") { size = long.Parse(parameter.Value.ToString()); } if (parameter.Name == "Last-Modified") { DateTime.Parse(parameter.Value.ToString()); } if (parameter.Name == "ETag") { etag = parameter.Value.ToString().Replace("\"", ""); } if (parameter.Name == "Content-Type") { contentType = parameter.Value.ToString(); } } return new ObjectStat(key, size, lastModified, etag, contentType); } ClientException ex = ParseError(response); if (ex.GetType() == typeof(ObjectNotFoundException)) { if (!this.BucketExists(bucket)) { var bnfe = new BucketNotFoundException(); bnfe.Response = ex.Response; throw new BucketNotFoundException(); } } throw ex; }
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); }