3

I use Retrofit as network library with Rx-Java. I want to make some centralized error checking for most requests and handle errors or pass it to subscriber's onError() if I cannot handle it. How could I do this?

Something like this

 projectListObservable
    .subscribeOn(Schedulers.newThread())
    .timeout(App.NETWORK_TIMEOUT_SEC, TimeUnit.SECONDS)
    .retry(App.NETWORK_RETRY_COUNT)
    .onError(e -> {
     if (e instanseOf HttpError && ((HttpError)e).getCode == 403){
        App.getInstance.getNetworkManager.reAuth();
     } else {
        throw(e);
     }})
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(new ProjectListSubscriber());

Also, I should stop retrying in that case, but keep retry if it's a network problem (instanceof IOException).

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
DmitryBorodin
  • 4,584
  • 4
  • 17
  • 29
  • found good answer in different question http://stackoverflow.com/questions/26201420/retrofit-with-rxjava-handling-network-exceptions-globally – DmitryBorodin Jan 09 '16 at 16:01

1 Answers1

1

I think you want to implement too many tihngs with a single observable.

You can have some HttpErrorHandler which will have method

boolean fix(Exception e) 

if error handler fixed the exception(renew token etc) return true. You can have differrent error handlers for different cases or one for everything. TokenErrorHandler RetryErrorHandler and then make a chain.

If all error hadlers return false, throw this exception to the up level

wnc_21
  • 1,751
  • 13
  • 20
  • How will I integrate this HttpErrorHandler with Rx? Will rx understand that if fix returns true - it should retry observable (send one more request) and if false - call next handler's fix method or subscriber's onError? – DmitryBorodin Dec 01 '15 at 10:07
  • I think you can use onErrorResumeNext() which returns new observable, with for loop and handler.fix() calls – wnc_21 Dec 01 '15 at 10:45
  • @user1306579 Looks like a solution that can help me out with my problem. Could you maybe post an answer showing a way to chain error handlers for retrofit calls before they reach the onNext onError methods? – GuyZ Sep 12 '16 at 06:03