diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorException.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorException.java index 3da73d6d2e..cbb1120e95 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorException.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/VCloudDirectorException.java @@ -21,6 +21,12 @@ package org.jclouds.vcloud.director.v1_5; import org.jclouds.vcloud.director.v1_5.domain.Error; import org.jclouds.vcloud.director.v1_5.domain.Task; +import com.google.common.base.CaseFormat; +import com.google.common.base.Function; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; + /** * @author grkvlt@apache.org */ @@ -29,19 +35,19 @@ public class VCloudDirectorException extends RuntimeException { /** The serialVersionUID. */ private static final long serialVersionUID = -5292516858598372960L; - private static final String MSG_FMT = "%s: %s"; + private static final String MSG_FMT = "%s (%d) %s: %s"; private final Error error; private final Task task; public VCloudDirectorException(Error error) { - super(String.format(MSG_FMT, "Error", error.getMessage())); + super(message(error, "Error")); this.error = error; this.task = null; } public VCloudDirectorException(Task task) { - super(String.format(MSG_FMT, "Task error", task.getError().getMessage())); + super(message(task.getError(), "Task error")); this.error = task.getError(); this.task = task; } @@ -65,4 +71,14 @@ public class VCloudDirectorException extends RuntimeException { public Task getTask() { return task; } + + private static String message(Error error, String from) { + Iterable words = Iterables.transform(Splitter.on('_').split(error.getMinorErrorCode()), new Function() { + @Override + public String apply(String input) { + return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, input); + } + }); + return String.format(MSG_FMT, Joiner.on(' ').join(words), error.getMajorErrorCode(), from, error.getMessage()); + } } diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Error.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Error.java index 4f01522555..d76973c9e9 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Error.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Error.java @@ -203,7 +203,7 @@ public class Error { /** * Specific API error code. - *

+ * * For example - can indicate that vApp power on failed by some reason. */ public String getMinorErrorCode() { diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java index 1507376f8d..c52ca75974 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/functions/ThrowVCloudErrorOn4xx.java @@ -26,6 +26,7 @@ import javax.xml.bind.JAXB; import org.jclouds.http.HttpResponseException; import org.jclouds.io.InputSuppliers; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.domain.Error; @@ -49,6 +50,9 @@ public class ThrowVCloudErrorOn4xx implements Function { if (exception != null && exception.getResponse() != null && exception.getResponse().getStatusCode() >= 400 && exception.getResponse().getStatusCode() < 500) { try { Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class); + if (exception.getResponse().getStatusCode() == 403 && error.getMinorErrorCode().equals("ACCESS_TO_RESOURCE_IS_FORBIDDEN")) { + throw new ResourceNotFoundException(error.getMessage()); + } throw new VCloudDirectorException(error); } catch (IOException e) { Throwables.propagate(e); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java index c9f1324876..35e62a4118 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/MediaClientExpectTest.java @@ -23,6 +23,7 @@ import static org.testng.Assert.fail; import java.net.URI; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; @@ -165,19 +166,15 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes .xmlFilePayload("/media/error403-catalog.xml", VCloudDirectorMediaType.ERROR) .httpResponseBuilder().statusCode(403).build()); - Error expected = Error.builder() - .message("No access to entity \"(com.vmware.vcloud.entity.media:e9cd3387-ac57-4d27-a481-9bee75e0690f)\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"(com.vmware.vcloud.entity.media:e9cd3387-ac57-4d27-a481-9bee75e0690f)\"."; try { client.getMediaClient().getMedia(mediaUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } @@ -194,19 +191,15 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes .xmlFilePayload("/media/error403-fake.xml", VCloudDirectorMediaType.ERROR) .httpResponseBuilder().statusCode(403).build()); - Error expected = Error.builder() - .message("No access to entity \"(com.vmware.vcloud.entity.media:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"(com.vmware.vcloud.entity.media:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)\"."; try { client.getMediaClient().getMedia(mediaUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java index b0f7056439..1a6d507036 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java @@ -23,6 +23,7 @@ import static org.testng.Assert.fail; import java.net.URI; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; @@ -113,19 +114,15 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT .xmlFilePayload("/network/error403-catalog.xml", VCloudDirectorMediaType.ERROR) .httpResponseBuilder().statusCode(403).build()); - Error expected = Error.builder() - .message("This operation is denied.") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "This operation is denied."; try { client.getNetworkClient().getNetwork(networkUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } @@ -142,19 +139,15 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT .xmlFilePayload("/network/error403-fake.xml", VCloudDirectorMediaType.ERROR) .httpResponseBuilder().statusCode(403).build()); - Error expected = Error.builder() - .message("This operation is denied.") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "This operation is denied."; try { client.getNetworkClient().getNetwork(networkUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/OrgClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/OrgClientExpectTest.java index 4dfa409e9b..d982e7c1ea 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/OrgClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/OrgClientExpectTest.java @@ -23,6 +23,7 @@ import static org.testng.Assert.fail; import java.net.URI; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; @@ -127,19 +128,15 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest getStandardRequest("GET", "/org/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"), getStandardPayloadResponse(403, "/org/error403-catalog.xml", VCloudDirectorMediaType.ERROR)); - Error expected = Error.builder() - .message("No access to entity \"com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c\"."; try { client.getOrgClient().getOrg(orgUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } @@ -151,19 +148,15 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest getStandardRequest("GET", "/org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), getStandardPayloadResponse(403, "/org/error403-fake.xml", VCloudDirectorMediaType.ERROR)); - Error expected = Error.builder() - .message("No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\"."; try { client.getOrgClient().getOrg(orgUri); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); + fail("Should have thrown a ResourceNotFoundException"); } } diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/TaskClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/TaskClientExpectTest.java index 327d588f39..34db40eda3 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/TaskClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/TaskClientExpectTest.java @@ -25,6 +25,7 @@ import java.net.URI; import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; @@ -162,20 +163,16 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse, orgRequest, orgResponse); - Error expected = Error.builder() - .message("No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\"."; try { client.getTaskClient().getTaskList(URI.create("https://vcloudbeta.bluelock.com/api/org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); - } catch (Exception e) { - fail("Should have thrown a VCloudDirectorException"); - } + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); + } catch (Exception e) { + fail("Should have thrown a ResourceNotFoundException"); + } } @Test diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientExpectTest.java index 8c826f18a1..7106a32fe0 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VAppTemplateClientExpectTest.java @@ -38,6 +38,7 @@ import java.text.SimpleDateFormat; import java.util.Locale; import java.util.TimeZone; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.domain.CustomizationSection; import org.jclouds.vcloud.director.v1_5.domain.FirewallRule; @@ -119,7 +120,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx client.getVAppTemplate(uri); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorEditVAppTemplate() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -157,7 +158,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx assertNotNull(task); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testConsolidateMissingVAppTemplate() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -208,7 +209,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx assertNotNull(task); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testEnableDownloadMissingVAppTemplate() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -274,7 +275,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx assertNotNull(task); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorGetCustomizationSection() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -286,7 +287,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx client.getVAppTemplateCustomizationSection(uri); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorEditCustomizationSection() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -362,7 +363,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx assertNotNull(task); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorGetLeaseSettingsSection() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -374,7 +375,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx client.getVappTemplateLeaseSettingsSection(uri); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorEditLeaseSettingsSection() throws ParseException { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -455,7 +456,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx assertNotNull(task); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testErrorGetMetadataValue() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); @@ -479,7 +480,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx client.getMetadataClient().setMetadata(uri, "12345", exampleMetadataValue()); } - @Test(expectedExceptions = VCloudDirectorException.class) + @Test(expectedExceptions = ResourceNotFoundException.class) public void testDeleteMissingMetadataValue() { final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9"; URI uri = URI.create(endpoint + templateId); diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java index 90240a667f..bcca92a047 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/VdcClientExpectTest.java @@ -23,6 +23,7 @@ import static org.testng.Assert.fail; import java.net.URI; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; @@ -119,17 +120,15 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest .xmlFilePayload("/vdc/error403-fake.xml", VCloudDirectorMediaType.ERROR) .httpResponseBuilder().statusCode(403).build()); - Error expected = Error.builder() - .message("No access to entity \"com.vmware.vcloud.entity.vdc:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".") - .majorErrorCode(403) - .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") - .build(); + String message = "No access to entity \"com.vmware.vcloud.entity.vdc:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\"."; try { client.getVdcClient().getVdc(URI.create(endpoint + "/vdc/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")); fail("Should give HTTP 403 error"); - } catch (VCloudDirectorException vde) { - assertEquals(vde.getError(), expected); + } catch (ResourceNotFoundException rnfe) { + assertEquals(rnfe.getMessage(), message); + } catch (Exception e) { + fail("Should have thrown a ResourceNotFoundException"); } }