From 5039708d34ccfc1592de03b8b1f66f231898fbf8 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 12 Jan 2012 18:02:34 -0800 Subject: [PATCH] now with guice in production mode, we get exceptions wrapped in CreationException and need to unwrap these accordingly --- .../java/org/jclouds/util/Throwables2.java | 19 +++++++ .../org/jclouds/util/Throwables2Test.java | 53 ++++++++++++++++--- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/jclouds/util/Throwables2.java b/core/src/main/java/org/jclouds/util/Throwables2.java index 559dfe74f7..00fa94d2f4 100644 --- a/core/src/main/java/org/jclouds/util/Throwables2.java +++ b/core/src/main/java/org/jclouds/util/Throwables2.java @@ -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 getFirstThrowableOfType(Throwable from, Class 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 getFirstThrowableOfType(CreationException e, Class 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; } } diff --git a/core/src/test/java/org/jclouds/util/Throwables2Test.java b/core/src/test/java/org/jclouds/util/Throwables2Test.java index ecc89b9a9c..20110ebb4a 100644 --- a/core/src/test/java/org/jclouds/util/Throwables2Test.java +++ b/core/src/test/java/org/jclouds/util/Throwables2Test.java @@ -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();