Copilot commented on code in PR #3645:
URL: https://github.com/apache/maven-surefire/pull/3645#discussion_r4730544100


##########
surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java:
##########
@@ -202,25 +204,46 @@ private static boolean 
containsClassName(StackTraceElement[] stackTrace, StackTr
         return false;
     }
 
-    private static String causeToString(Throwable cause, StackTraceFilter 
filter) {
+    private static String causeToString(Throwable topmost, StackTraceFilter 
filter) {
         StringBuilder resp = new StringBuilder();
-        while (cause != null) {
+        Set<Throwable> seen = newIdentitySet();
+        seen.add(topmost);
+        for (Throwable cause = topmost.getCause(); cause != null && 
seen.add(cause); cause = cause.getCause()) {
             resp.append("Caused by: ");
-            resp.append(toString(cause, asList(cause.getStackTrace()), 
filter));
-            cause = cause.getCause();
+            resp.append(toString(cause, asList(stackTraceOf(cause)), filter));
         }
         return resp.toString();
     }
 
+    // two exceptions can be the cause of each other, so remember which ones 
we already walked through
+    private static Set<Throwable> newIdentitySet() {
+        return Collections.newSetFromMap(new IdentityHashMap<>());
+    }
+
+    /**
+     * The frames of {@code t}, or none. A mocked exception can return null 
here, or throw, and an overridden
+     * {@code getStackTrace()} can even give back null frames, which we leave 
out.
+     */
+    public static StackTraceElement[] stackTraceOf(Throwable t) {
+        try {
+            StackTraceElement[] stackTrace = t.getStackTrace();
+            return stackTrace == null
+                    ? new StackTraceElement[0]
+                    : 
Arrays.stream(stackTrace).filter(Objects::nonNull).toArray(StackTraceElement[]::new);
+        } catch (Throwable e) {
+            return new StackTraceElement[0];
+        }
+    }
+
     private static String toString(Throwable t, Iterable<StackTraceElement> 
elements, StackTraceFilter filter) {
         StringBuilder result = new StringBuilder();
         if (t != null) {
             result.append(t.getClass().getName());
             String msg;
-            // SUREFIRE-818
+            // SUREFIRE-818: a test's exception can throw from getMessage()
             try {
                 msg = t.getMessage();
-            } catch (NullPointerException npe) {
+            } catch (Throwable e) {
                 msg = "";

Review Comment:
   With forked execution and `trimStackTrace=true`, these fixtures still 
produce invalid XML types: `SelfThrowingException` gains a trailing colon, and 
`NoLocaleException` includes stack-frame text. Build the trimmed header from 
`new SafeThrowable(t).getLocalizedMessage()`, omitting the colon when the 
result is null, to match the serialized message. Update the affected unit 
expectations and assert both exact XML types in the trimmed integration test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to