fixed joyent error handler

This commit is contained in:
Adrian Cole 2012-07-24 23:34:51 -07:00
parent 01918a02ec
commit 8179713655
2 changed files with 30 additions and 17 deletions

View File

@ -35,7 +35,6 @@ import org.jclouds.rest.ResourceNotFoundException;
* @author Adrian Cole * @author Adrian Cole
* *
*/ */
// TODO: is there error spec someplace? let's type errors, etc.
@Singleton @Singleton
public class JoyentCloudErrorHandler implements HttpErrorHandler { public class JoyentCloudErrorHandler implements HttpErrorHandler {
public void handleError(HttpCommand command, HttpResponse response) { public void handleError(HttpCommand command, HttpResponse response) {
@ -44,21 +43,24 @@ public class JoyentCloudErrorHandler implements HttpErrorHandler {
String message = data != null ? new String(data) : null; String message = data != null ? new String(data) : null;
Exception exception = message != null ? new HttpResponseException(command, response, message) 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(), message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
response.getStatusLine()); response.getStatusLine());
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 400: case 400:
break; break;
case 401: case 401:
case 403: case 403:
exception = new AuthorizationException(message, exception); exception = new AuthorizationException(message, exception);
break; break;
case 404: case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) { if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception); exception = new ResourceNotFoundException(message, exception);
} }
break; break;
case 409:
exception = new IllegalStateException(message, exception);
break;
} }
command.setException(exception); command.setException(exception);
} }

View File

@ -18,7 +18,7 @@
*/ */
package org.jclouds.joyent.cloudapi.v6_5.handlers; 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.expect;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reportMatcher; import static org.easymock.EasyMock.reportMatcher;
@ -38,10 +38,21 @@ import org.testng.annotations.Test;
*/ */
@Test(groups = "unit", testName = "JoyentCloudErrorHandlerTest") @Test(groups = "unit", testName = "JoyentCloudErrorHandlerTest")
public class 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, private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
Class<? extends Exception> expected) { Class<? extends Exception> 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, private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
@ -49,7 +60,7 @@ public class JoyentCloudErrorHandlerTest {
JoyentCloudErrorHandler function = new JoyentCloudErrorHandler(); JoyentCloudErrorHandler function = new JoyentCloudErrorHandler();
HttpCommand command = createMockBuilder(HttpCommand.class).createMock(); HttpCommand command = createMock(HttpCommand.class);
HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build(); HttpRequest request = HttpRequest.builder().method(method).endpoint(uri).build();
HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build(); HttpResponse response = HttpResponse.builder().statusCode(statusCode).message(message).payload(content).build();
response.getPayload().getContentMetadata().setContentType(contentType); response.getPayload().getContentMetadata().setContentType(contentType);