You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/06/18 17:41:53 UTC

[commons-lang] 05/05: Add ExceptionUtils.getRootCauseStackTraceList(Throwable)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git

commit 911fbb9352f029dd02971eebdbf7d9cffc7f9175
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 18 13:41:40 2022 -0400

    Add ExceptionUtils.getRootCauseStackTraceList(Throwable)
---
 src/changes/changes.xml                            |  1 +
 .../commons/lang3/exception/ExceptionUtils.java    | 31 +++++++++++++++-------
 .../lang3/exception/ExceptionUtilsTest.java        | 26 ++++++++++++++++++
 3 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ddfbc2534..0946e7e68 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -149,6 +149,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add DurationUtils.of(FailableConsumer|FailableRunnbale).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.forEach(Throwable, Consumer&lt;Throwable&gt;).</action>
     <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.stream(Throwable).</action>
+    <action                   type="add" dev="ggregory" due-to="Gary Gregory">Add ExceptionUtils.getRootCauseStackTraceList(Throwable).</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.4 #742, #752, #764, #833, #867.</action>
     <action                   type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>
diff --git a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index 33333d58b..89316cb1b 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -23,6 +23,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.UndeclaredThrowableException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.StringTokenizer;
@@ -266,8 +267,24 @@ public class ExceptionUtils {
      * @since 2.0
      */
     public static String[] getRootCauseStackTrace(final Throwable throwable) {
+        return getRootCauseStackTraceList(throwable).toArray(ArrayUtils.EMPTY_STRING_ARRAY);
+    }
+
+    /**
+     * Gets a compact stack trace for the root cause of the supplied {@code Throwable}.
+     *
+     * <p>
+     * The output of this method is consistent across JDK versions. It consists of the root exception followed by each of
+     * its wrapping exceptions separated by '[wrapped]'. Note that this is the opposite order to the JDK1.4 display.
+     * </p>
+     *
+     * @param throwable the throwable to examine, may be null
+     * @return a list of stack trace frames, never null
+     * @since 3.13.0
+     */
+    public static List<String> getRootCauseStackTraceList(final Throwable throwable) {
         if (throwable == null) {
-            return ArrayUtils.EMPTY_STRING_ARRAY;
+            return Collections.emptyList();
         }
         final Throwable[] throwables = getThrowables(throwable);
         final int count = throwables.length;
@@ -286,7 +303,7 @@ public class ExceptionUtils {
             }
             frames.addAll(trace);
         }
-        return frames.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
+        return frames;
     }
 
     /**
@@ -661,10 +678,7 @@ public class ExceptionUtils {
             return;
         }
         Objects.requireNonNull(printStream, "printStream");
-        final String[] trace = getRootCauseStackTrace(throwable);
-        for (final String element : trace) {
-            printStream.println(element);
-        }
+        getRootCauseStackTraceList(throwable).forEach(printStream::println);
         printStream.flush();
     }
 
@@ -693,10 +707,7 @@ public class ExceptionUtils {
             return;
         }
         Objects.requireNonNull(printWriter, "printWriter");
-        final String[] trace = getRootCauseStackTrace(throwable);
-        for (final String element : trace) {
-            printWriter.println(element);
-        }
+        getRootCauseStackTraceList(throwable).forEach(printWriter::println);
         printWriter.flush();
     }
 
diff --git a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
index 47ff55750..ffe83c625 100644
--- a/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/exception/ExceptionUtilsTest.java
@@ -343,6 +343,32 @@ public class ExceptionUtilsTest {
         assertFalse(match);
     }
 
+    @Test
+    public void testGetRootCauseStackTraceList_Throwable() {
+        assertEquals(0, ExceptionUtils.getRootCauseStackTraceList(null).size());
+
+        final Throwable cause = createExceptionWithCause();
+        List<String> stackTrace = ExceptionUtils.getRootCauseStackTraceList(cause);
+        boolean match = false;
+        for (final String element : stackTrace) {
+            if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
+                match = true;
+                break;
+            }
+        }
+        assertTrue(match);
+
+        stackTrace = ExceptionUtils.getRootCauseStackTraceList(withoutCause);
+        match = false;
+        for (final String element : stackTrace) {
+            if (element.startsWith(ExceptionUtils.WRAPPED_MARKER)) {
+                match = true;
+                break;
+            }
+        }
+        assertFalse(match);
+    }
+
     @Test
     @DisplayName("getStackFrames returns empty string array when the argument is null")
     public void testgetStackFramesHappyPath() {