Version: 2.1.10
I'm having difficulties handling exceptions inside the publish operator. Here's a sample code:
class FooException : RuntimeException()
Observable.error<String>(FooException())
.publish { obs ->
val continuation = obs.onErrorResumeNext { _: Throwable -> Observable.empty() }
obs.mergeWith(continuation)
}
.onErrorResumeNext { _: Throwable -> Observable.just("Bar") }
.subscribe {
println(it)
latch.countDown()
}
I'm expecting the stream to emit "Bar" and end gracefully. Instead, it's crashing the application with an UndeliverableException.
From the error handling wiki (link), if my understanding is correct, the issue is that the stream is emitting more than one exception, leaving one of them "unhandled". Right?
However, since I'm handling all the exceptions of the stream with onErrorResumeNext, I'm failing to see where the issue is. Can someone clarify why I'm getting an exception here? Thank you :)
You are merging the plain obs with the error suppressing derivative, however, mergeWith first gets the error from obs, then cancels the other part. This other part then receives the error again at which point it is already disposed and the error goes into the global error handler. So by merging with obs, some paths will end up sending the shared error into the global error handler.
Use Observable.mergeDelayError(obs, continuation) instead.
Oh, got it!
Very clear and informative answer! Thank you very much 馃憤
Most helpful comment
You are merging the plain
obswith the error suppressing derivative, however,mergeWithfirst gets the error fromobs, then cancels the other part. This other part then receives the error again at which point it is already disposed and the error goes into the global error handler. So by merging withobs, some paths will end up sending the shared error into the global error handler.Use
Observable.mergeDelayError(obs, continuation)instead.