title: RxJava + Retrofit -> BaseObservable for API calls for centralized response handling
body: <p>I am new to RxJava so please forgive me if this sounds too newbie :-).</p>
  
  <p>As of now I have an abstract CallbackClass that implements the Retofit Callback. There I catch the Callback's "onResponse" and "onError" methods and handle various error types before finally forwarding to the custom implemented methods.
  I also use this centralized class to for request/response app logging and other stuff.</p>
  
  <p>For example: for specific error codes from my sever I receive a new Auth token in the response body, refresh the token and then clone.enqueue the call.
  There are of course several other global behaviors to the responses from my server.</p>
  
  <p>Current solution (Without Rx):</p>
  
  <pre><code>    public abstract void onResponse(Call&lt;T&gt; call, Response&lt;T&gt; response, boolean isSuccess);
  
      public abstract void onFailure(Call&lt;T&gt; call, Response&lt;T&gt; response, Throwable t, boolean isTimeout);
  
      @Override
      public void onResponse(Call&lt;T&gt; call, Response&lt;T&gt; response) {
          if (_isCanceled) return;
  
          if (response != null &amp;&amp; !response.isSuccessful()) {
              if (response.code() == "SomeCode" &amp;&amp; retryCount &lt; RETRY_LIMIT) {
                  TokenResponseModel newToken = null;
                  try {
                      newToken = new Gson().fromJson(new String(response.errorBody().bytes(), "UTF-8"), TokenResponseModel.class);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
                      SomeClass.token = newToken.token;
                      retryCount++;
                      call.clone().enqueue(this);
                      return;
                  }
              }
          } else {
              onResponse(call, response, true);
              removeFinishedRequest();
              return;
          }
  
          onFailure(call, response, null, false);
          removeFinishedRequest();
      }
  
      @Override
      public void onFailure(Call&lt;T&gt; call, Throwable t) {
          if (_isCanceled) return;
  
          if (t instanceof UnknownHostException)
              if (eventBus != null)
                  eventBus.post(new NoConnectionErrorEvent());
  
          onFailure(call, null, t, false);
          removeFinishedRequest();
      }
  </code></pre>
  
  <p>My question is: Is there any way to have this sort of centralized response handling behavior before finally chaining (or retrying) back to the subscriber methods?</p>
  
  <p>I found these 2 links which both have a nice starting point but not a concrete solution. Any help will be really appreciated.</p>
  
  <p><a href="https://stackoverflow.com/questions/26919190/forcing-request-retry-after-custom-api-exceptions-in-rxjava?rq=1">Forcing request retry after custom API exceptions in RxJava</a></p>
  
  <p><a href="https://stackoverflow.com/questions/33740066/retrofit-2-and-rxjava-error-handling-operators">Retrofit 2 and RxJava error handling operators</a></p>