File tree Expand file tree Collapse file tree 2 files changed +35
-2
lines changed Expand file tree Collapse file tree 2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -38,9 +38,9 @@ public final class CompositeException extends RuntimeException {
38
38
public CompositeException (String messagePrefix , Collection <Throwable > errors ) {
39
39
List <Throwable > _exceptions = new ArrayList <Throwable >();
40
40
CompositeExceptionCausalChain _cause = new CompositeExceptionCausalChain ();
41
- int count = 0 ;
41
+ int count = errors .size ();
42
+ errors = removeDuplicatedCauses (errors );
42
43
for (Throwable e : errors ) {
43
- count ++;
44
44
attachCallingThreadStack (_cause , e );
45
45
_exceptions .add (e );
46
46
}
@@ -73,6 +73,29 @@ public synchronized Throwable getCause() {
73
73
return cause ;
74
74
}
75
75
76
+ private Collection <Throwable > removeDuplicatedCauses (Collection <Throwable > errors ) {
77
+ Set <Throwable > duplicated = new HashSet <Throwable >();
78
+ for (Throwable cause : errors ) {
79
+ for (Throwable error : errors ) {
80
+ if (cause == error || duplicated .contains (error )) {
81
+ continue ;
82
+ }
83
+ while (error .getCause () != null ) {
84
+ error = error .getCause ();
85
+ if (error == cause ) {
86
+ duplicated .add (cause );
87
+ break ;
88
+ }
89
+ }
90
+ }
91
+ }
92
+ if (!duplicated .isEmpty ()) {
93
+ errors = new ArrayList <Throwable >(errors );
94
+ errors .removeAll (duplicated );
95
+ }
96
+ return errors ;
97
+ }
98
+
76
99
@ SuppressWarnings ("unused" )
77
100
// useful when debugging but don't want to make part of publicly supported API
78
101
private static String getStackTraceAsString (StackTraceElement [] stack ) {
Original file line number Diff line number Diff line change @@ -53,6 +53,16 @@ public void testMultipleWithSameCause() {
53
53
54
54
}
55
55
56
+ @ Test (timeout = 1000 )
57
+ public void testOneIsCauseOfAnother () {
58
+ Throwable rootCause = new Throwable ("RootCause" );
59
+ Throwable throwable = new Throwable ("1" , rootCause );
60
+ CompositeException ce = new CompositeException ("One is the cause of another" ,
61
+ Arrays .asList (rootCause , throwable ));
62
+
63
+ assertEquals (1 , ce .getExceptions ().size ());
64
+ }
65
+
56
66
@ Test (timeout = 1000 )
57
67
public void testAttachCallingThreadStackParentThenChild () {
58
68
CompositeException .attachCallingThreadStack (ex1 , ex2 );
You can’t perform that action at this time.
0 commit comments