Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In RxJava, what is difference between RxJavaPlugins.setErrorHandler and Subscribe onError?

It seems that there are two kinds of error in RxJava:

  1. Errors caught by a subscriber in onError
  2. And errors caught globally by the handler set by RxJavaPlugins.setErrorHandler

I am having some trouble understanding why this is. Questions:

  • What was the rationale behind having two handlers for errors?
  • What causes an error to be sent to one handler vs the other?
  • How can I ensure that errors are only sent to onError?
like image 373
sdgfsdh Avatar asked Jun 22 '17 14:06

sdgfsdh


1 Answers

You can find most of the design decisions on the Wiki pages for changes done for Rx2:

One important design requirement for 2.x is that no Throwable errors should be swallowed. This means errors that can't be emitted because the downstream's lifecycle already reached its terminal state or the downstream cancelled a sequence which was about to emit an error.

To ensure that errors are only handled by the onError() consumer of the observer, you'd have to set the global handler to an empty consumer:

RxJavaPlugins.setErrorHandler(emptyConsumer());
like image 117
tynn Avatar answered Oct 05 '22 06:10

tynn