Skip to content

Commit

Permalink
Merge pull request ReactiveX#3626 from davidmoten/simplify-throw
Browse files Browse the repository at this point in the history
use Exceptions.throwOrError to simplify error handling
  • Loading branch information
zsxwing committed Jan 22, 2016
2 parents 90df5da + 25f7667 commit fe9badc
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 27 deletions.
6 changes: 2 additions & 4 deletions src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,15 @@ public void call(Subscriber<? super R> o) {
st.onStart();
onSubscribe.call(st);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
// localized capture of errors rather than it skipping all operators
// and ending up in the try/catch of the subscribe method which then
// prevents onErrorResumeNext and other similar approaches to error handling
st.onError(e);
Exceptions.throwOrReport(e, st);
}
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
// if the lift function failed all we can do is pass the error to the final Subscriber
// as we don't have the operator available to us
o.onError(e);
Exceptions.throwOrReport(e, o);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public void call(Subscriber<? super T> subscriber) {
try {
singleDelayedProducer.setValue(resultFactory.call());
} catch (Throwable t) {
Exceptions.throwIfFatal(t);
subscriber.onError(t);
Exceptions.throwOrReport(t, subscriber);
}
}
}
9 changes: 3 additions & 6 deletions src/main/java/rx/internal/operators/OperatorScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public void onNext(T t) {
try {
v = accumulator.call(v, t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
child.onError(OnErrorThrowable.addValueAsLastCause(e, t));
Exceptions.throwOrReport(e, child, t);
return;
}
}
Expand Down Expand Up @@ -138,8 +137,7 @@ public void onNext(T currentValue) {
try {
v = accumulator.call(v, currentValue);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
onError(OnErrorThrowable.addValueAsLastCause(e, currentValue));
Exceptions.throwOrReport(e, this, currentValue);
return;
}
value = v;
Expand Down Expand Up @@ -322,8 +320,7 @@ void emitLoop() {
try {
child.onNext(v);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
child.onError(OnErrorThrowable.addValueAsLastCause(ex, v));
Exceptions.throwOrReport(ex, child, v);
return;
}
r--;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/rx/internal/operators/OperatorToMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public Subscriber<? super T> call(final Subscriber<? super Map<K, V>> subscriber
try {
localMap = mapFactory.call();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
subscriber.onError(ex);
Exceptions.throwOrReport(ex, subscriber);
Subscriber<? super T> parent = Subscribers.empty();
parent.unsubscribe();
return parent;
Expand All @@ -110,8 +109,7 @@ public void onNext(T v) {
key = keySelector.call(v);
value = valueSelector.call(v);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
subscriber.onError(ex);
Exceptions.throwOrReport(ex, subscriber);
return;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/rx/internal/operators/OperatorToMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public void onNext(T v) {
key = keySelector.call(v);
value = valueSelector.call(v);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
subscriber.onError(ex);
Exceptions.throwOrReport(ex, subscriber);
return;
}

Expand All @@ -148,8 +147,7 @@ public void onNext(T v) {
try {
collection = collectionFactory.call(key);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
subscriber.onError(ex);
Exceptions.throwOrReport(ex, subscriber);
return;
}
map.put(key, collection);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/rx/internal/operators/UnicastSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ public void onNext(T t) {
try {
s.onNext(t);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.onError(OnErrorThrowable.addValueAsLastCause(ex, t));
Exceptions.throwOrReport(ex, s, t);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/rx/observers/SafeSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ public void onNext(T args) {
} catch (Throwable e) {
// we handle here instead of another method so we don't add stacks to the frame
// which can prevent it from being able to handle StackOverflow
Exceptions.throwIfFatal(e);
// handle errors if the onNext implementation fails, not just if the Observable fails
onError(e);
Exceptions.throwOrReport(e, this);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/rx/observers/SerializedObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public void onNext(T t) {
actual.onNext(t);
} catch (Throwable e) {
terminated = true;
Exceptions.throwIfFatal(e);
actual.onError(OnErrorThrowable.addValueAsLastCause(e, t));
Exceptions.throwOrReport(e, actual, t);
return;
}
for (;;) {
Expand Down

0 comments on commit fe9badc

Please sign in to comment.