Skip to content

Commit c8faee4

Browse files
committed
1.x: Fix Completable using JDK 7 suppressed exceptions feature
1 parent f8dcc93 commit c8faee4

File tree

3 files changed

+35
-36
lines changed

3 files changed

+35
-36
lines changed

src/main/java/rx/Completable.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,7 @@ public void onError(Throwable e) {
938938
try {
939939
disposer.call(resource);
940940
} catch (Throwable ex) {
941-
ex.addSuppressed(e);
942-
e = ex;
941+
e = new CompositeException(Arrays.asList(e, ex));
943942
}
944943
}
945944
}
@@ -1298,8 +1297,7 @@ public void onError(Throwable e) {
12981297
try {
12991298
onError.call(e);
13001299
} catch (Throwable ex) {
1301-
ex.addSuppressed(e);
1302-
e = ex;
1300+
e = new CompositeException(Arrays.asList(e, ex));
13031301
}
13041302

13051303
s.onError(e);
@@ -1619,8 +1617,7 @@ public void onError(Throwable e) {
16191617
try {
16201618
b = predicate.call(e);
16211619
} catch (Throwable ex) {
1622-
e.addSuppressed(ex);
1623-
s.onError(e);
1620+
e = new CompositeException(Arrays.asList(e, ex));
16241621
return;
16251622
}
16261623

@@ -1669,15 +1666,15 @@ public void onError(Throwable e) {
16691666
try {
16701667
c = errorMapper.call(e);
16711668
} catch (Throwable ex) {
1672-
ex.addSuppressed(e);
1673-
s.onError(ex);
1669+
e = new CompositeException(Arrays.asList(e, ex));
1670+
s.onError(e);
16741671
return;
16751672
}
16761673

16771674
if (c == null) {
16781675
NullPointerException npe = new NullPointerException("The completable returned is null");
1679-
npe.addSuppressed(e);
1680-
s.onError(npe);
1676+
e = new CompositeException(Arrays.asList(e, npe));
1677+
s.onError(e);
16811678
return;
16821679
}
16831680

@@ -1900,7 +1897,7 @@ public void onError(Throwable e) {
19001897
try {
19011898
onError.call(e);
19021899
} catch (Throwable ex) {
1903-
e.addSuppressed(ex);
1900+
e = new CompositeException(Arrays.asList(e, ex));
19041901
ERROR_HANDLER.handleError(e);
19051902
}
19061903
}

src/main/java/rx/internal/operators/CompletableOnSubscribeMerge.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package rx.internal.operators;
1818

19-
import java.util.Queue;
19+
import java.util.*;
2020
import java.util.concurrent.ConcurrentLinkedQueue;
2121
import java.util.concurrent.atomic.*;
2222

2323
import rx.*;
2424
import rx.Completable.*;
25+
import rx.exceptions.CompositeException;
26+
import rx.Observable;
2527
import rx.plugins.RxJavaPlugins;
2628
import rx.subscriptions.CompositeSubscription;
2729

@@ -197,19 +199,18 @@ void terminate() {
197199
* @return the Throwable containing all other Throwables as suppressed
198200
*/
199201
public static Throwable collectErrors(Queue<Throwable> q) {
200-
Throwable ex = null;
202+
List<Throwable> list = new ArrayList<Throwable>();
201203

202204
Throwable t;
203-
int count = 0;
204205
while ((t = q.poll()) != null) {
205-
if (count == 0) {
206-
ex = t;
207-
} else {
208-
ex.addSuppressed(t);
209-
}
210-
211-
count++;
206+
list.add(t);
207+
}
208+
if (list.isEmpty()) {
209+
return null;
210+
}
211+
if (list.size() == 1) {
212+
return list.get(0);
212213
}
213-
return ex;
214+
return new CompositeException(list);
214215
}
215216
}

src/test/java/rx/CompletableTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,10 +1872,11 @@ public void doOnErrorThrows() {
18721872

18731873
try {
18741874
c.await();
1875-
} catch (IllegalStateException ex) {
1876-
Throwable[] a = ex.getSuppressed();
1877-
Assert.assertEquals(1, a.length);
1878-
Assert.assertTrue(a[0] instanceof TestException);
1875+
} catch (CompositeException ex) {
1876+
List<Throwable> a = ex.getExceptions();
1877+
Assert.assertEquals(2, a.size());
1878+
Assert.assertTrue(a.get(0) instanceof TestException);
1879+
Assert.assertTrue(a.get(1) instanceof IllegalStateException);
18791880
}
18801881
}
18811882

@@ -2217,11 +2218,11 @@ public Completable call(Throwable e) {
22172218
try {
22182219
c.await();
22192220
Assert.fail("Did not throw an exception");
2220-
} catch (NullPointerException ex) {
2221-
Throwable[] a = ex.getSuppressed();
2222-
2223-
Assert.assertEquals(1, a.length);
2224-
Assert.assertTrue(a[0] instanceof TestException);
2221+
} catch (CompositeException ex) {
2222+
List<Throwable> a = ex.getExceptions();
2223+
Assert.assertEquals(2, a.size());
2224+
Assert.assertTrue(a.get(0) instanceof TestException);
2225+
Assert.assertTrue(a.get(1) instanceof NullPointerException);
22252226
}
22262227
}
22272228

@@ -2235,11 +2236,11 @@ public void onErrorResumeNextFunctionThrows() {
22352236
try {
22362237
c.await();
22372238
Assert.fail("Did not throw an exception");
2238-
} catch (TestException ex) {
2239-
Throwable[] a = ex.getSuppressed();
2240-
2241-
Assert.assertEquals(1, a.length);
2242-
Assert.assertTrue(a[0] instanceof TestException);
2239+
} catch (CompositeException ex) {
2240+
List<Throwable> a = ex.getExceptions();
2241+
Assert.assertEquals(2, a.size());
2242+
Assert.assertTrue(a.get(0) instanceof TestException);
2243+
Assert.assertTrue(a.get(1) instanceof TestException);
22432244
}
22442245
}
22452246

0 commit comments

Comments
 (0)