Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e149ee4

Browse files
andcltmaxday
authored andcommittedJun 28, 2024
fix: handle circular exception references (#63)
1 parent 09f74a0 commit e149ee4

File tree

2 files changed

+58
-3
lines changed
  • aws-lambda-java-runtime-interface-client/src

2 files changed

+58
-3
lines changed
 

‎aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/UserFault.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import java.io.PrintWriter;
66
import java.io.StringWriter;
7+
import java.util.HashSet;
8+
import java.util.Set;
79

810
public final class UserFault extends RuntimeException {
911
private static final long serialVersionUID = -479308856905162038L;
@@ -65,6 +67,16 @@ public static String trace(Throwable t) {
6567
* the same object for convenience.
6668
*/
6769
public static <T extends Throwable> T filterStackTrace(T t) {
70+
return filterStackTrace(t, new HashSet<>(), new HashSet<>());
71+
}
72+
73+
private static <T extends Throwable> T filterStackTrace(T t, Set<Throwable> visited, Set<Throwable> visitedSuppressed) {
74+
if (visited.contains(t)) {
75+
return t;
76+
}
77+
78+
visited.add(t);
79+
6880
StackTraceElement[] trace = t.getStackTrace();
6981
for (int i = 0; i < trace.length; i++) {
7082
if (trace[i].getClassName().startsWith(packagePrefix)) {
@@ -78,12 +90,15 @@ public static <T extends Throwable> T filterStackTrace(T t) {
7890

7991
Throwable cause = t.getCause();
8092
if (cause != null) {
81-
filterStackTrace(cause);
93+
filterStackTrace(cause, visited, visitedSuppressed);
8294
}
8395

8496
Throwable[] suppressedExceptions = t.getSuppressed();
85-
for (Throwable suppressed : suppressedExceptions) {
86-
filterStackTrace(suppressed);
97+
for(Throwable suppressed: suppressedExceptions) {
98+
if (!visitedSuppressed.contains(suppressed)) {
99+
visitedSuppressed.add(suppressed);
100+
filterStackTrace(suppressed, visited, visitedSuppressed);
101+
}
87102
}
88103

89104
return t;

‎aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/UserFaultTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,44 @@ public void testSuppressedExceptionsAreIncluded() {
8484
assertTrue(reportableUserFault.contains("Suppressed: java.lang.RuntimeException: error 2"), "Suppressed error 2 missing in reported UserFault");
8585
}
8686
}
87+
88+
@Test
89+
public void testCircularExceptionReference() {
90+
RuntimeException e1 = new RuntimeException();
91+
RuntimeException e2 = new RuntimeException(e1);
92+
e1.initCause(e2);
93+
94+
try {
95+
throw e2;
96+
} catch (Exception e) {
97+
String stackTrace = UserFault.trace(e);
98+
String expectedStackTrace = "java.lang.RuntimeException: java.lang.RuntimeException\n" +
99+
"Caused by: java.lang.RuntimeException\n" +
100+
"Caused by: [CIRCULAR REFERENCE: java.lang.RuntimeException: java.lang.RuntimeException]\n";
101+
102+
assertEquals(expectedStackTrace, stackTrace);
103+
}
104+
}
105+
106+
@Test
107+
public void testCircularSuppressedExceptionReference() {
108+
RuntimeException e1 = new RuntimeException("Primary Exception");
109+
RuntimeException e2 = new RuntimeException(e1);
110+
RuntimeException e3 = new RuntimeException("Exception with suppressed");
111+
112+
e1.addSuppressed(e2);
113+
e3.addSuppressed(e2);
114+
115+
try {
116+
throw e3;
117+
} catch (Exception e) {
118+
String stackTrace = UserFault.trace(e);
119+
String expectedStackTrace = "java.lang.RuntimeException: Exception with suppressed\n" +
120+
"\tSuppressed: java.lang.RuntimeException: java.lang.RuntimeException: Primary Exception\n" +
121+
"\tCaused by: java.lang.RuntimeException: Primary Exception\n" +
122+
"\t\tSuppressed: [CIRCULAR REFERENCE: java.lang.RuntimeException: java.lang.RuntimeException: Primary Exception]\n";
123+
124+
assertEquals(expectedStackTrace, stackTrace);
125+
}
126+
}
87127
}

0 commit comments

Comments
 (0)
Please sign in to comment.