Skip to content

Commit 028d189

Browse files
Fix Exception Handling from SafeObserver and Subscriptions
1 parent fdc4c60 commit 028d189

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

rxjava-core/src/main/java/rx/observers/SafeObserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected void _onError(Throwable e) {
148148
throw new RuntimeException("Error occurred when trying to propagate error to Observer.onError", new CompositeException(Arrays.asList(e, e2)));
149149
}
150150
}
151-
// if we did not throw about we will unsubscribe here, if onError failed then unsubscribe happens in the catch
151+
// if we did not throw above we will unsubscribe here, if onError failed then unsubscribe happens in the catch
152152
try {
153153
unsubscribe();
154154
} catch (RuntimeException unsubscribeException) {

rxjava-core/src/main/java/rx/subscriptions/CompositeSubscription.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.Collection;
21+
import java.util.List;
2122
import java.util.concurrent.atomic.AtomicReference;
2223

2324
import rx.Subscription;
@@ -141,7 +142,7 @@ public void unsubscribe() {
141142
}
142143

143144
private static void unsubscribeFromAll(Subscription[] subscriptions) {
144-
final Collection<Throwable> es = new ArrayList<Throwable>();
145+
final List<Throwable> es = new ArrayList<Throwable>();
145146
for (Subscription s : subscriptions) {
146147
try {
147148
s.unsubscribe();
@@ -150,8 +151,18 @@ private static void unsubscribeFromAll(Subscription[] subscriptions) {
150151
}
151152
}
152153
if (!es.isEmpty()) {
153-
throw new CompositeException(
154-
"Failed to unsubscribe to 1 or more subscriptions.", es);
154+
if (es.size() == 1) {
155+
Throwable t = es.get(0);
156+
if (t instanceof RuntimeException) {
157+
throw (RuntimeException) t;
158+
} else {
159+
throw new CompositeException(
160+
"Failed to unsubscribe to 1 or more subscriptions.", es);
161+
}
162+
} else {
163+
throw new CompositeException(
164+
"Failed to unsubscribe to 2 or more subscriptions.", es);
165+
}
155166
}
156167
}
157168
}

rxjava-core/src/test/java/rx/subscriptions/CompositeSubscriptionTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,52 @@ public void unsubscribe() {
116116
}
117117
});
118118

119+
try {
120+
s.unsubscribe();
121+
fail("Expecting an exception");
122+
} catch (RuntimeException e) {
123+
// we expect this
124+
assertEquals(e.getMessage(), "failed on first one");
125+
}
126+
127+
// we should still have unsubscribed to the second one
128+
assertEquals(1, counter.get());
129+
}
130+
131+
@Test
132+
public void testCompositeException() {
133+
final AtomicInteger counter = new AtomicInteger();
134+
CompositeSubscription s = new CompositeSubscription();
135+
s.add(new Subscription() {
136+
137+
@Override
138+
public void unsubscribe() {
139+
throw new RuntimeException("failed on first one");
140+
}
141+
});
142+
143+
s.add(new Subscription() {
144+
145+
@Override
146+
public void unsubscribe() {
147+
throw new RuntimeException("failed on second one too");
148+
}
149+
});
150+
151+
s.add(new Subscription() {
152+
153+
@Override
154+
public void unsubscribe() {
155+
counter.incrementAndGet();
156+
}
157+
});
158+
119159
try {
120160
s.unsubscribe();
121161
fail("Expecting an exception");
122162
} catch (CompositeException e) {
123163
// we expect this
124-
assertEquals(1, e.getExceptions().size());
164+
assertEquals(e.getExceptions().size(), 2);
125165
}
126166

127167
// we should still have unsubscribed to the second one

0 commit comments

Comments
 (0)