In my last project, I use rxJava and I realize that observable.doOnError('onErrorCallback').subscribe(action) and observable.subscribe(action, 'onErrorCallback') behave in different ways. Even from docs it's not obvious for me what's exactly the difference between them and when I should use first and second variant.
19
votes
2 Answers
23
votes
The doOnError operator allows you to inject side-effect into the error propagation of a sequence, but does not stop the error propagation itself. The Subscriber is the final destination of the events and they 'exit' the sequence.
You can see the usefulness of doOnError with the following example:
api.getData()
.doOnError(e -> log.error(e))
.retry(2)
.subscribe(...)
It allows you to peek into the error but lets you retry in case of an error. With an end subscriber:
api.getData()
.subscribe(v -> {}, e -> log.error(e) );
You have to arrange the handling of the error (besides the logging) on your own way.