diff --git a/apis/sts/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java b/apis/sts/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java index 53b3e96792..d71ea176dd 100644 --- a/apis/sts/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java +++ b/apis/sts/src/main/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContent.java @@ -35,6 +35,7 @@ import org.jclouds.http.HttpResponseException; import org.jclouds.logging.Logger; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.InsufficientResourcesException; +import org.jclouds.rest.ResourceAlreadyExistsException; import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.util.Strings2; @@ -128,7 +129,12 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler { } break; case 409: - exception = new IllegalStateException(message, exception); + if ("BucketAlreadyExists".equals(errorCode)) { + exception = new ResourceAlreadyExistsException(exception); + } else { + exception = new IllegalStateException(message, exception); + } + break; } return exception; } diff --git a/apis/sts/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java b/apis/sts/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java index bf6d4a5067..b74ed73ba0 100644 --- a/apis/sts/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java +++ b/apis/sts/src/test/java/org/jclouds/aws/handlers/ParseAWSErrorFromXmlContentTest.java @@ -38,6 +38,7 @@ import org.jclouds.http.functions.config.SaxParserModule; import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.InsufficientResourcesException; import org.jclouds.rest.RequestSigner; +import org.jclouds.rest.ResourceAlreadyExistsException; import org.jclouds.rest.ResourceNotFoundException; import org.testng.annotations.Test; @@ -110,6 +111,22 @@ public class ParseAWSErrorFromXmlContentTest { InsufficientResourcesException.class); } + @Test + public void test409WithBucketAlreadyExistsMakesResourceAlreadyExistsException() { + assertCodeMakes( + POST, + URI.create("https://ec2.us-east-1.amazonaws.com/"), + CONFLICT.getStatusCode(), + "", + "" + + "BucketAlreadyExists" + + "The requested bucket name is not available." + + " The bucket namespace is shared by all users of the system." + + " Please select a different name and try again." + + "c14f531a-cc35-4b48-8149-2655c7e6dc76", + ResourceAlreadyExistsException.class); + } + @Test public void test400WithInUseCodeSetsIllegalStateException() { assertCodeMakes(GET, URI.create("https://amazonaws.com/foo"), BAD_REQUEST.getStatusCode(), "", diff --git a/core/src/main/java/org/jclouds/rest/ResourceAlreadyExistsException.java b/core/src/main/java/org/jclouds/rest/ResourceAlreadyExistsException.java new file mode 100644 index 0000000000..e096a5c9e7 --- /dev/null +++ b/core/src/main/java/org/jclouds/rest/ResourceAlreadyExistsException.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jclouds.rest; + +/** + * Thrown when creating a resource that already exists. + * + * @author Andrew Gaul + */ +public class ResourceAlreadyExistsException extends RuntimeException { + public ResourceAlreadyExistsException() { + this(null, null); + } + + public ResourceAlreadyExistsException(String message) { + this(message, null); + } + + public ResourceAlreadyExistsException(Throwable cause) { + this(null, cause); + } + + public ResourceAlreadyExistsException(String message, Throwable cause) { + super(message, cause); + } +}