title: Generalize way to handle response and error for all web-service calls
body: <p>I am trying to create generlize approach to handle following </p>
  
  <ol>
  <li>response from webservice </li>
  <li>to handle HTTP error and custom errors</li>
  </ol>
  
  <p>for that i have written following code and crated callbacks to handle response <code>ResponseCallback</code> and errors <code>ErrorCallback</code>.</p>
  
  <p>To fetch data related to nutrition or any other objects i can use the same callbacks.</p>
  
  <pre><code>    Request request = new Request();
      request.setAccess_token("d80fa6bd6f78cc704104d61146c599bc94b82ca225349ee68762fc6c70d2dcf0");
      request.setStart_date("2018-06-01T00:00:00");
      Flowable&lt;Response&gt; fitnessFlowable = new WebRequest()
              .getRemoteClient()
              .create(FitnessApi.class)
              .getFitnessData("5b238abb4d3590001d9b94a8",request.toMap());
  
        fitnessFlowable.subscribeOn(Schedulers.io())
              .takeUntil(response-&gt;response.getSummary().getNext()!=null)
              .doOnNext(new ResponseCallback())
              .doOnError(new ErrorCallback&lt;Throwable&gt;())
              .subscribe();
  </code></pre>
  
  <p><em>Response callback</em> </p>
  
  <pre><code>public  class ResponseCallback&lt;T extends Response&gt; implements Consumer&lt;Response&gt; {
  
  
      @Override
      public void accept(Response response) throws Exception {
          if(response ==null || response.getFitness() == null || response.getFitness().isEmpty()) {
  
  
              new Exception("NUll response");
  
          }
  
          RxBus.getInstance().send(response.getFitness());
         Log.e("Data","accept");
          try(Realm r = Realm.getDefaultInstance()) {
              r.executeTransaction((realm) -&gt; {
                  realm.copyToRealmOrUpdate(response.getFitness());
              });
          }
      }
  }
  </code></pre>
  
  <p><em>Error callback</em></p>
  
  <pre><code>public class ErrorCallback &lt;T extends Throwable&gt; implements Consumer&lt;Throwable&gt; {
  
      public static final String TAG = "ErrorCallback";
      @Override
      public void accept(Throwable e) throws Exception {
  
          if (e instanceof HttpException) {
              ResponseBody body = ((HttpException) e).response().errorBody();
              try {
                  Response response=  LoganSquare.parse(body.byteStream(),Response.class);
  
                  if(response.getErrors() !=null)
                      if(response.getErrors().size() &gt; 0)
                        Log.e(TAG , response.getErrors().get(0).getErrors() );
  
              } catch (IOException t) {
                  t.printStackTrace();
              }
  
          } else if (e instanceof SocketTimeoutException) {
  
              Log.e(TAG , e.getMessage() );
  
          }else if (e instanceof IOException) {
  
              Log.e(TAG , e.getMessage() );
          }else{
              Log.e(TAG , e.getMessage() );
          }
      }
  }
  </code></pre>
  
  <p>Two problems here i am trying to address </p>
  
  <ol>
  <li><p>To handle response and error i need to make two separate callbacks. is there any other way where i can handle response and error in callback rather than creating two separate one</p></li>
  <li><p>When should i dispose the subscribe in above code because you can see in my code that my subscribe is hagging.</p></li>
  </ol>
  
  <p>I have refereed  <a href="https://medium.com/mindorks/rxjava2-and-retrofit2-error-handling-on-a-single-place-8daf720d42d6" rel="nofollow noreferrer">this link</a> to design generalize approach.</p>