Closed
Description
I was looking at a discussion in the RxJS related to what happens when onNext throws an exception. I'll try to summarize with a simple example. Lets say the map operator is implemented like this:
void onNext(T t) {
try {
R r = transformer.call(t); // could throw
o.onNext(r); // shouldn't throw but could
} catch(Throwable e) {
o.onError(e);
}
}
And the not so good implementation of a downstream subscriber is:
void onNext(Number r) {
Number s = 1000/r; // could throw a divide by zero or null pointer exception
o.onNext(s); // shouldn't throw but could
}
void onError(Throwable e) {
// handle getting bad divisor from producer
// but also has to handle failure from the onNext above.
}
- Some are arguing that the exceptions from an Observer's onNext should not be sent to the same Observer's onError because it means that the onError has to be able to handle two different types of errors: errors from the producer and errors from itself.
- Others are arguing that the only code to blame for the confusing situation of having to deal with different sources of the errors is the onNext implementation in the same class as the onError and can be easily solved by wrapping the body of the onNext with a try/catch.
The first question to be answered: is this a problem that should be fix? Yes or No please. We'll get into if it is even possible and the how later.