Skip to content
Browse files

Merge pull request #18 from sonatype/explain-throwable

REVIEW: Add helper to explain a throwable casual-chain tersely
  • Loading branch information...
2 parents 4d84aef + 665c430 commit f2cf4974bd0bc9b3675d545c2a02482c664d8dd8 @jdillon jdillon committed
View
6 common/pom.xml
@@ -53,6 +53,12 @@
</dependency>
<dependency>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>animal-sniffer-annotations</artifactId>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>org.sonatype.sisu.litmus</groupId>
<artifactId>litmus-testsupport</artifactId>
<scope>test</scope>
View
91 common/src/main/java/org/sonatype/sisu/goodies/common/Throwables2.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
+ *
+ * This program is licensed to you under the Apache License Version 2.0,
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the Apache License Version 2.0 is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
+ */
+
+package org.sonatype.sisu.goodies.common;
+
+import java.util.Collection;
+
+import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Throwable utilities.
+ *
+ * @since 1.7.4
+ */
+public final class Throwables2
+{
+ /**
+ * Explain an exception and its causal-chain tersely.
+ */
+ public static String explain(final Throwable throwable) {
+ checkNotNull(throwable);
+
+ StringBuilder buff = new StringBuilder();
+ explain(buff, throwable);
+
+ Throwable cause = throwable;
+ while ((cause = cause.getCause()) != null) {
+ buff.append(", caused by: ");
+ explain(buff, cause);
+ }
+
+ return buff.toString();
+ }
+
+ private static void explain(final StringBuilder buff, final Throwable throwable) {
+ buff.append(throwable.getClass().getName());
+ String msg = throwable.getMessage();
+ if (msg != null) {
+ // if there is a message, check to see if is the same as the cause and only include if its different
+ Throwable cause = throwable.getCause();
+ // handles Throwable(Throwable) case where message is set to cause.toString()
+ if (cause == null || !msg.equals(cause.toString())) {
+ buff.append(": ").append(msg);
+ }
+ }
+ }
+
+ // FIXME: This requires Java7 API, but goodies is presently Java6.
+ // FIXME: This should compile to Java6 bytecode w/o any problems and can used on a Java7 JRE.
+ // FIXME: Remove @IgnoreJRERequirement once goodies is based on Java7.
+
+ /**
+ * Helper to composite suppressed exceptions onto given throwable and throw.
+ */
+ @IgnoreJRERequirement
+ public static <T extends Throwable> T composite(final T root, final Throwable... suppressed)
+ throws T
+ {
+ checkNotNull(suppressed);
+ for (Throwable t : suppressed) {
+ root.addSuppressed(t);
+ }
+ throw root;
+ }
+
+ /**
+ * Helper to composite suppressed exceptions onto given throwable and throw.
+ */
+ @IgnoreJRERequirement
+ public static <T extends Throwable> T composite(final T root, final Collection<? extends Throwable> suppressed)
+ throws T
+ {
+ checkNotNull(suppressed);
+ for (Throwable t : suppressed) {
+ root.addSuppressed(t);
+ }
+ throw root;
+ }
+}
View
72 common/src/test/java/org/sonatype/sisu/goodies/common/Throwables2Test.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2007-2012 Sonatype, Inc. All rights reserved.
+ *
+ * This program is licensed to you under the Apache License Version 2.0,
+ * and you may not use this file except in compliance with the Apache License Version 2.0.
+ * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the Apache License Version 2.0 is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
+ */
+
+package org.sonatype.sisu.goodies.common;
+
+import org.sonatype.sisu.litmus.testsupport.TestSupport;
+
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Tests for {@link Throwables2}.
+ */
+public class Throwables2Test
+ extends TestSupport
+{
+ @Test
+ public void explainThrowable() {
+ String msg = Throwables2.explain(new RuntimeException("foo"));
+ log(msg);
+ assertThat(msg, is("java.lang.RuntimeException: foo"));
+ }
+
+ @Test
+ public void explainThrowable2() {
+ String msg = Throwables2.explain(
+ new RuntimeException("foo",
+ new Exception("bar")
+ )
+ );
+ log(msg);
+ assertThat(msg, is("java.lang.RuntimeException: foo, caused by: java.lang.Exception: bar"));
+ }
+
+ @Test
+ public void explainThrowable3() {
+ String msg = Throwables2.explain(
+ new RuntimeException("foo",
+ new Exception(
+ new Exception("bar")
+ )
+ )
+ );
+ log(msg);
+ assertThat(msg, is("java.lang.RuntimeException: foo, caused by: java.lang.Exception, caused by: java.lang.Exception: bar"));
+ }
+
+ @Test
+ public void explainThrowable4() {
+ String msg = Throwables2.explain(
+ new RuntimeException("foo",
+ new Exception("bar",
+ new Exception("baz")
+ )
+ )
+ );
+ log(msg);
+ assertThat(msg, is("java.lang.RuntimeException: foo, caused by: java.lang.Exception: bar, caused by: java.lang.Exception: baz"));
+ }
+}
View
8 pom.xml
@@ -65,6 +65,12 @@
</dependency>
<dependency>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>animal-sniffer-annotations</artifactId>
+ <version>1.9</version>
+ </dependency>
+
+ <dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
@@ -328,7 +334,7 @@
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java16</artifactId>
- <version>1.0</version>
+ <version>1.1</version>
</signature>
</configuration>
</execution>

0 comments on commit f2cf497

Please sign in to comment.
Something went wrong with that request. Please try again.