From f4f32e07e07ab56bb540dd5a91b22c0fd40c19b6 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Fri, 30 Apr 2010 13:27:55 -0700 Subject: [PATCH] throw root exception for login failures, not provisioningexception --- .../jclouds/compute/BaseComputeServiceLiveTest.java | 7 +++++++ .../java/org/jclouds/rest/RestContextFactory.java | 12 +++++++++++- core/src/main/java/org/jclouds/util/Utils.java | 12 ++++++++++++ core/src/test/java/org/jclouds/util/UtilsTest.java | 13 +++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java b/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java index 4a52b148bb..30098e5ef4 100755 --- a/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java +++ b/compute/src/test/java/org/jclouds/compute/BaseComputeServiceLiveTest.java @@ -54,6 +54,7 @@ import org.jclouds.http.HttpResponseException; import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.SocketOpen; +import org.jclouds.rest.AuthorizationException; import org.jclouds.ssh.ExecResponse; import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshException; @@ -146,6 +147,12 @@ public abstract class BaseComputeServiceLiveTest { abstract protected Module getSshModule(); + @Test(enabled = true, expectedExceptions = AuthorizationException.class) + public void testCorrectAuthException() throws Exception { + new ComputeServiceContextFactory().createContext(service, "MOMMA", "MIA").close(); + } + + @Test(enabled = true, dependsOnMethods = "testCorrectAuthException") public void testImagesCache() throws Exception { client.getImages(); long time = System.currentTimeMillis(); diff --git a/core/src/main/java/org/jclouds/rest/RestContextFactory.java b/core/src/main/java/org/jclouds/rest/RestContextFactory.java index 761b64de41..e081a8d1d5 100644 --- a/core/src/main/java/org/jclouds/rest/RestContextFactory.java +++ b/core/src/main/java/org/jclouds/rest/RestContextFactory.java @@ -29,11 +29,14 @@ import javax.inject.Inject; import org.jclouds.PropertiesBuilder; import org.jclouds.domain.Credentials; +import org.jclouds.util.Utils; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.io.Resources; import com.google.inject.Module; +import com.google.inject.ProvisionException; /** * Helper class to instantiate {@code RestContext} instances. "blobstore.properties" @@ -175,8 +178,15 @@ public abstract class RestContextFactory> B contextBuilder = (B) contextBuilderClass.getConstructor(Properties.class).newInstance( builder.build()).withModules(Iterables.toArray(modules, Module.class)); return build(contextBuilder); + } catch (ProvisionException e) { + Throwable throwable = Utils.firstRootCauseOrOriginalException(e); + Throwables.propagate(throwable); + assert false : "exception should have propogated " + e; + return null; } catch (Exception e) { - throw new RuntimeException("error instantiating " + contextBuilderClassName, e); + Throwables.propagate(Throwables.getRootCause(e)); + assert false : "exception should have propogated " + e; + return null; } } diff --git a/core/src/main/java/org/jclouds/util/Utils.java b/core/src/main/java/org/jclouds/util/Utils.java index 7201fab2cb..24df0e5442 100644 --- a/core/src/main/java/org/jclouds/util/Utils.java +++ b/core/src/main/java/org/jclouds/util/Utils.java @@ -45,6 +45,8 @@ import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteStreams; import com.google.common.io.Closeables; import com.google.common.io.OutputSupplier; +import com.google.inject.ProvisionException; +import com.google.inject.spi.Message; /** * General utilities used in jclouds code. @@ -67,6 +69,16 @@ public class Utils { return null; } + public static Throwable firstRootCauseOrOriginalException(ProvisionException e) { + for (Message message : e.getErrorMessages()) { + Throwable cause = Throwables.getRootCause(message.getCause()); + if (cause instanceof ProvisionException) + return firstRootCauseOrOriginalException(ProvisionException.class.cast(cause)); + return cause; + } + return e; + } + public static String replaceTokens(String value, Collection> tokenValues) { for (Entry tokenValue : tokenValues) { value = replaceAll(value, TOKEN_TO_PATTERN.get(tokenValue.getKey()), tokenValue.getValue()); diff --git a/core/src/test/java/org/jclouds/util/UtilsTest.java b/core/src/test/java/org/jclouds/util/UtilsTest.java index 50dfa73d9a..1efad97c28 100644 --- a/core/src/test/java/org/jclouds/util/UtilsTest.java +++ b/core/src/test/java/org/jclouds/util/UtilsTest.java @@ -18,13 +18,19 @@ */ package org.jclouds.util; +import static org.easymock.classextension.EasyMock.createMock; import static org.testng.Assert.assertEquals; import java.io.UnsupportedEncodingException; +import org.jclouds.rest.AuthorizationException; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.inject.ProvisionException; +import com.google.inject.internal.ImmutableList; +import com.google.inject.spi.Message; /** * @author Adrian Cole @@ -32,6 +38,13 @@ import com.google.common.collect.ImmutableMap; @Test(groups = "unit", testName = "jclouds.UtilsTest") public class UtilsTest { + public void testGetCause() { + AuthorizationException aex = createMock(AuthorizationException.class); + Message message = new Message(ImmutableList.of(), "test", aex); + ProvisionException pex = new ProvisionException(ImmutableSet.of(message)); + assertEquals(Utils.firstRootCauseOrOriginalException(pex), aex); + } + public void testReplaceTokens() throws UnsupportedEncodingException { assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world");