it is possible for an ultradns error to not include a message

This commit is contained in:
adriancole 2013-03-24 22:35:28 -07:00
parent 6ab744d674
commit 16b43b08db
8 changed files with 72 additions and 32 deletions

View File

@ -17,24 +17,25 @@
* under the License.
*/
package org.jclouds.ultradns.ws;
import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.String.format;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
/**
* @author Adrian Cole
*/
public final class UltraDNSWSError {
public static UltraDNSWSError fromCodeAndDescription(int code, String description) {
public static UltraDNSWSError fromCodeAndDescription(int code, Optional<String> description) {
return new UltraDNSWSError(code, description);
}
private final int code;
private final String description;
private final Optional<String> description;
private UltraDNSWSError(int code, String description) {
private UltraDNSWSError(int code, Optional<String> description) {
this.code = code;
this.description = checkNotNull(description, "description for code %s", code);
}
@ -49,7 +50,7 @@ public final class UltraDNSWSError {
/**
* The description of the error. ex {@code Zone does not exist in the system.}
*/
public String getDescription() {
public Optional<String> getDescription() {
return description;
}
@ -70,6 +71,6 @@ public final class UltraDNSWSError {
@Override
public String toString() {
return String.format("Error %s: %s", code, description);
return description.isPresent() ? format("Error %s: %s", code, description.get()) : format("Error %s", code);
}
}

View File

@ -78,10 +78,7 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
* there are 51002 potential codes. This defines the ones we are handling.
*/
static final class ErrorCodes {
/**
* Cannot find task with guid.
*/
static final int TASK_NOT_FOUND = 0;
static final int UNKNOWN = 0;
/**
* Zone does not exist in the system.
*/
@ -117,18 +114,23 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
}
private Exception refineException(UltraDNSWSResponseException exception) {
String message = exception.getError().getDescription().or(exception.getMessage());
switch (exception.getError().getCode()) {
case TASK_NOT_FOUND:
case UNKNOWN:
if (!exception.getError().getDescription().isPresent())
return exception;
if (exception.getError().getDescription().get().indexOf("Cannot find") == -1)
return exception;
case ZONE_NOT_FOUND:
case RESOURCE_RECORD_NOT_FOUND:
case ACCOUNT_NOT_FOUND:
case POOL_NOT_FOUND:
case POOL_RECORD_NOT_FOUND:
return new ResourceNotFoundException(exception.getError().getDescription(), exception);
return new ResourceNotFoundException(message, exception);
case ZONE_ALREADY_EXISTS:
case RESOURCE_RECORD_ALREADY_EXISTS:
case POOL_ALREADY_EXISTS:
return new ResourceAlreadyExistsException(exception.getError().getDescription(), exception);
return new ResourceAlreadyExistsException(message, exception);
}
return exception;
}

View File

@ -24,6 +24,8 @@ import static org.jclouds.util.SaxUtils.equalsOrSuffix;
import org.jclouds.http.functions.ParseSax;
import org.jclouds.ultradns.ws.UltraDNSWSError;
import com.google.common.base.Optional;
/**
*
* @author Adrian Cole
@ -36,12 +38,7 @@ public class UltraWSExceptionHandler extends ParseSax.HandlerForGeneratedRequest
@Override
public UltraDNSWSError getResult() {
try {
return code != -1 ? UltraDNSWSError.fromCodeAndDescription(code, description) : null;
} finally {
code = -1;
description = null;
}
return code != -1 ? UltraDNSWSError.fromCodeAndDescription(code, Optional.fromNullable(description)) : null;
}
@Override

View File

@ -53,7 +53,7 @@ public class TaskApiLiveTest extends BaseUltraDNSWSApiLiveTest {
assertEquals(got.getGuid(), task.getGuid());
assertEquals(got.getStatusCode(), task.getStatusCode());
assertEquals(got.getMessage(), task.getMessage());
assertEquals(got.getResultUrl(), task.getMessage());
assertEquals(got.getResultUrl(), task.getResultUrl());
}
}

View File

@ -47,7 +47,26 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSErrorHandler.class);
@Test
public void testCode0SetsResourceNotFoundException() throws IOException {
public void testCode0SetsUltraDNSWSResponseException() throws IOException {
HttpRequest request = HttpRequest.builder().method("POST")
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader("Host", "ultra-api.ultradns.com:8443").payload(payloadFromResource("/list_tasks.xml")).build();
HttpCommand command = new HttpCommand(request);
HttpResponse response = HttpResponse.builder().message("Server Error").statusCode(500)
.payload(payloadFromResource("/server_fault.xml")).build();
function.handleError(command, response);
assertEquals(command.getException().getClass(), UltraDNSWSResponseException.class);
assertEquals(command.getException().getMessage(), "Error 0");
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException());
assertEquals(exception.getError().getCode(), 0);
}
@Test
public void testCode0ForDescriptionMatchingCannotFindSetsResourceNotFoundException() throws IOException {
HttpRequest request = HttpRequest.builder().method("POST")
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader("Host", "ultra-api.ultradns.com:8443").payload(payloadFromResource("/list_tasks.xml")).build();
@ -63,7 +82,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 0: Cannot find task with guid AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription(), "Cannot find task with guid AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription().get(), "Cannot find task with guid AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getCode(), 0);
}
@ -85,7 +104,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 2401: Account not found in the system. ID: AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription(), "Account not found in the system. ID: AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription().get(), "Account not found in the system. ID: AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getCode(), 2401);
}
@ -106,7 +125,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 1801: Zone does not exist in the system.");
assertEquals(exception.getError().getDescription(), "Zone does not exist in the system.");
assertEquals(exception.getError().getDescription().get(), "Zone does not exist in the system.");
assertEquals(exception.getError().getCode(), 1801);
}
@ -127,7 +146,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 2103: No Resource Record with GUID found in the system AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription(), "No Resource Record with GUID found in the system AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getDescription().get(), "No Resource Record with GUID found in the system AAAAAAAAAAAAAAAA");
assertEquals(exception.getError().getCode(), 2103);
}
@ -148,7 +167,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 1802: Zone already exists in the system.");
assertEquals(exception.getError().getDescription(), "Zone already exists in the system.");
assertEquals(exception.getError().getDescription().get(), "Zone already exists in the system.");
assertEquals(exception.getError().getCode(), 1802);
}
@ -171,7 +190,7 @@ public class UltraDNSWSErrorHandlerTest {
assertEquals(exception.getMessage(),
"Error 2111: Resource Record of type 15 with these attributes already exists in the system.");
assertEquals(exception.getError().getDescription(),
assertEquals(exception.getError().getDescription().get(),
"Resource Record of type 15 with these attributes already exists in the system.");
assertEquals(exception.getError().getCode(), 2111);
}
@ -193,7 +212,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 2911: Pool does not exist in the system");
assertEquals(exception.getError().getDescription(), "Pool does not exist in the system");
assertEquals(exception.getError().getDescription().get(), "Pool does not exist in the system");
assertEquals(exception.getError().getCode(), 2911);
}
@ -216,7 +235,7 @@ public class UltraDNSWSErrorHandlerTest {
assertEquals(exception.getMessage(),
"Error 2912: Pool already created for this host name : www.rrpool.adrianc.rrpool.ultradnstest.jclouds.org.");
assertEquals(exception.getError().getDescription(),
assertEquals(exception.getError().getDescription().get(),
"Pool already created for this host name : www.rrpool.adrianc.rrpool.ultradnstest.jclouds.org.");
assertEquals(exception.getError().getCode(), 2912);
}
@ -238,7 +257,7 @@ public class UltraDNSWSErrorHandlerTest {
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 3101: Pool Record does not exist.");
assertEquals(exception.getError().getDescription(), "Pool Record does not exist.");
assertEquals(exception.getError().getDescription().get(), "Pool Record does not exist.");
assertEquals(exception.getError().getCode(), 3101);
}

View File

@ -27,6 +27,8 @@ import org.jclouds.ultradns.ws.UltraDNSWSError;
import org.jclouds.ultradns.ws.xml.UltraWSExceptionHandler;
import org.testng.annotations.Test;
import com.google.common.base.Optional;
/**
* @author Adrian Cole
*/
@ -45,6 +47,6 @@ public class TaskNotFoundTest extends BaseHandlerTest {
}
public UltraDNSWSError expected() {
return UltraDNSWSError.fromCodeAndDescription(0, "Cannot find task with guid AAAAAAAAAAAAAAAA");
return UltraDNSWSError.fromCodeAndDescription(0, Optional.of("Cannot find task with guid AAAAAAAAAAAAAAAA"));
}
}

View File

@ -27,6 +27,8 @@ import org.jclouds.ultradns.ws.UltraDNSWSError;
import org.jclouds.ultradns.ws.xml.UltraWSExceptionHandler;
import org.testng.annotations.Test;
import com.google.common.base.Optional;
/**
* @author Adrian Cole
*/
@ -45,7 +47,7 @@ public class UltraWSExceptionTest extends BaseHandlerTest {
}
public UltraDNSWSError expected() {
return UltraDNSWSError.fromCodeAndDescription(1801, "Zone does not exist in the system.");
return UltraDNSWSError.fromCodeAndDescription(1801, Optional.of("Zone does not exist in the system."));
}
}

View File

@ -0,0 +1,17 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
<detail>
<ns1:UltraWSException xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<errorCode xmlns:ns2="http://schema.ultraservice.neustar.com/v01/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:int">0</errorCode>
<errorDescription xmlns:ns2="http://schema.ultraservice.neustar.com/v01/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
</ns1:UltraWSException>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>