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


##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/DefaultStackTraceWriter.java:
##########
@@ -72,16 +74,38 @@ public String smartTrimmedStackTrace() {
 
     @Override
     public String writeTrimmedTraceToString() {
-        return t == null ? "" : 
SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, testClass);
+        if (t == null) {
+            return "";
+        }
+        try {
+            return SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, 
testClass);
+        } catch (Throwable e) {
+            return classNameAndFrames(t);
+        }
     }
 
     @Override
     public SafeThrowable getThrowable() {
         return t == null ? null : new SafeThrowable(t);
     }
 
+    /**
+     * Used when the exception of a test throws while we print it, for example 
from getMessage() or
+     * toString() (SUREFIRE-818). If we let that escape, the test error is 
never reported.
+     */
+    private static String classNameAndFrames(Throwable t) {
+        StringBuilder trace = new 
StringBuilder(t.getClass().getName()).append('\n');
+        StackTraceElement[] frames = t.getStackTrace();

Review Comment:
   If `getStackTrace()` throws, the trimmed writer catches it but this fallback 
calls it again and throws instead of returning the exception class. The same 
unguarded access in the smart parser can prevent `EventChannelEncoder` from 
sending the result, because it renders the smart trace first. Guard both frame 
retrieval here and `smartTrimmedStackTrace()`, returning a minimal trace when 
rendering fails.



##########
surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/DefaultStackTraceWriter.java:
##########
@@ -72,16 +74,38 @@ public String smartTrimmedStackTrace() {
 
     @Override
     public String writeTrimmedTraceToString() {
-        return t == null ? "" : 
SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, testClass);
+        if (t == null) {
+            return "";
+        }
+        try {
+            return SmartStackTraceParser.stackTraceWithFocusOnClassAsString(t, 
testClass);
+        } catch (Throwable e) {
+            return classNameAndFrames(t);
+        }
     }
 
     @Override
     public SafeThrowable getThrowable() {
         return t == null ? null : new SafeThrowable(t);
     }
 
+    /**
+     * Used when the exception of a test throws while we print it, for example 
from getMessage() or
+     * toString() (SUREFIRE-818). If we let that escape, the test error is 
never reported.
+     */
+    private static String classNameAndFrames(Throwable t) {
+        StringBuilder trace = new 
StringBuilder(t.getClass().getName()).append('\n');

Review Comment:
   With full traces, the new `MessageThrowsTest` gets an XML exception `type` 
containing part of the first stack frame. `StatelessXmlReporter.java:593–596` 
uses the first colon when `SafeThrowable` supplies a non-null message, but that 
colon is now the frame's line-number separator. Extract the type from the 
exception header only, or emit a compatible header, and assert the XML type in 
the 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