O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Central handler

A very convenient and flexible way to implement centralized logging is just to have a handler that is reused in the places where the general exception (error) handling is needed.

Basically, it will be a class that implements the Consumer<Throwable> interface. This way, it can be plugged in anywhere the RxJava is consuming that interface. This includes methods such as .doOnError() or the .subscribe() block.

Let's take a look at how it can be implemented:

public class ErrorHandler implements Consumer<Throwable> {@Override    public void accept(Throwable throwable) throws Exception {        Log.e("APP", "Error on " + Thread.currentThread().getName() +         ":", throwable);    }}

Since it is supposed to be used as a global handler of errors, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required