title: Volley error handling generalized
body: <p>in my application, I've been thinking of the best way to implement 5xx responses from the server.
  <br/>The first approach was to write my own version of the <code>Request.deliverError</code> method as attached: </p>
  
  <pre><code>@Override
  public void deliverError(VolleyError error) {
      if(error.networkResponse == null){
          super.deliverError(error);
          return;
      }else {
          switch(error.networkResponse.statusCode){
              case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
                  AppInfo.reportDevInfo(GlideApplication.applicationContext, "got a 505 response for request" +this.toString(), null);
                  break;
              case HttpStatus.SC_INTERNAL_SERVER_ERROR:
              case HttpStatus.SC_BAD_GATEWAY:
              case HttpStatus.SC_SERVICE_UNAVAILABLE:
              case HttpStatus.SC_GATEWAY_TIMEOUT:
                  int retryCount = RETRY_COUNT - getRetryPolicy().getCurrentRetryCount();
                  if(retryCount &lt; 0) {
                      super.deliverError(error);
                      return;
                  }
                  String backoff = error.networkResponse.getHeaders.get("Retry-After");
                  if(TextUtils.isEmpty(backoof) == false) {
                      attemptRetryWithNewBackoff(backoff);
                      return;
                  }
                  break;
              }
                  super.deliverError(error)
          }
      }
  }
  </code></pre>
  
  <p>but that only caused ANRs in the application.</p>
  
  <p>Looking at further research, I found <a href="https://github.com/smanikandan14/Volley-demo#handling-error-codes" rel="noreferrer">this blog post</a> that showed a way of handling the different response codes, the only issue, is that I'm not sure how to generalise this to my entire application and implementing handling of 5xx response codes with the appropriate "Retry-After" heade. </p>
  
  <p>Having a class that implements <code>ErrorListener</code> and gets a different one as a param in the constructor seems very costly and inefficient:</p>
  
  <pre><code>public class MyErrorListener implements ErrorListener {
      ErrorListener mListener;
  
      public MyErrorListener(ErrorListener listener) {
          this.mListener = listener;
      }
  
      @Override
      public void onErrorResponse(VolleyError error) {
          if(handleFiveHundredResponse(error) == false) {
              this.mListener.onErrorResponse(error);
          }
      } 
  
  }
  </code></pre>