Skip to content

Commit 4b58a87

Browse files
committed
Merge pull request #2993 from davidmoten/take-while-cause
takeWhile(predicate) - include last value in error cause
2 parents 84084da + 2d98e34 commit 4b58a87

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import rx.Observable.Operator;
1919
import rx.Subscriber;
20+
import rx.exceptions.Exceptions;
21+
import rx.exceptions.OnErrorThrowable;
2022
import rx.functions.Func1;
2123
import rx.functions.Func2;
2224

@@ -52,18 +54,19 @@ public Subscriber<? super T> call(final Subscriber<? super T> subscriber) {
5254
private boolean done = false;
5355

5456
@Override
55-
public void onNext(T args) {
57+
public void onNext(T t) {
5658
boolean isSelected;
5759
try {
58-
isSelected = predicate.call(args, counter++);
60+
isSelected = predicate.call(t, counter++);
5961
} catch (Throwable e) {
6062
done = true;
61-
subscriber.onError(e);
63+
Exceptions.throwIfFatal(e);
64+
subscriber.onError(OnErrorThrowable.addValueAsLastCause(e, t));
6265
unsubscribe();
6366
return;
6467
}
6568
if (isSelected) {
66-
subscriber.onNext(args);
69+
subscriber.onNext(t);
6770
} else {
6871
done = true;
6972
subscriber.onCompleted();

src/test/java/rx/internal/operators/OperatorTakeWhileTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package rx.internal.operators;
1717

18+
import static org.junit.Assert.assertTrue;
1819
import static org.junit.Assert.fail;
1920
import static org.mockito.Matchers.any;
2021
import static org.mockito.Mockito.*;
@@ -25,6 +26,7 @@
2526

2627
import rx.*;
2728
import rx.Observable.OnSubscribe;
29+
import rx.exceptions.TestException;
2830
import rx.functions.Func1;
2931
import rx.observers.TestSubscriber;
3032
import rx.subjects.*;
@@ -261,4 +263,20 @@ public Boolean call(Integer t1) {
261263

262264
Assert.assertFalse("Unsubscribed!", ts.isUnsubscribed());
263265
}
266+
267+
@Test
268+
public void testErrorCauseIncludesLastValue() {
269+
TestSubscriber<String> ts = new TestSubscriber<String>();
270+
Observable.just("abc").takeWhile(new Func1<String, Boolean>() {
271+
@Override
272+
public Boolean call(String t1) {
273+
throw new TestException();
274+
}
275+
}).subscribe(ts);
276+
277+
ts.assertTerminalEvent();
278+
ts.assertNoValues();
279+
assertTrue(ts.getOnErrorEvents().get(0).getCause().getMessage().contains("abc"));
280+
}
281+
264282
}

0 commit comments

Comments
 (0)