9

I want to check if the mobile is connected to internet before invoking my rx retrofit service. If not connected I want to return a fake Response that contains an error.

I ended with the solution below, using defer(), but I think it can be better, any hints ?

private Observable<Response> checkNetwork(Observable<Response> retrofitService) {
        return Observable.defer(new Func0<Observable<Response>>() {
            @Override
            public Observable<Response> call() {
                if (!isOnline()) {
                    return Observable.just(Response.error(R.string.error_no_network_label)));
                }
                return retrofitService;
            }
        });
    }
tbruyelle
  • 12,895
  • 9
  • 60
  • 74

1 Answers1

2

You can implement retrofit ErrorHandler like described here: https://stackoverflow.com/a/21055979/2927901

And then handle thrown exception in doOnError method or your subscribers's onError method.

Community
  • 1
  • 1
VasyaFromRussia
  • 1,872
  • 2
  • 14
  • 18