From 8179713655c6cc34127c85dbc986e9cd69b5b63d Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 24 Jul 2012 23:34:51 -0700 Subject: [PATCH] fixed joyent error handler --- .../handlers/JoyentCloudErrorHandler.java | 30 ++++++++++--------- .../handlers/JoyentCloudErrorHandlerTest.java | 17 +++++++++-- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandler.java b/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandler.java index 1104ee19fe..dee1693a77 100644 --- a/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandler.java +++ b/labs/joyent-cloudapi/src/main/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandler.java @@ -35,7 +35,6 @@ import org.jclouds.rest.ResourceNotFoundException; * @author Adrian Cole * */ -// TODO: is there error spec someplace? let's type errors, etc. @Singleton public class JoyentCloudErrorHandler implements HttpErrorHandler { public void handleError(HttpCommand command, HttpResponse response) { @@ -44,21 +43,24 @@ public class JoyentCloudErrorHandler implements HttpErrorHandler { String message = data != null ? new String(data) : null; Exception exception = message != null ? new HttpResponseException(command, response, message) - : new HttpResponseException(command, response); + : new HttpResponseException(command, response); message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), - response.getStatusLine()); + response.getStatusLine()); switch (response.getStatusCode()) { - case 400: - break; - case 401: - case 403: - exception = new AuthorizationException(message, exception); - break; - case 404: - if (!command.getCurrentRequest().getMethod().equals("DELETE")) { - exception = new ResourceNotFoundException(message, exception); - } - break; + case 400: + break; + case 401: + case 403: + exception = new AuthorizationException(message, exception); + break; + case 404: + if (!command.getCurrentRequest().getMethod().equals("DELETE")) { + exception = new ResourceNotFoundException(message, exception); + } + break; + case 409: + exception = new IllegalStateException(message, exception); + break; } command.setException(exception); } diff --git a/labs/joyent-cloudapi/src/test/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandlerTest.java b/labs/joyent-cloudapi/src/test/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandlerTest.java index f18e0c8164..b67360f6e4 100644 --- a/labs/joyent-cloudapi/src/test/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandlerTest.java +++ b/labs/joyent-cloudapi/src/test/java/org/jclouds/joyent/cloudapi/v6_5/handlers/JoyentCloudErrorHandlerTest.java @@ -18,7 +18,7 @@ */ package org.jclouds.joyent.cloudapi.v6_5.handlers; -import static org.easymock.EasyMock.createMockBuilder; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reportMatcher; @@ -38,10 +38,21 @@ import org.testng.annotations.Test; */ @Test(groups = "unit", testName = "JoyentCloudErrorHandlerTest") public class JoyentCloudErrorHandlerTest { + + @Test + public void test409MakesIllegalStateException() { + assertCodeMakes( + "POST", + URI.create("https://us-east-1.api.joyentcloud.com/my/machines/b7d07c64-ba40-496a-a19a-3f1d028494ff"), + 409, + "HTTP/1.1 409 Conflict", + "\"{\"code\":\"InvalidState\",\"message\":\"An incompatible transition has already been queued for this resource\"}\"", + IllegalStateException.class); + } private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content, Class expected) { - assertCodeMakes(method, uri, statusCode, message, "text/plain", content, expected); + assertCodeMakes(method, uri, statusCode, message, "application/json", content, expected); } private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType, @@ -49,7 +60,7 @@ public class JoyentCloudErrorHandlerTest { JoyentCloudErrorHandler function = new JoyentCloudErrorHandler(); - HttpCommand command = createMockBuilder(HttpCommand.class).createMock(); + HttpCommand command = createMock(HttpCommand.class); HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build(); HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build(); response.getPayload().getContentMetadata().setContentType(contentType);