now with guice in production mode, we get exceptions wrapped in CreationException and need to unwrap these accordingly

This commit is contained in:
Adrian Cole 2012-01-12 18:02:34 -08:00
parent 983b1fbe07
commit 5039708d34
2 changed files with 64 additions and 8 deletions

View File

@ -31,6 +31,7 @@ import org.jclouds.rest.InsufficientResourcesException;
import org.jclouds.rest.ResourceNotFoundException;
import com.google.common.base.Throwables;
import com.google.inject.CreationException;
import com.google.inject.ProvisionException;
import com.google.inject.spi.Message;
@ -45,6 +46,8 @@ public class Throwables2 {
public static <T extends Throwable> T getFirstThrowableOfType(Throwable from, Class<T> clazz) {
if (from instanceof ProvisionException)
return getFirstThrowableOfType(ProvisionException.class.cast(from), clazz);
else if (from instanceof CreationException)
return getFirstThrowableOfType(CreationException.class.cast(from), clazz);
try {
return (T) find(getCausalChain(from), instanceOf(clazz));
} catch (NoSuchElementException e) {
@ -58,6 +61,22 @@ public class Throwables2 {
T cause = getFirstThrowableOfType(message.getCause(), clazz);
if (cause instanceof ProvisionException)
return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz);
else if (cause instanceof CreationException)
return getFirstThrowableOfType(CreationException.class.cast(cause), clazz);
return cause;
}
}
return null;
}
public static <T extends Throwable> T getFirstThrowableOfType(CreationException e, Class<T> clazz) {
for (Message message : e.getErrorMessages()) {
if (message.getCause() != null) {
T cause = getFirstThrowableOfType(message.getCause(), clazz);
if (cause instanceof ProvisionException)
return getFirstThrowableOfType(ProvisionException.class.cast(cause), clazz);
else if (cause instanceof CreationException)
return getFirstThrowableOfType(CreationException.class.cast(cause), clazz);
return cause;
}
}

View File

@ -18,8 +18,8 @@
*/
package org.jclouds.util;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createNiceMock;
import static org.jclouds.util.Throwables2.getFirstThrowableOfType;
import static org.jclouds.util.Throwables2.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause;
import static org.testng.Assert.assertEquals;
@ -37,6 +37,7 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.inject.CreationException;
import com.google.inject.ProvisionException;
import com.google.inject.spi.Message;
@ -46,12 +47,6 @@ import com.google.inject.spi.Message;
@Test
public class Throwables2Test {
public void testGetCause() {
AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
}
public void testGetFirstThrowableOfTypeSubclass() {
SocketException aex = createMock(SocketException.class);
@ -62,6 +57,13 @@ public class Throwables2Test {
AuthorizationException aex = createMock(AuthorizationException.class);
assertEquals(getFirstThrowableOfType(aex, AuthorizationException.class), aex);
}
public void testGetCause() {
AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
}
public void testGetFirstThrowableOfTypeInner() {
AuthorizationException aex = createMock(AuthorizationException.class);
@ -83,6 +85,34 @@ public class Throwables2Test {
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
}
public void testGetCauseCreation() {
AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
CreationException pex = new CreationException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
}
public void testGetFirstThrowableOfTypeInnerCreation() {
AuthorizationException aex = createMock(AuthorizationException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
CreationException pex = new CreationException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
}
public void testGetFirstThrowableOfTypeFailCreation() {
TimeoutException aex = createMock(TimeoutException.class);
Message message = new Message(ImmutableList.of(), "test", aex);
CreationException pex = new CreationException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
}
public void testGetFirstThrowableOfTypeWhenCauseIsNullCreation() {
Message message = new Message(ImmutableList.of(), "test", null);
CreationException pex = new CreationException(ImmutableSet.of(message));
assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
}
@Test
public void testReturnExceptionThatsInList() throws Exception {
Exception e = new TestException();
@ -135,6 +165,13 @@ public class Throwables2Test {
ImmutableList.of(), "Error in custom provider",e))));
}
@Test(expectedExceptions = AuthorizationException.class)
public void testPropagateCreationExceptionAuthorizationException() throws Exception {
Exception e = new AuthorizationException();
returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new CreationException(ImmutableSet.of(new Message(
ImmutableList.of(), "Error in custom provider",e))));
}
@Test(expectedExceptions = InsufficientResourcesException.class)
public void testPropagateStandardExceptionInsufficientResourcesException() throws Exception {
Exception e = new InsufficientResourcesException();