title: Angularjs: How to intercept errors in external url $resource or $http service?
body: <p>I have a service available on a server distinct from my grunt server, which serves my angularjs app.<br>
  The external server is an Apache server, serving JSON data with PHP.
  I would like to intercept any possible server error (from "server down", to "404"...).</p>
  
  <p>I have tried to use interceptors, this way:</p>
  
  <pre><code>app.factory('myFactory', function($resource) {
    return $resource('http://www.remote.server.com/api/WRONGPATH',
  });
  
  app.config(function ($routeProvider, $httpProvider) {
  
    ...
  
    $httpProvider.responseInterceptors.push(function($q) {
      return function(promise) {
        return promise.then(function(successResponse) {
          if (successResponse.config.method.toUpperCase() !== 'GET') {
            console.info('success');
          }
          return successResponse;
        }, function(errorResponse) {
          switch (errorResponse.status) {
            case 401:
              console.info('wrong usename or password');
              break;
            case 403:
              console.info('no rights to do this');
              break;
            case 500:
              console.info('server internal error: ' + errorResponse.data);
              break;
            default:
              console.info('error ' + errorResponse.status + ': ' + errorResponse.data);
          }
          return $q.reject(errorResponse);
        });
      };
    });
  
  });
  </code></pre>
  
  <p>with no success: I always fall in the 'default' switch case, with <code>errorResponse.status == 0</code>.<br>
  I suppose this happens because the browsers stop when intercepting a CORS error...<br>
  Everything works just fine if I change the remote url with a local one...</p>
  
  <p><strong>Is there any possibility to handle angular.js $resource (or $http) remote requests errors?</strong></p>
  
  <p><em>I would also accept a design shift suggestion, like reimplementing my PHP service in a grunt-like way... (my PHP service - actually - just prints a JSON list of image names from a server folder...).</em></p>