Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

REVIEW: Add helper to explain a throwable casual-chain tersely #18

Merged
merged 5 commits into from

2 participants

Jason Dillon Peter Lynch
Jason Dillon
Owner

No description provided.

Jason Dillon
Owner

maybe should avoid the collection creation

Peter Lynch
Owner

looks useful

Jason Dillon jdillon merged commit f2cf497 into from
Jason Dillon jdillon deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 8, 2013
  1. Jason Dillon
  2. Jason Dillon

    avoid collection

    jdillon authored
  3. Jason Dillon
Commits on Nov 9, 2013
  1. Jason Dillon

    Add annimal-sniffer annos

    jdillon authored
    uncomment composite helpers, and make to ignore jre requirements
  2. Jason Dillon

    use latest signature module

    jdillon authored
This page is out of date. Refresh to see the latest.
6 common/pom.xml
View
@@ -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>
91 common/src/main/java/org/sonatype/sisu/goodies/common/Throwables2.java
View
@@ -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;
+ }
+}
72 common/src/test/java/org/sonatype/sisu/goodies/common/Throwables2Test.java
View
@@ -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"));
+ }
+}
8 pom.xml
View
@@ -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>
Something went wrong with that request. Please try again.