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("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; }